Skip to content

Commit 224e550

Browse files
author
mleimer
authored
docs: add selector with attribute example (#264)
Closes #263
1 parent 0e5e3c7 commit 224e550

5 files changed

+30
-6
lines changed

Diff for: apps/example-app/src/app/examples/02-input-output.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ test('is possible to set input and listen for output', async () => {
3333
test('is possible to set input and listen for output with the template syntax', async () => {
3434
const sendSpy = jest.fn();
3535

36-
await render(InputOutputComponent, {
37-
template: '<app-fixture [value]="47" (sendValue)="sendValue($event)" (clicked)="clicked()"></app-fixture>',
36+
await render('<app-fixture [value]="47" (sendValue)="sendValue($event)" (clicked)="clicked()"></app-fixture>', {
37+
declarations: [InputOutputComponent],
3838
componentProperties: {
3939
sendValue: sendSpy,
4040
},

Diff for: apps/example-app/src/app/examples/11-ng-content.spec.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import { TestBed } from '@angular/core/testing';
21
import { render, screen } from '@testing-library/angular';
32

43
import { CellComponent } from './11-ng-content';
54

65
test('it is possible to test ng-content without selector', async () => {
76
const projection = 'it should be showed into a p element!';
87

9-
TestBed.overrideComponent(CellComponent, { set: { selector: 'cell' } });
10-
await render(CellComponent, {
11-
template: `<cell data-testid="one-cell-with-ng-content">${projection}</cell>`,
8+
await render(`<app-fixture data-testid="one-cell-with-ng-content">${projection}</app-fixture>`, {
9+
declarations: [CellComponent]
1210
});
1311

1412
expect(screen.getByText(projection)).toBeInTheDocument();

Diff for: apps/example-app/src/app/examples/11-ng-content.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, ChangeDetectionStrategy } from '@angular/core';
22

33
@Component({
4+
selector: 'app-fixture',
45
template: `
56
<p>
67
<ng-content></ng-content>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { render, screen } from '@testing-library/angular';
2+
import {ComponentWithAttributeSelectorComponent} from './17-component-with-attribute-selector';
3+
4+
// Note: At this stage it is not possible to use the render(ComponentWithAttributeSelectorComponent, {...}) syntax
5+
// for components with attribute selectors!
6+
test('is possible to set input of component with attribute selector through template', async () => {
7+
await render(`<app-fixture-component-with-attribute-selector [value]="42"></app-fixture-component-with-attribute-selector>`, {
8+
declarations: [ComponentWithAttributeSelectorComponent]
9+
});
10+
11+
const valueControl = screen.getByTestId('value');
12+
13+
expect(valueControl).toHaveTextContent('42');
14+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Component, Input } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-fixture-component-with-attribute-selector[value]',
5+
template: `
6+
<span data-testid="value">{{ value }}</span>
7+
`,
8+
})
9+
export class ComponentWithAttributeSelectorComponent {
10+
@Input() value!: number;
11+
}

0 commit comments

Comments
 (0)