Skip to content

Commit 577522b

Browse files
timdeschryverKent C. Dodds
authored andcommitted
Update angular api (#341)
- sort functions alphabetically - add docs for new functions added to the Angular Testing Library
1 parent e05111a commit 577522b

File tree

1 file changed

+137
-102
lines changed
  • docs/angular-testing-library

1 file changed

+137
-102
lines changed

docs/angular-testing-library/api.md

Lines changed: 137 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -24,110 +24,108 @@ async function render<DirectiveType, WrapperType = WrapperComponent>(
2424

2525
## Component RenderOptions
2626

27-
### `detectChanges`
27+
### `componentProperties`
2828

29-
Will call `detectChanges` when the component is compiled
29+
An object to set `@Input` and `@Output` properties of the component.
3030

31-
**default** : `true`
31+
**default** : `{}`
3232

3333
**example**:
3434

3535
```typescript
36-
const component = await render(AppComponent, { detectChanges: false })
36+
const component = await render(AppComponent, {
37+
componentProperties: {
38+
counterValue: 10,
39+
send: (value) => { ... }
40+
}
41+
})
3742
```
3843

39-
### `declarations`
44+
### `componentProviders`
4045

41-
A collection of providers needed to render the component via Dependency
42-
Injection, for example, injectable services or tokens.
46+
A collection of providers to inject dependencies of the component.
4347

4448
For more info see the
45-
[Angular docs](https://angular.io/api/core/NgModule#providers).
49+
[Angular docs](https://angular.io/api/core/Directive#providers).
4650

4751
**default** : `[]`
4852

4953
**example**:
5054

5155
```typescript
5256
const component = await render(AppComponent, {
53-
providers: [
54-
CustomersService,
55-
{
56-
provide: MAX_CUSTOMERS_TOKEN,
57-
useValue: 10,
58-
},
59-
],
57+
componentProviders: [AppComponentService],
6058
})
6159
```
6260

63-
### `imports`
61+
### `declarations`
6462

65-
A collection of imports needed to render the component, for example, shared
66-
modules. Adds `NoopAnimationsModule` by default if `BrowserAnimationsModule`
67-
isn't added to the collection
63+
A collection of providers needed to render the component via Dependency
64+
Injection, for example, injectable services or tokens.
6865

6966
For more info see the
70-
[Angular docs](https://angular.io/api/core/NgModule#imports).
67+
[Angular docs](https://angular.io/api/core/NgModule#providers).
7168

72-
**default** : `[NoopAnimationsModule]`
69+
**default** : `[]`
7370

7471
**example**:
7572

7673
```typescript
7774
const component = await render(AppComponent, {
78-
imports: [AppSharedModule, MaterialModule],
75+
providers: [
76+
CustomersService,
77+
{
78+
provide: MAX_CUSTOMERS_TOKEN,
79+
useValue: 10,
80+
},
81+
],
7982
})
8083
```
8184

82-
### `schemas`
83-
84-
A collection of schemas needed to render the component. Allowed values are
85-
`NO_ERRORS_SCHEMA` and `CUSTOM_ELEMENTS_SCHEMA`.
85+
### `detectChanges`
8686

87-
For more info see the
88-
[Angular docs](https://angular.io/api/core/NgModule#schemas).
87+
Will call `detectChanges` when the component is compiled
8988

90-
**default** : `[]`
89+
**default** : `true`
9190

9291
**example**:
9392

9493
```typescript
95-
const component = await render(AppComponent, {
96-
schemas: [NO_ERRORS_SCHEMA],
97-
})
94+
const component = await render(AppComponent, { detectChanges: false })
9895
```
9996

100-
### `componentProperties`
97+
### `excludeComponentDeclaration`
10198

102-
An object to set `@Input` and `@Output` properties of the component.
99+
Exclude the component to be automatically be added as a declaration. This is
100+
needed when the component is declared in an imported module.
103101

104-
**default** : `{}`
102+
**default** : `false`
105103

106104
**example**:
107105

108106
```typescript
109107
const component = await render(AppComponent, {
110-
componentProperties: {
111-
counterValue: 10,
112-
send: (value) => { ... }
113-
}
108+
imports: [AppModule], // a module that includes AppComponent
109+
excludeComponentDeclaration: true,
114110
})
115111
```
116112

117-
### `componentProviders`
113+
### `imports`
118114

119-
A collection of providers to inject dependencies of the component.
115+
A collection of imports needed to render the component, for example, shared
116+
modules. Adds `NoopAnimationsModule` by default if `BrowserAnimationsModule`
117+
isn't added to the collection
120118

121119
For more info see the
122-
[Angular docs](https://angular.io/api/core/Directive#providers).
120+
[Angular docs](https://angular.io/api/core/NgModule#imports).
123121

124-
**default** : `[]`
122+
**default** : `[NoopAnimationsModule]`
125123

126124
**example**:
127125

128126
```typescript
129127
const component = await render(AppComponent, {
130-
componentProviders: [AppComponentService],
128+
imports: [AppSharedModule, MaterialModule],
131129
})
132130
```
133131

@@ -146,22 +144,6 @@ const component = await render(AppComponent, {
146144
})
147145
```
148146

149-
### `excludeComponentDeclaration`
150-
151-
Exclude the component to be automatically be added as a declaration. This is
152-
needed when the component is declared in an imported module.
153-
154-
**default** : `false`
155-
156-
**example**:
157-
158-
```typescript
159-
const component = await render(AppComponent, {
160-
imports: [AppModule], // a module that includes AppComponent
161-
excludeComponentDeclaration: true,
162-
})
163-
```
164-
165147
### `routes`
166148

167149
The route configuration to set up the router service via
@@ -189,6 +171,24 @@ const component = await render(AppComponent, {
189171
})
190172
```
191173

174+
### `schemas`
175+
176+
A collection of schemas needed to render the component. Allowed values are
177+
`NO_ERRORS_SCHEMA` and `CUSTOM_ELEMENTS_SCHEMA`.
178+
179+
For more info see the
180+
[Angular docs](https://angular.io/api/core/NgModule#schemas).
181+
182+
**default** : `[]`
183+
184+
**example**:
185+
186+
```typescript
187+
const component = await render(AppComponent, {
188+
schemas: [NO_ERRORS_SCHEMA],
189+
})
190+
```
191+
192192
## Directive RenderOptions
193193

194194
To test a directive, the render API is a bit different. The API has the same
@@ -224,23 +224,36 @@ const component = await render(SpoilerDirective, {
224224

225225
## `RenderResult`
226226

227-
### `...queries`
227+
### `container`
228228

229-
The most important feature of `render` is that the queries from
230-
[DOM Testing Library](https://testing-library.com/docs/dom-testing-library) are
231-
automatically returned with their first argument bound to the component under
232-
test.
229+
The containing DOM node of your rendered Angular Component. This is a regular
230+
DOM node, so you can call `container.querySelector` etc. to inspect the
231+
children.
233232

234-
See [Queries](https://testing-library.com/docs/dom-testing-library/api-queries)
235-
for a complete list.
233+
### `debug`
236234

237-
**example**:
235+
Prints out the component's DOM with syntax highlighting. Accepts an optional
236+
parameter, to print out a specific DOM node.
238237

239238
```typescript
240239
const component = await render(AppComponent)
241240
242-
component.getByText('Hello world')
243-
component.queryByLabelText('First name:')
241+
component.debug()
242+
component.debug(component.getByRole('alert'))
243+
```
244+
245+
### `rerender`
246+
247+
Re-render the same component with different props.
248+
Will call `detectChanges` after props has been updated.
249+
250+
```typescript
251+
const component = await render(Counter, { componentProperties: { count: 4 }})
252+
253+
expect(component.getByTestId('count-value').textContent).toBe('4')
254+
255+
component.rerender({ count: 7 })
256+
expect(component.getByTestId('count-value').textContent).toBe('7')
244257
```
245258

246259
### `fireEvent.***`
@@ -268,29 +281,23 @@ component.change(component.getByLabelText('Username'), {
268281
component.click(component.getByText('Login'))
269282
```
270283

271-
### `type`
272-
273-
Types a value in an input field with the same interactions as the user would do.
274-
275-
```typescript
276-
const component = await render(AppComponent)
277-
278-
component.type(component.getByLabelText('Username'), 'Tim')
279-
component.type(component.getByLabelText('Username'), 'Tim', { delay: 250 })
280-
```
284+
### `fixture`
281285

282-
### `selectOptions`
286+
The Angular `ComponentFixture` of the component.
283287

284-
Select an option(s) from a select field with the same interactions as the user
285-
would do.
288+
For more info see the
289+
[Angular docs](https://angular.io/api/core/testing/ComponentFixture).
286290

287291
```typescript
288292
const component = await render(AppComponent)
289293
290-
component.selectOptions(component.getByLabelText('Fruit'), 'Blueberry')
291-
component.selectOptions(component.getByLabelText('Fruit'), ['Blueberry'. 'Grape'])
294+
const componentInstance = component.fixture.componentInstance as AppComponent
292295
```
293296

297+
> 🚨 If you find yourself using `fixture` to access the component's internal
298+
> values you should reconsider! This probable means, you're testing
299+
> implementation details.
300+
294301
### `navigate`
295302

296303
Accepts a DOM element or a path as parameter. If an element is passed,
@@ -306,37 +313,65 @@ await component.navigate(component.getByLabelText('To details'))
306313
await component.navigate('details/3')
307314
```
308315

309-
### `fixture`
316+
### `...queries`
310317

311-
The Angular `ComponentFixture` of the component.
318+
The most important feature of `render` is that the queries from
319+
[DOM Testing Library](https://testing-library.com/docs/dom-testing-library) are
320+
automatically returned with their first argument bound to the component under
321+
test.
312322

313-
For more info see the
314-
[Angular docs](https://angular.io/api/core/testing/ComponentFixture).
323+
See [Queries](https://testing-library.com/docs/dom-testing-library/api-queries)
324+
for a complete list.
325+
326+
**example**:
315327

316328
```typescript
317329
const component = await render(AppComponent)
318330
319-
const componentInstance = component.fixture.componentInstance as AppComponent
331+
component.getByText('Hello world')
332+
component.queryByLabelText('First name:')
320333
```
321334

322-
> 🚨 If you find yourself using `fixture` to access the component's internal
323-
> values you should reconsider! This probable means, you're testing
324-
> implementation details.
335+
### `selectOptions`
325336

326-
### `container`
337+
Select an option(s) from a select field with the same interactions as the user
338+
would do.
327339

328-
The containing DOM node of your rendered Angular Component. This is a regular
329-
DOM node, so you can call `container.querySelector` etc. to inspect the
330-
children.
340+
```typescript
341+
const component = await render(AppComponent)
331342
332-
### `debug`
343+
component.selectOptions(component.getByLabelText('Fruit'), 'Blueberry')
344+
component.selectOptions(component.getByLabelText('Fruit'), ['Blueberry'. 'Grape'])
345+
```
333346

334-
Prints out the component's DOM with syntax highlighting. Accepts an optional
335-
parameter, to print out a specific DOM node.
347+
### `type`
348+
349+
Types a value in an input field with the same interactions as the user would do.
336350

337351
```typescript
338352
const component = await render(AppComponent)
339353
340-
component.debug()
341-
component.debug(component.getByRole('alert'))
354+
component.type(component.getByLabelText('Username'), 'Tim')
355+
component.type(component.getByLabelText('Username'), 'Tim', { delay: 250 })
342356
```
357+
358+
### `waitForDomChange`
359+
360+
Wait for the DOM to change.
361+
For more info see the [DOM Testing Library docs](https://testing-library.com/docs/dom-testing-library/api-async#waitfordomchange).
362+
363+
This function will also call `detectChanges` repeatably to update the DOM, is helpful to test async functionality.
364+
365+
### `waitForElement`
366+
367+
Wait for DOM elements to appear, disappear, or change.
368+
For more info see the [DOM Testing Library docs](https://testing-library.com/docs/dom-testing-library/api-async#waitforelement).
369+
370+
This function will also call `detectChanges` repeatably to update the DOM, is helpful to test async functionality.
371+
372+
### `waitForElementToBeRemoved`
373+
374+
Wait for the removal of element(s) from the DOM.
375+
For more info see the [DOM Testing Library docs](https://testing-library.com/docs/dom-testing-library/api-async#waitforelementtoberemoved).
376+
377+
This function will also call `detectChanges` repeatably to update the DOM, is helpful to test async functionality.

0 commit comments

Comments
 (0)