@@ -5,12 +5,9 @@ sidebar_label: API
5
5
---
6
6
7
7
` Angular Testing Library ` re-exports everything from ` DOM Testing Library ` as
8
- well as these methods:
8
+ well as the ` render ` method.
9
9
10
- - [ ` render ` ] ( #render )
11
-
12
- Some of the ` DOM Testing Library ` re-exports are patched to work easier with
13
- Angular:
10
+ The following re-exports are patched to make them easier to use with Angular:
14
11
15
12
- The events on ` fireEvent ` automatically invoke a change detection cycle after
16
13
the event has been fired
@@ -21,15 +18,61 @@ Angular:
21
18
22
19
## ` render `
23
20
21
+ With Angular Testing Library, the component can be rendered in two ways, via the
22
+ component's type or with a template.
23
+
24
+ > By default, ` render ` also imports the ` NoopAnimationsModule ` .
25
+
26
+ ## ` Type `
27
+
28
+ To render a component, you need to pass component's type to the ` render ` method.
29
+ For components that don't use other parts of your application (for example
30
+ design modules or services), rendering a component can be as simple as the
31
+ following example.
32
+
33
+ ``` typescript
34
+ await render (AppComponent )
35
+ ```
36
+
37
+ ## ` template `
38
+
39
+ Instead of passing the component's type as first argument, you can also provide
40
+ a template. This practice is required to render directives but can also be
41
+ applied to components, it might even be more useful. The directive's (or
42
+ component's) type must then be added to the ` declarations ` .
43
+
44
+ ** example with directive** :
45
+
46
+ ``` typescript
47
+ await render (' <div appSpoiler></div>' , {
48
+ declarations: [SpoilerDirective ],
49
+ })
50
+ ```
51
+
52
+ ** example with component** :
53
+
54
+ ``` typescript
55
+ await render (
56
+ ' <app-component [value]="47" [otherValue]="anotherValue" (sendValue)="sendValue($event)"></app-component>' ,
57
+ {
58
+ declarations: [AppComponent ],
59
+ componentProperties: {
60
+ anotherValue: ' valueOfAnotherProperty' ,
61
+ sendValue: jest .fn (),
62
+ },
63
+ },
64
+ )
65
+ ```
66
+
24
67
``` typescript
25
- async function render<ComponentType >(
68
+ export async function render<ComponentType >(
26
69
component : Type <ComponentType >,
27
70
renderOptions ? : RenderComponentOptions <ComponentType >,
28
71
): Promise <RenderResult <ComponentType , ComponentType >>
29
- async function render<DirectiveType , WrapperType = WrapperComponent >(
30
- component : Type < DirectiveType > ,
31
- renderOptions ? : RenderDirectiveOptions < DirectiveType , WrapperType >,
32
- ): Promise <RenderResult <DirectiveType , WrapperType >>
72
+ export async function render<WrapperType = WrapperComponent >(
73
+ template : string ,
74
+ renderOptions ? : RenderTemplateOptions < WrapperType >,
75
+ ): Promise <RenderResult <WrapperType >>
33
76
```
34
77
35
78
## Component RenderOptions
@@ -105,7 +148,8 @@ await render(AppComponent, {detectChanges: false})
105
148
### ` excludeComponentDeclaration `
106
149
107
150
Exclude the component to be automatically be added as a declaration . This is
108
- needed when the component is declared in an imported module .
151
+ needed when the component is declared in an imported module , for example with
152
+ SCAMs .
109
153
110
154
** default ** : ` false `
111
155
@@ -238,39 +282,6 @@ await render(AppComponent, {
238
282
})
239
283
` ` `
240
284
241
- ## Directive RenderOptions
242
-
243
- To test a directive , the render API is a bit different . The API has the same
244
- options as the Component RenderOptions , but has more options :
245
-
246
- ### ` template `
247
-
248
- The template to render the directive .
249
-
250
- ** example ** :
251
-
252
- ` ` ` typescript
253
- await render(SpoilerDirective, {
254
- template: ` < div spoiler message = ' SPOILER' > </div >` ,
255
- })
256
- ` ` `
257
-
258
- ### ` wrapper `
259
-
260
- An Angular component to wrap the directive in .
261
-
262
- ** default ** : ` WrapperComponent ` , an empty component that strips the
263
- ` ng-version ` attribute .
264
-
265
- ** example ** :
266
-
267
- ` ` ` typescript
268
- await render(SpoilerDirective, {
269
- template: ` < div spoiler message = ' SPOILER' > </div >`
270
- wrapper: CustomWrapperComponent
271
- })
272
- ` ` `
273
-
274
285
## ` RenderResult `
275
286
276
287
### ` container `
@@ -290,19 +301,46 @@ const {debug} = await render(AppComponent)
290
301
debug()
291
302
` ` `
292
303
304
+ ### ` change `
305
+
306
+ Change the input of the component . This calls ` detectChanges ` after the props
307
+ are updated .
308
+
309
+ ` ` ` typescript
310
+ const {change} = await render(Counter, {
311
+ componentProperties: {count: 4, name: 'Sarah'},
312
+ })
313
+
314
+ expect(screen.getByTestId('count-value').textContent).toBe('4')
315
+ expect(screen.getByTestId('name-value').textContent).toBe('Sarah')
316
+
317
+ change({count: 7})
318
+
319
+ // count updated to 7
320
+ expect(screen.getByTestId('count-value').textContent).toBe('7')
321
+ // name keeps the same value
322
+ expect(screen.getByTestId('name-value').textContent).toBe('Sarah')
323
+ ` ` `
324
+
293
325
### ` rerender `
294
326
295
- Re - render the same component with different props . Will call ` detectChanges `
296
- after props has been updated .
327
+ Re - render the same component with different props . Input properties that are not
328
+ defined are cleared . This calls ` detectChanges ` after the props are updated .
297
329
298
330
` ` ` typescript
299
- const {rerender} = await render(Counter, {componentProperties: {count: 4}})
331
+ const {rerender} = await render(Counter, {
332
+ componentProperties: {count: 4, name: 'Sarah'},
333
+ })
300
334
301
335
expect(screen.getByTestId('count-value').textContent).toBe('4')
336
+ expect(screen.getByTestId('name-value').textContent).toBe('Sarah')
302
337
303
338
rerender({count: 7})
304
339
340
+ // count updated to 7
305
341
expect(screen.getByTestId('count-value').textContent).toBe('7')
342
+ // name is undefined because it's not provided in rerender
343
+ expect(screen.getByTestId('name-value').textContent).toBeUndefined()
306
344
` ` `
307
345
308
346
### ` detectChanges `
@@ -328,7 +366,8 @@ For more info see the
328
366
` ` ` typescript
329
367
const {fixture} = await render(AppComponent)
330
368
331
- const componentInstance = fixture.componentInstance as AppComponent
369
+ // componentInstance is typed as AppComponent
370
+ const componentInstance = fixture.componentInstance
332
371
` ` `
333
372
334
373
> 🚨 If you find yourself using ` fixture ` to access the component ' s internal
@@ -363,6 +402,8 @@ See [Queries](queries/about.mdx) for a complete list.
363
402
` ` ` typescript
364
403
const {getByText, queryByLabelText} = await render(AppComponent)
365
404
366
- getByText('Hello world')
367
- queryByLabelText('First name:')
405
+ screen.getByRole('heading', {
406
+ name: /api/i,
407
+ })
408
+ queryByLabelText(/First name/i')
368
409
` ` `
0 commit comments