Allow to get control value from MockControlValueAccessor
See original GitHub issueWhen testing angular components, we use the Page Object pattern to simulate the user experience and access the form field not through the component, but through view. Thus, we check the correctness of the bindings in the template and abstract from the internal implementation. In this case, we can change the Reactive Forms to Template-Driven Forms without changing the tests. So, I would like to refer to the control value not directly, but indirectly, through the template.
Example
ngOnInit() {
this.loginService.getLogin().subscribe(login => this.loginControl.setValue(login));
}
it('If login comes in, then it is put in the form field', () => {
// arrange
when(loginServiceMock.getLogin()).thenReturn('login');
// act
fixture.detectChanges();
// assert
expect(pageObject.loginInput.__value).toBe('login');
});
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (7 by maintainers)
Top Results From Across the Web
Allow to get control value from MockControlValueAccessor #126
When testing angular components, we use the Page Object pattern to simulate the user experience and access the form field not through the ......
Read more >How to mock form controls in Angular tests
How to mock form controls in Angular tests ; ngModel; ngModelChange; formControl; formControlName ; NG_VALUE_ACCESSOR; ControlValueAccessor; writeValue ...
Read more >How to do angular unit testing for a customize component ...
I created a customized searchable dropdown. For this angular component, it implemented ControlValueAccessor and also got NG_VALUE_ACCESSOR, ...
Read more >Testing Forms Using the CVA | Jennifer Wadella
You've mastered the Control Value Accessor, now you need to learn how to unit test components implementing it. For good unit tests we...
Read more >29 | Unit testing form group setValue method using ... - YouTube
In this video, you will learn How to test setvalue method of a form group with form controls.1. How to test set value...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@satanTime In your version you will work with controller of component, in our case we are trying to abstract of realisation and write test on component as Black Box. We have template and mocking of data and out tests writing as Controller not exists, in my opinion it’s better, than i will assert that i have formControl, than i will test that i call method on FormControl, after 2 weeks i decide to use NgModel and i will rewrite all my tests, just because i test realisation
The thing about
formControl
andngModel
is that they should be mocked as all other dependencies in unit tests too, therefore there won’t be code that callswriteValue
and therefore we cannot rely on assertions ofwriteValue
, we can only check that we passed proper inputs / outputs and believe that real directives work as expected.