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.

Does shallow-render support ViewChild queries?

See original GitHub issue

Hello,

I have the next component tree:

            AppComponent
                 |
                 |
           ParentComponent
                 |
                 |
            ChildComponent

The ParentComponent has a ViewChild reference to ChildComponent. When i mock ChildComponent by hand in the ParentComponent spec, it renders but never gets the reference to the ViewChild. Is any way to solve this using shallow-render?

If it helps, this repo has an example of the case i explained above: https://github.com/Ivykp/test-viewchild-mock

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ike18tcommented, Oct 16, 2018

Hi @ticklemycode ,

I noticed a few problems with your stackblitz example:

  • Needs to use dontMock or neverMock the FormsModule
  • Needed to dispatchEvent on the input so that ngModel will update
  • “value” was misspelled in the assertions

Here are my updates: https://stackblitz.com/edit/github-jc7sw5-bq4p3x?file=examples%2Fcomponent-with-view-child.spec.ts

2reactions
getsafcommented, Oct 15, 2018

Looks like the issue you’re seeing here is that, Shallow mocks ALL modules except for CommonModule by default. This means, Angular’s FormsModule is being mocked but your test is expecting stuff from the FormsModule to behave normally. Since most (all?) of the things in the FormsModule are self-contained (eg: nothing inside the module has dependencies outside the forms module), you can/should safely be able to use the real FormsModule in your specs.

If your project uses FormsModule excessively, I’d recommend neverMocking it in your karma setup:

Shallow.neverMock(FormsModule);

If you just need a one-off, you can do it right in your test:

shallow = new Shallow(MyComponent, MyModule).dontMock(FormsModule);

^-- This worked in my fork of your StackBlitz example

Read more comments on GitHub >

github_iconTop Results From Across the Web

ViewChild - Angular
Property decorator that configures a view query. The change detector looks for the first element or the directive matching the selector in the...
Read more >
Shallow Render
If my test uses a mock that does not match the contract of the service it is mocking. ... All shallow-render queries return...
Read more >
Angular @ViewChild: In-Depth Explanation (All Features ...
What is the scope of @ViewChild template queries? using @ViewChild to inject a component; how to use @ViewChild to inject a plain HTML...
Read more >
angular/angular - Gitter
Hi Does anyone use shallow-render when testing? I'm attempting to write a test for a structural directive but am not sure how I...
Read more >
Understanding the ViewChild and ViewChildren decorators in ...
A view query is a requested reference to a child element within a ... the @ViewChild documentation, five kinds of selectors are supported....
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