Search code examples
angularunit-testingangular-reactive-forms

Angular - Forcing a reactive form to be valid in a unit test


I am using Angular v4.4.4. In a component, after a button is clicked in the template, the form is saved assuming the reactive form is valid. Something like (pseudo-code):

public onSave(): void {
    if (this.myForm.valid) {
        this._createFoo();
    }
}

private _createFoo(): void {
    this._fooService.createItem(this.foo).subscribe(result => {
        // stuff happens...
    });
}

In the related unit test I need to force the form to be valid so I can confirm the service is being called. Something like this:

it('should create Foo', () => {
    const spy = spyOn(_fooService, 'createItem').and.callThrough();
    component.foo = new Foo();
    fixture.detectChanges();
    const bookButton = fixture.debugElement.query(By.css('#bookButton'));
    expect(bookButton !== null).toBeTruthy('missing Book button');
    bookButton.triggerEventHandler('click', null);
    expect(spy).toHaveBeenCalled();
});

This will fail because myForm is never set as valid.

In this particular case I do not want to give every input in the form a value. I just need to watch and see if the service subscription occurs. How can I force the form to be valid?


Solution

  • Why not just clear the validators list?

    // If there are any async validators
    //
    this.myForm.clearAsyncValidators();
    // If there are any normal validators
    //
    this.myForm.clearValidators();
    // Doing the validation on all the controls to put them back to valid
    //
    this.formGroup.updateValueAndValidity();
    

    This will ensure your form has no validators, thus being valid.