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.

How to test components with ng-content?

See original GitHub issue

Hey, I’m testing components that rely completely in <ng-content>.

Here is a component I want to test:

import { Component, ChangeDetectionStrategy, Inject } from '@angular/core';

import { use } from '@core/utils';

import { CONTEXT } from '../table/tokens';

@Component({
  template: `
    <p data-testid="too-long-cell" [style.width]="context">
      <ng-content></ng-content>
    </p>
  `,
  styles: [
    `
      p {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    `,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TooLongCellComponent {
  constructor(@Inject(CONTEXT) public context = '150px') {}
}

And I have written this test:

import { Component, ChangeDetectionStrategy } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { render } from '@testing-library/angular';

import { CONTEXT } from '../table/tokens';
import { TooLongCellComponent } from './too-long-cell.component';

export function getTestingComponent(template: string) {
  @Component({
    template,
    changeDetection: ChangeDetectionStrategy.OnPush,
  })
  class TestComponent {}

  return TestComponent;
}

function createComponent({ width }: { width?: number } = {}) {
  TestBed.overrideComponent(TooLongCellComponent, { set: { selector: 'cell' } });

  return render(getTestingComponent('<cell>projected value</cell>'), {
    providers: [
      {
        provide: CONTEXT,
        useValue: width,
      },
    ],
    declarations: [TooLongCellComponent],
  });
}

describe(TooLongCellComponent, () => {
 it('should add a width property with 100px', async () => {
    const { getByTestId } = await createComponent({ width: '100px' });

    const cell = getByTestId('too-long-cell');

    expect(cell.style.width).toBe('100px');
    expect(cell).toHaveTextContent(/^projected value$/);
  });
});

Could we improve the testing of <ng-content> using angular-testing-library? Or should I do in this way?

Thanks 👍

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
timdeschryvercommented, Mar 21, 2020

Hi thanks for raising this issue, it’s the first time!

I haven’t tested this, but could you use the same approach as with directives? https://github.com/testing-library/angular-testing-library/blob/master/src/app/examples/08-directive.spec.ts

1reaction
tonivj5commented, Mar 24, 2020

No matter, thanks for the time and the template option! 🚀

I will create an example with my use-case 😸

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2 test ng-content - Stack Overflow
I'm wondering if there is a way to test ng-content without creating a host element? For example, if I have alert component -...
Read more >
How to test <ng-content> in angular 2 - Google Groups
How can I test that my component is transcluding the content? For example: import {Component, Input} from '@angular/core'; @Component({
Read more >
Testing Components – Testing Angular
Introduction to testing Angular Components with Angular's TestBed. ... It renders the content ( ng-content ) and templates ( ng-template ) ...
Read more >
Angular ng-content and Content Projection: A Complete Guide ...
In this post, we are going to learn how to use this feature to design components that have a very simple but still...
Read more >
[Angular Unit Testing] Testing component with content projection
Idea is to create a WrapperComponent to include our component. Then we are able to project content which need to be passed into...
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