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.

Testing ng-template templates projected into dependency component?

See original GitHub issue

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

github_iconTop GitHub Comments

2reactions
getsafcommented, Aug 24, 2020

FYI, I did not get this worked out in a generic way. Unfortunately, I see no way to do this except:

  • Using the actual PrimeNG directives (which may work out well)
  • Providing hand-written mock replacements of the PrimeNG directives that mimic the same behavior.
  • Writing a host-component that mimics the behavior of the PrimeNG directives (not sure if a host component would have access to a child’s templates though).

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.

2reactions
getsafcommented, Jun 3, 2020

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.

const rendering = await shallow.render();
renderTemplate('#my-template-id', {caption: 'Here is my caption'});

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.

Read more comments on GitHub >

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

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