I am trying to unit test a simple reactive form in Angular 5, and don't understand why setting the value in a unit test does not work.
I build the form in my ngOnInit, with the formbuilder:
component.ts
ngOnInit() {
this.loginForm = this.fb.group({
email: ['', [Validators.required, ValidateEmail]],
password: ['', Validators.required]
});
}
In my unit test, I have the normal CLI setutp, in befofreEach:
beforeEach(() => {
fixture = TestBed.createComponent(SignInComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
My unit test then looks like this:
fit('login-form printing unexpected value', fakeAsync(() => {
const form = component.loginForm;
fixture.detectChanges();
tick();
form.controls['email'].setValue('aaa');
console.log(form.get('email')); // PRINTS bbb
fixture.detectChanges();
tick();
form.controls['email'].setValue('bbb');
console.log(form.get('email'));// PRINTS bbb
}));
I tried printing only
console.log(form.get('email').value)
And then the value is aaa and bbb as expected. I am very confused and hope you can help me understand this.