-
Notifications
You must be signed in to change notification settings - Fork 91
How to test components with ng-content? #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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 |
I'm going to try it rigth now! 👍 |
It works 😄! Thanks for the hint 💪 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';
function createComponent({ width }: { width?: number } = {}) {
TestBed.overrideComponent(TooLongCellComponent, { set: { selector: 'cell' } });
return render(TooLongCellComponent, {
template: '<cell>projected value</cell>',
providers: [
{
provide: CONTEXT,
useValue: width,
},
],
});
}
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$/);
});
}); I guess I'm going to keep needing this line |
I'm leaning towards leaving it as is, since this is a very specific use case, and this would add an extra option to the API. |
No matter, thanks for the time and the I will create an example with my use-case 😸 |
I have created a PR to add the use-case #82 |
🎉 This issue has been resolved in version 9.1.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Hey, I'm testing components that rely completely in
<ng-content>
.Here is a component I want to test:
And I have written this test:
Could we improve the testing of
<ng-content>
usingangular-testing-library
? Or should I do in this way?Thanks 👍
The text was updated successfully, but these errors were encountered: