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.

Mocked component and host decorator

See original GitHub issue

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

github_iconTop GitHub Comments

1reaction
ike18tcommented, May 29, 2018

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:

      providers: [ 
        { 
          provide: AppComponent, 
          useClass: MockComponent(AppComponent)
        } 
      ]
0reactions
kenborgecommented, Jun 16, 2018

@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.

Read more comments on GitHub >

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

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