question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow to get control value from MockControlValueAccessor

See original GitHub issue

When 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:closed
  • Created 3 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
vladiagcommented, May 22, 2020

@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

0reactions
satanTimecommented, May 22, 2020

The thing about formControl and ngModel is that they should be mocked as all other dependencies in unit tests too, therefore there won’t be code that calls writeValue and therefore we cannot rely on assertions of writeValue, we can only check that we passed proper inputs / outputs and believe that real directives work as expected.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found