mirror of https://github.com/Squidex/squidex.git
Browse Source
* Get rid of formbuilder * Temp * Cleanup forms. * More tests. * More improvements. * Cleanup.pull/802/head
committed by
GitHub
70 changed files with 1161 additions and 980 deletions
@ -0,0 +1,128 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
||||
|
*/ |
||||
|
|
||||
|
import { FormArray, FormControl } from '@angular/forms'; |
||||
|
import { ExtendedFormArray, UndefinableFormArray } from './extended-form-array'; |
||||
|
|
||||
|
describe('ExtendedFormArray', () => { |
||||
|
it('should provide value even if controls are disabled', () => { |
||||
|
const control = new ExtendedFormArray([ |
||||
|
new FormControl('1'), |
||||
|
new FormControl('2'), |
||||
|
]); |
||||
|
|
||||
|
expect(control.value).toEqual(['1', '2']); |
||||
|
|
||||
|
assertValue(control, ['1', '2'], () => { |
||||
|
control.controls[0].disable(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe('UndefinableFormArray', () => { |
||||
|
const tests = [{ |
||||
|
name: 'undefined (on)', |
||||
|
undefinable: true, |
||||
|
valueExpected: undefined, |
||||
|
valueActual: undefined, |
||||
|
}, { |
||||
|
name: 'defined (on)', |
||||
|
undefinable: true, |
||||
|
valueExpected: [1], |
||||
|
valueActual: [1], |
||||
|
}, { |
||||
|
name: 'defined (off)', |
||||
|
undefinable: false, |
||||
|
valueExpected: [1], |
||||
|
valueActual: [1], |
||||
|
}]; |
||||
|
|
||||
|
it('should provide value even if controls are disabled', () => { |
||||
|
const control = new UndefinableFormArray([ |
||||
|
new FormControl('1'), |
||||
|
new FormControl('2'), |
||||
|
]); |
||||
|
|
||||
|
expect(control.value).toEqual(['1', '2']); |
||||
|
|
||||
|
assertValue(control, ['1', '2'], () => { |
||||
|
control.controls[0].disable(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
tests.forEach(x => { |
||||
|
it(`should set value as <${x.name}>`, () => { |
||||
|
const control = buildControl(x.undefinable); |
||||
|
|
||||
|
assertValue(control, x.valueExpected, () => { |
||||
|
control.setValue(x.valueActual as any); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
tests.forEach(x => { |
||||
|
it(`should patch value as <${x.name}>`, () => { |
||||
|
const control = buildControl(x.undefinable); |
||||
|
|
||||
|
assertValue(control, x.valueExpected, () => { |
||||
|
control.patchValue(x.valueActual as any); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
tests.forEach(x => { |
||||
|
it(`should reset value as <${x.name}>`, () => { |
||||
|
const control = buildControl(x.undefinable); |
||||
|
|
||||
|
assertValue(control, x.valueExpected, () => { |
||||
|
control.reset(x.valueActual as any); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
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 buildControl(undefinable: boolean) { |
||||
|
return undefinable ? |
||||
|
new UndefinableFormArray([ |
||||
|
new FormControl(''), |
||||
|
]) : |
||||
|
new ExtendedFormArray([ |
||||
|
new FormControl(''), |
||||
|
]); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
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); |
||||
|
expect(control.value).toEqual(expected); |
||||
|
} |
||||
@ -0,0 +1,110 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
||||
|
*/ |
||||
|
|
||||
|
import { FormControl, FormGroup } from '@angular/forms'; |
||||
|
import { ExtendedFormGroup, UndefinableFormGroup } from './extended-form-group'; |
||||
|
|
||||
|
describe('UndefinableFormGroup', () => { |
||||
|
it('should provide value even if controls are disabled', () => { |
||||
|
const control = new ExtendedFormGroup({ |
||||
|
test1: new FormControl('1'), |
||||
|
test2: new FormControl('2'), |
||||
|
}); |
||||
|
|
||||
|
expect(control.value).toEqual({ test1: '1', test2: '2' }); |
||||
|
|
||||
|
assertValue(control, { test1: '1', test2: '2' }, () => { |
||||
|
control.controls['test1'].disable(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe('ExtendedFormGroup', () => { |
||||
|
const tests = [{ |
||||
|
name: 'undefined (on)', |
||||
|
undefinable: true, |
||||
|
valueExpected: undefined, |
||||
|
valueActual: undefined, |
||||
|
}, { |
||||
|
name: 'defined (on)', |
||||
|
undefinable: true, |
||||
|
valueExpected: { field: 1 }, |
||||
|
valueActual: { field: 1 }, |
||||
|
}, { |
||||
|
name: 'defined (off)', |
||||
|
undefinable: false, |
||||
|
valueExpected: { field: 1 }, |
||||
|
valueActual: { field: 1 }, |
||||
|
}]; |
||||
|
|
||||
|
it('should provide value even if controls are disabled', () => { |
||||
|
const control = new ExtendedFormGroup({ |
||||
|
test1: new FormControl('1'), |
||||
|
test2: new FormControl('2'), |
||||
|
}); |
||||
|
|
||||
|
expect(control.value).toEqual({ test1: '1', test2: '2' }); |
||||
|
|
||||
|
assertValue(control, { test1: '1', test2: '2' }, () => { |
||||
|
control.controls['test1'].disable(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
tests.forEach(x => { |
||||
|
it(`should set value as <${x.name}>`, () => { |
||||
|
const control = buildControl(x.undefinable); |
||||
|
|
||||
|
assertValue(control, x.valueExpected, () => { |
||||
|
control.setValue(x.valueActual as any); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
tests.forEach(x => { |
||||
|
it(`should patch value as <${x.name}>`, () => { |
||||
|
const control = buildControl(x.undefinable); |
||||
|
|
||||
|
assertValue(control, x.valueExpected, () => { |
||||
|
control.patchValue(x.valueActual as any); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
tests.forEach(x => { |
||||
|
it(`should reset value as <${x.name}>`, () => { |
||||
|
const control = buildControl(x.undefinable); |
||||
|
|
||||
|
assertValue(control, x.valueExpected, () => { |
||||
|
control.reset(x.valueActual); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
function buildControl(undefinable: boolean) { |
||||
|
return undefinable ? |
||||
|
new UndefinableFormGroup({ |
||||
|
field: new FormControl(), |
||||
|
}) : |
||||
|
new ExtendedFormGroup({ |
||||
|
field: new FormControl(), |
||||
|
}); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
function assertValue(control: FormGroup, expected: any, action: () => void) { |
||||
|
let currentValue: any; |
||||
|
|
||||
|
control.valueChanges.subscribe(value => { |
||||
|
currentValue = value; |
||||
|
}); |
||||
|
|
||||
|
action(); |
||||
|
|
||||
|
expect(currentValue).toEqual(expected); |
||||
|
expect(control.getRawValue()).toEqual(expected); |
||||
|
expect(control.value).toEqual(expected); |
||||
|
} |
||||
@ -1,89 +0,0 @@ |
|||||
/* |
|
||||
* 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); |
|
||||
} |
|
||||
}); |
|
||||
@ -1,71 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|
||||
*/ |
|
||||
|
|
||||
import { FormControl, FormGroup } from '@angular/forms'; |
|
||||
import { UndefinableFormGroup } from './undefinable-form-group'; |
|
||||
|
|
||||
describe('UndefinableFormGroup', () => { |
|
||||
const tests = [{ |
|
||||
name: 'undefined', |
|
||||
value: undefined, |
|
||||
}, { |
|
||||
name: 'defined', |
|
||||
value: { field: ['1'] }, |
|
||||
}]; |
|
||||
|
|
||||
tests.forEach(x => { |
|
||||
it(`should set value as <${x.name}>`, () => { |
|
||||
const control = |
|
||||
new UndefinableFormGroup({ |
|
||||
field: new FormControl(), |
|
||||
}); |
|
||||
|
|
||||
assertValue(control, x.value, () => { |
|
||||
control.setValue(x.value); |
|
||||
}); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
tests.forEach(x => { |
|
||||
it(`should patch value as <${x.name}>`, () => { |
|
||||
const control = |
|
||||
new UndefinableFormGroup({ |
|
||||
field: new FormControl(), |
|
||||
}); |
|
||||
|
|
||||
assertValue(control, x.value, () => { |
|
||||
control.patchValue(x.value); |
|
||||
}); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
tests.forEach(x => { |
|
||||
it(`should reset value as <${x.name}>`, () => { |
|
||||
const control = |
|
||||
new UndefinableFormGroup({ |
|
||||
field: new FormControl(), |
|
||||
}); |
|
||||
|
|
||||
assertValue(control, x.value, () => { |
|
||||
control.reset(x.value); |
|
||||
}); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
function assertValue(control: FormGroup, expected: any, action: () => void) { |
|
||||
let currentValue: any; |
|
||||
|
|
||||
control.valueChanges.subscribe(value => { |
|
||||
currentValue = value; |
|
||||
}); |
|
||||
|
|
||||
action(); |
|
||||
|
|
||||
expect(currentValue).toEqual(expected); |
|
||||
expect(control.getRawValue()).toEqual(expected); |
|
||||
} |
|
||||
}); |
|
||||
Loading…
Reference in new issue