mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.3 KiB
89 lines
2.3 KiB
/*
|
|
* Squidex Headless CMS
|
|
*
|
|
* @license
|
|
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
|
|
*/
|
|
|
|
import { FormArray, FormControl } from '@angular/forms';
|
|
import { UndefinableFormArray } from './undefinable-form-array';
|
|
|
|
describe('UndefinableFormArray', () => {
|
|
const tests = [{
|
|
name: 'undefined',
|
|
value: undefined,
|
|
}, {
|
|
name: 'defined',
|
|
value: ['1'],
|
|
}];
|
|
|
|
tests.forEach(x => {
|
|
it(`should set value as <${x.name}>`, () => {
|
|
const control =
|
|
new UndefinableFormArray([
|
|
new FormControl(''),
|
|
]);
|
|
|
|
assertValue(control, x.value, () => {
|
|
control.setValue(x.value);
|
|
});
|
|
});
|
|
});
|
|
|
|
tests.forEach(x => {
|
|
it(`should patch value as <${x.name}>`, () => {
|
|
const control =
|
|
new UndefinableFormArray([
|
|
new FormControl(''),
|
|
]);
|
|
|
|
assertValue(control, x.value, () => {
|
|
control.patchValue(x.value);
|
|
});
|
|
});
|
|
});
|
|
|
|
tests.forEach(x => {
|
|
it(`should reset value as <${x.name}>`, () => {
|
|
const control =
|
|
new UndefinableFormArray([
|
|
new FormControl(''),
|
|
]);
|
|
|
|
assertValue(control, x.value, () => {
|
|
control.reset(x.value);
|
|
});
|
|
});
|
|
});
|
|
|
|
it('should reset value back after push', () => {
|
|
const control = new UndefinableFormArray([]);
|
|
|
|
assertValue(control, ['1'], () => {
|
|
control.setValue(undefined);
|
|
control.push(new FormControl('1'));
|
|
});
|
|
});
|
|
|
|
it('should reset value back after insert', () => {
|
|
const control = new UndefinableFormArray([]);
|
|
|
|
assertValue(control, ['1'], () => {
|
|
control.setValue(undefined);
|
|
control.insert(0, new FormControl('1'));
|
|
});
|
|
});
|
|
|
|
function assertValue(control: FormArray, expected: any, action: () => void) {
|
|
let currentValue: any;
|
|
|
|
control.valueChanges.subscribe(value => {
|
|
currentValue = value;
|
|
});
|
|
|
|
action();
|
|
|
|
expect(currentValue).toEqual(expected);
|
|
expect(control.getRawValue()).toEqual(expected);
|
|
}
|
|
});
|
|
|