Skip to content

Commit 44dc2d4

Browse files
docs(angular): update docs to latest API changes (#295)
1 parent d9deb10 commit 44dc2d4

File tree

2 files changed

+84
-20
lines changed

2 files changed

+84
-20
lines changed

docs/angular-testing-library/api.md

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ well as these methods:
1212
## `render`
1313

1414
```typescript
15-
function render<T>(
16-
component: Type<T>,
17-
renderOptions?: RenderOptions<T>
18-
): Promise<RenderResult>
19-
function render<T>(
20-
template: string,
21-
renderOptions: RenderOptions<T>
22-
): Promise<RenderResult>
15+
async function render<ComponentType>(
16+
component: Type<ComponentType>,
17+
renderOptions?: RenderComponentOptions<ComponentType>
18+
): Promise<RenderResult<ComponentType, ComponentType>>
19+
async function render<DirectiveType, WrapperType = WrapperComponent>(
20+
component: Type<DirectiveType>,
21+
renderOptions?: RenderDirectiveOptions<DirectiveType, WrapperType>
22+
): Promise<RenderResult<DirectiveType, WrapperType>>
2323
```
2424

25-
## RenderOptions
25+
## Component RenderOptions
2626

2727
### `detectChanges`
2828

@@ -146,34 +146,79 @@ const component = await render(AppComponent, {
146146
})
147147
```
148148

149-
### `wrapper`
149+
### `excludeComponentDeclaration`
150150

151-
An Angular component to wrap the component in.
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.
152153

153-
**default** : `WrapperComponent`, an empty component that strips the
154-
`ng-version` attribute.
154+
**default** : `false`
155155

156156
**example**:
157157

158158
```typescript
159159
const component = await render(AppComponent, {
160-
wrapper: CustomWrapperComponent,
160+
imports: [AppModule], // a module that includes AppComponent
161+
excludeComponentDeclaration: true,
161162
})
162163
```
163164

164-
### `excludeComponentDeclaration`
165+
### `routes`
165166

166-
Exclude the component to be automatically be added as a declaration. This is
167-
needed when the component is declared in an imported module.
167+
The route configuration to set up the router service via
168+
`RouterTestingModule.withRoutes`. For more info see the
169+
[Angular Routes docs](https://angular.io/api/router/Routes).
168170

169-
**default** : `false`
171+
**default** : `[]`
170172

171173
**example**:
172174

173175
```typescript
174176
const component = await render(AppComponent, {
175-
imports: [AppModule], // a module that includes AppComponent
176-
excludeComponentDeclaration: true,
177+
declarations: [ChildComponent],
178+
routes: [
179+
{
180+
path: '',
181+
children: [
182+
{
183+
path: 'child/:id',
184+
component: ChildComponent,
185+
},
186+
],
187+
},
188+
],
189+
})
190+
```
191+
192+
## Directive RenderOptions
193+
194+
To test a directive, the render API is a bit different. The API has the same
195+
options as the Component RenderOptions, but has more options:
196+
197+
### `template`
198+
199+
The template to render the directive.
200+
201+
**example**:
202+
203+
```typescript
204+
const component = await render(SpoilerDirective, {
205+
template: `<div spoiler message='SPOILER'></div>`,
206+
})
207+
```
208+
209+
### `wrapper`
210+
211+
An Angular component to wrap the directive in.
212+
213+
**default**: `WrapperComponent` , an empty component that strips the
214+
`ng-version` attribute.
215+
216+
**example**:
217+
218+
```typescript
219+
const component = await render(SpoilerDirective, {
220+
template: `<div spoiler message='SPOILER'></div>`
221+
wrapper: CustomWrapperComponent
177222
})
178223
```
179224

@@ -246,6 +291,21 @@ component.selectOptions(component.getByLabelText('Fruit'), 'Blueberry')
246291
component.selectOptions(component.getByLabelText('Fruit'), ['Blueberry'. 'Grape'])
247292
```
248293

294+
### `navigate`
295+
296+
Accepts a DOM element or a path as parameter. If an element is passed,
297+
`navigate` will navigate to the `href` value of the element. If a path is
298+
passed, `navigate` will navigate to the path.
299+
300+
```typescript
301+
const component = await render(AppComponent, {
302+
routes: [...]
303+
})
304+
305+
await component.navigate(component.getByLabelText('To details'))
306+
await component.navigate('details/3')
307+
```
308+
249309
### `fixture`
250310

251311
The Angular `ComponentFixture` of the component.

docs/angular-testing-library/examples.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +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
10+
711
counter.component.ts
812

913
```typescript

0 commit comments

Comments
 (0)