Does shallow-render support ViewChild queries?
See original GitHub issueHello,
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:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
Hi @ticklemycode ,
I noticed a few problems with your stackblitz example:
dontMock
orneverMock
the FormsModuledispatchEvent
on the input so that ngModel will updateHere are my updates: https://stackblitz.com/edit/github-jc7sw5-bq4p3x?file=examples%2Fcomponent-with-view-child.spec.ts
Looks like the issue you’re seeing here is that, Shallow mocks ALL modules except for
CommonModule
by default. This means, Angular’sFormsModule
is being mocked but your test is expecting stuff from theFormsModule
to behave normally. Since most (all?) of the things in theFormsModule
are self-contained (eg: nothing inside the module has dependencies outside the forms module), you can/should safely be able to use the realFormsModule
in your specs.If your project uses
FormsModule
excessively, I’d recommendneverMock
ing it in your karma setup:If you just need a one-off, you can do it right in your test:
^-- This worked in my fork of your StackBlitz example