Testing ng-template templates projected into dependency component?
See original GitHub issueThe component I am trying to test uses a PrimeNG Datatable, which accepts various things as named ng-templates. (e.g. a caption)
I am trying to test that the templates being projected into the mocked dependency are correct, but can’t see any way to do that.
Am I missing something, or is this not possible?
I had a look at ng-mocks and noticed it does have an example using MockRender() and MockedComponent<>.__render(), but it is testing the dependency component, not involving the component under test at all…
e.g.
under-test.component.ts
@Input() caption: string;
under-test.component.html
<dependency-component>
<ng-template pTemplate="caption"><div class="my-caption">{{caption}}</div></ng-template>
</dependency-component>
under-test.component.spec.ts
const rendering = new Shallow(UnderTestComponent, M).render({ bind: { caption: 'Test' } });
rendering.find('.my-caption'); // hasn't been rendered
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Component testing scenarios - Angular
Many components specify external templates and external css with the ... If you created your project with the Angular CLI, zone-testing is already...
Read more >ngTemplateOutlet: The secret to customisation - Angular inDepth
ngTemplateOutlet is a powerful tool for creating customisable components. It is used by many Angular libraries to enable users to provide custom templates....
Read more >How to use the ng-mocks.MockRender function in ng-mocks
MockRender examples, based on popular ways it is used in public projects. ... const localFixture = MockRender(` <dependency-component-selector> <ng-template ...
Read more >Angular — Four practical use cases of NgTemplateOutlet
Similar to the previously example, we can wrap the HTML with a ng-template and apply context on it. @Component({ selector: 'app-test', template: ...
Read more >How to access a service inside TemplateRef which was ...
Angular v14 solution. With Angular 14, you can pass injector to ngTemplateOutlet directive. And by that way, the embeddedView that is going ...
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
FYI, I did not get this worked out in a generic way. Unfortunately, I see no way to do this except:
If you have any other ideas or want to give it a go in a PR to prove out an idea we can continue trying. I don’t even mind taking a functional POC in a PR and formalizing the code in my free time from there.
Let me know if you want to pick this back up. Gonna close the issue for now.
This is an interesting use-case. It seems like what you need here is something from Shallow that can render templates (with optional context) on-demand.
I’m bummed that there is no type-safety here but there isn’t any in the Angular implementation either.
Does this solution sound work-able to you? I’m making a few assumptions because I’ve never used PrimeNG before. Definitely open to suggestions.