Skip to content

Commit d61fd25

Browse files
add standalone component example (#1132)
1 parent b6f8cf5 commit d61fd25

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

docs/angular-testing-library/api.mdx

+16
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ await render(AppComponent, {
133133
})
134134
```
135135

136+
### `ɵcomponentImports`
137+
138+
A collection of imports to override a standalone component's imports with.
139+
140+
**default** : `undefined`
141+
142+
**example**:
143+
144+
```typescript
145+
await render(AppComponent, {
146+
ɵcomponentImports: [
147+
MockChildComponent
148+
]
149+
})
150+
```
151+
136152
### `detectChanges`
137153

138154
Will call `detectChanges` when the component is compiled

docs/angular-testing-library/examples.mdx

+67-6
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,77 @@ describe('Counter', () => {
5656
})
5757
```
5858

59+
## With Standalone Components
60+
61+
Angular Testing Library can also test your standalone components.
62+
In fact, it even becomes easier because you don't have to set up the whole Angular TestBed.
63+
This was previously only [possible when you used Single Component Angular Modules (SCAMs)](https://timdeschryver.dev/blog/single-component-angular-modules-and-component-tests-go-hand-in-hand).
64+
65+
Let's migrate the counter component to a standalone component, and let's take a look at how this affects the test.
66+
67+
```ts title="counter.component.ts"
68+
@Component({
69+
selector: 'counter',
70+
template: `
71+
<button (click)="decrement()">-</button>
72+
<span data-testid="count">Current Count: {{ counter }}</span>
73+
<button (click)="increment()">+</button>
74+
`,
75+
standalone: true,
76+
})
77+
export class CounterComponent {
78+
@Input() counter = 0
79+
80+
increment() {
81+
this.counter += 1
82+
}
83+
84+
decrement() {
85+
this.counter -= 1
86+
}
87+
}
88+
```
89+
90+
Just as you would've expected, this doesn't affect the test cases.
91+
Writing tests for standalone components remains the same as for "regular" components.
92+
93+
```ts title="counter.component.spec.ts"
94+
import {render, screen, fireEvent} from '@testing-library/angular'
95+
import {CounterComponent} from './counter.component.ts'
96+
97+
describe('Counter', () => {
98+
test('should render counter', async () => {
99+
await render(CounterComponent, {
100+
componentProperties: {counter: 5},
101+
})
102+
103+
expect(screen.getByText('Current Count: 5')).toBeInTheDocument()
104+
})
105+
106+
test('should increment the counter on click', async () => {
107+
await render(CounterComponent, {
108+
componentProperties: {counter: 5},
109+
})
110+
111+
fireEvent.click(screen.getByText('+'))
112+
113+
expect(screen.getByText('Current Count: 6')).toBeInTheDocument()
114+
})
115+
})
116+
```
117+
118+
To override the an import of a standalone component for your component test, you can use the [`ɵcomponentImports` property](api.mdx#ɵcomponentImports]).
119+
120+
## More examples
121+
59122
More examples can be found in the
60123
[GitHub project](https://github.com/testing-library/angular-testing-library/tree/master/apps/example-app/src/app/examples).
61124
These examples include:
62125

63126
- `@Input` and `@Output` properties
64-
- (Reactive) Forms
65-
- Integration with NgRx (mock) Store
66-
- And
67-
[more](https://github.com/testing-library/angular-testing-library/tree/master/apps/example-app/src/app/examples)
127+
- Forms
128+
- Integration with services
129+
- And [more](https://github.com/testing-library/angular-testing-library/tree/master/apps/example-app/src/app/examples)
68130

69131
If you're looking for an example that isn't on the list, please feel free to
70-
create a
71-
[new issue](https://github.com/testing-library/angular-testing-library/issues/new).
132+
create a [new issue](https://github.com/testing-library/angular-testing-library/issues/new).

0 commit comments

Comments
 (0)