Headless CMS and Content Managment Hub
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.
 
 
 
 
 

64 lines
1.7 KiB

/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/
import { FormControl, FormGroup } from '@angular/forms';
import { FormGroupTemplate, TemplatedFormGroup } from './templated-form-group';
describe('TemplatedFormGroup', () => {
class Template implements FormGroupTemplate {
public clearCalled = 0;
public removeCalled: number[] = [];
public setControls(form: FormGroup) {
form.setControl('value', new FormControl());
}
public clearControls() {
this.clearCalled++;
}
}
let formTemplate: Template;
let formArray: TemplatedFormGroup;
beforeEach(() => {
formTemplate = new Template();
formArray = new TemplatedFormGroup(formTemplate);
});
type Test = [ (value: any) => void, string];
const methods: Test[] = [
[x => formArray.setValue(x), 'setValue'],
[x => formArray.patchValue(x), 'patchValue'],
[x => formArray.reset(x), 'reset'],
];
methods.forEach(([method, name]) => {
it(`Should call template to construct controls for ${name}`, () => {
const value1 = {
value: 1,
};
method(value1);
expect(formArray.value).toEqual(value1);
});
it(`Should call template to clear items with for ${name}`, () => {
const value1 = {
value: 1,
};
method(value1);
method(undefined);
expect(formArray.value).toEqual(undefined);
expect(formTemplate.clearCalled).toEqual(1);
expect(formTemplate.removeCalled).toEqual([]);
});
});
});