Implementing a FormGroup within the context of a Mat Dialog window, I am aiming to validate the method that returns the FormGroup through unit tests.
The method triggered on button click is as follows:
closeDialogAndSendForm(): void {
this.dialogWindow.close(this.form)
}
My unit test scenario involves (I have set up a form with just one field for testing purposes):
it('should close dialog and return FormGroup', fakeAsync(async () => {
await selectAnswers();
let resultFormGroup = createResultFormGroup();
let spy = spyOn(component.dialogWindow, 'close');
flush();
fixture.detectChanges();
clickOnButton('SUBMIT', fixture);
expect(spy).toHaveBeenCalledWith(resultFormGroup);
}));
In the function createResultFormGroup()
, my intention is to generate an identical form structure as the one returned by the previously mentioned method:
function createResultFormGroup(): FormGroup {
let form = new FormGroup({testField: new FormControl(true, Validators.required)});
return form;
}
However, upon running the test, multiple discrepancies arise such as the ones stated below:
Call 0:
Expected $[0]._onCollectionChange = Function to equal Function.
Expected $[0].pristine = false to equal true.
...
Error: Expected spy close to have been called with:
Is there any way to mock this behavior or should I focus solely on comparing field names and values?