Skip to content

Commit ddf60f7

Browse files
docs(angular): Update docs to reflect v11 of ATL (#990)
1 parent a23728b commit ddf60f7

File tree

2 files changed

+99
-56
lines changed

2 files changed

+99
-56
lines changed

docs/angular-testing-library/api.mdx

Lines changed: 91 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ sidebar_label: API
55
---
66

77
`Angular Testing Library` re-exports everything from `DOM Testing Library` as
8-
well as these methods:
8+
well as the `render` method.
99

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:
1411

1512
- The events on `fireEvent` automatically invoke a change detection cycle after
1613
the event has been fired
@@ -21,15 +18,61 @@ Angular:
2118

2219
## `render`
2320

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+
2467
```typescript
25-
async function render<ComponentType>(
68+
export async function render<ComponentType>(
2669
component: Type<ComponentType>,
2770
renderOptions?: RenderComponentOptions<ComponentType>,
2871
): 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>>
3376
```
3477

3578
## Component RenderOptions
@@ -105,7 +148,8 @@ await render(AppComponent, {detectChanges: false})
105148
### `excludeComponentDeclaration`
106149

107150
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.
109153

110154
**default** : `false`
111155

@@ -238,39 +282,6 @@ await render(AppComponent, {
238282
})
239283
```
240284

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-
274285
## `RenderResult`
275286

276287
### `container`
@@ -290,19 +301,46 @@ const {debug} = await render(AppComponent)
290301
debug()
291302
```
292303

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+
293325
### `rerender`
294326

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.
297329

298330
```typescript
299-
const {rerender} = await render(Counter, {componentProperties: {count: 4}})
331+
const {rerender} = await render(Counter, {
332+
componentProperties: {count: 4, name: 'Sarah'},
333+
})
300334
301335
expect(screen.getByTestId('count-value').textContent).toBe('4')
336+
expect(screen.getByTestId('name-value').textContent).toBe('Sarah')
302337
303338
rerender({count: 7})
304339
340+
// count updated to 7
305341
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()
306344
```
307345

308346
### `detectChanges`
@@ -328,7 +366,8 @@ For more info see the
328366
```typescript
329367
const {fixture} = await render(AppComponent)
330368
331-
const componentInstance = fixture.componentInstance as AppComponent
369+
// componentInstance is typed as AppComponent
370+
const componentInstance = fixture.componentInstance
332371
```
333372

334373
> 🚨 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.
363402
```typescript
364403
const {getByText, queryByLabelText} = await render(AppComponent)
365404
366-
getByText('Hello world')
367-
queryByLabelText('First name:')
405+
screen.getByRole('heading', {
406+
name: /api/i,
407+
})
408+
queryByLabelText(/First name/i')
368409
```

docs/angular-testing-library/examples.mdx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ title: Examples
44
sidebar_label: Examples
55
---
66

7-
> Read
8-
> [Good testing practices with 🦔 Angular Testing Library](https://timdeschryver.dev/posts/good-testing-practices-with-angular-testing-library)
9-
> for a guided example
7+
> Read about
8+
> [best practices](https://timdeschryver.dev/blog/good-testing-practices-with-angular-testing-library),
9+
> or follow the
10+
> [guided example](https://timdeschryver.dev/blog/getting-the-most-value-out-of-your-angular-component-tests)
1011
1112
counter.component.ts
1213

@@ -44,7 +45,7 @@ describe('Counter', () => {
4445
componentProperties: {counter: 5},
4546
})
4647

47-
expect(screen.getByText('Current Count: 5'))
48+
expect(screen.getByText('Current Count: 5')).toBeInTheDocument()
4849
})
4950

5051
test('should increment the counter on click', async () => {
@@ -54,7 +55,7 @@ describe('Counter', () => {
5455

5556
fireEvent.click(screen.getByText('+'))
5657

57-
expect(screen.getByText('Current Count: 6'))
58+
expect(screen.getByText('Current Count: 6')).toBeInTheDocument()
5859
})
5960
})
6061
```
@@ -66,7 +67,8 @@ These examples include:
6667
- `@Input` and `@Output` properties
6768
- (Reactive) Forms
6869
- Integration with NgRx (mock) Store
69-
- And more
70+
- And
71+
[more](https://github.com/testing-library/angular-testing-library/tree/master/apps/example-app/src/app/examples)
7072

7173
If you're looking for an example that isn't on the list, please feel free to
7274
create a

0 commit comments

Comments
 (0)