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?