Mocked component and host decorator
See original GitHub issueIf you have 2 nested components like this
<outer>
<inner></inner>
</outer>
And the inner component uses a host decorator to inject the outer component in it’s constructor
constructor( @Host() public outer: OuterComponent ) { }
When you want to test the inner component it does not seem like it’s possible to mock outer component in the tests as the component declared in the TestBed MockComponent(OuterComponent)
is not found and injected into the constructor of the inner component.
Is there any way to make this work?
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Issue with Decorator when testing component #91 - GitHub
I cannot post real example so I was doing a mocking scenario to show you ;). Working Component and Test: @UntilDestroy() @Component({ selector: ......
Read more >(Angular Unit-Test) How to mock input property in Jasmin?
The input property works just like any variable. In your beforeEach you could set it to a value beforeEach(() => { fixture =...
Read more >Component testing scenarios - Angular
This particular test suite supplies a minimal mock of the UserService that ... Clicking the hero should raise a selected event that the...
Read more >Testing Angular Components With @Input()
To test components that bind an input via the @Input() decorator, we can create a host component in our test to wrap our...
Read more >A curious case of the @Host decorator and Element Injectors ...
This article dives deep into mechanics of @Host decorator in Angular. It introduces rarely seen notion of Element Injector and shows how it's...
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
Here’s an example I whipped up really quick:
https://stackblitz.com/edit/bah?embed=1&file=app/hello.component.spec.ts
The meat of it is:
@ike18t yeah it did work using the viewProviders approach. I would still consider it a bug though or at least a limitation of the mock framework that maybe should be documented.