Skip to content

Commit 4b80ffe

Browse files
committed
docs: πŸ“ add shallow testing with testing library instructions
1 parent e750863 commit 4b80ffe

File tree

4 files changed

+109
-6
lines changed

4 files changed

+109
-6
lines changed

β€Ždocs/instructions/302-recipe-search-integration/1-test-bed.mdβ€Ž

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ pnpm test
4949
TestBed.configureTestingModule({ providers: [provideHttpClient()] });
5050
```
5151

52-
#### 4. Query DOM and check names are displayed. _Cf. [query DOM with `fixture.debugElement`](#-tip-query-dom-with-fixturedebugelement)_
52+
#### 4. Query DOM and check names are displayed.
53+
54+
Cf. [query DOM with `fixture.debugElement`](#-tip-query-dom-with-fixturedebugelement)
5355

5456
#### 5. [optional] Checkout the implementation if you've opted for TDD option:
5557

@@ -86,7 +88,9 @@ const fake = TestBed.inject(RecipeRepositoryFake);
8688
fake...
8789
```
8890

89-
#### 4. Query DOM and check names are displayed. _Cf. [query DOM with `fixture.debugElement`](#-tip-query-dom-with-fixturedebugelement)_
91+
#### 4. Query DOM and check names are displayed.
92+
93+
Cf. [query DOM with `fixture.debugElement`](#-tip-query-dom-with-fixturedebugelement)
9094

9195
#### 5. [optional] Checkout the implementation if you've opted for TDD option:
9296

β€Ždocs/instructions/302-recipe-search-integration/2-testing-library.mdβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ pnpm test
4949
await render(RecipeSearch, { providers: [provideHttpClient()] });
5050
```
5151

52-
#### 4. Query DOM and check names are displayed. _Cf. [Testing Library queries docs](#-testing-library-queries-docs--or-how-to-choose-the-right-query)_
52+
#### 4. Query DOM and check names are displayed.
53+
54+
Cf. [Testing Library Queries docs](#-testing-library-queries-docs--or-how-to-choose-the-right-query)
5355

5456
#### 5. [optional] Checkout the implementation if you've opted for TDD option:
5557

β€Ždocs/instructions/303-recipe-search-shallow/1-test-bed.mdβ€Ž

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
sidebar_label: 303 - Recipe Search Shallow
2+
sidebar_label: 1 - TestBed
33
---
44

5-
# Recipe Search Shallow
5+
# Recipe Search Shallow with `TestBed`
66

77
## Setup
88

@@ -49,7 +49,10 @@ TestBed.overrideComponent(RecipeSearch, {
4949
});
5050
```
5151

52-
#### 4. Query DOM and check child components properties. (Cf. [query DOM with `fixture.debugElement`](#-tip-query-dom-with-fixturedebugelement)] & [access element properties](#-tip-access-element-properties))
52+
#### 4. Query DOM and check child components properties
53+
54+
- Cf. [Query DOM with `fixture.debugElement`](#-tip-query-dom-with-fixturedebugelement)
55+
- Cf. [Access element properties](#-tip-access-element-properties)
5356

5457
#### 5. [optional] Checkout the implementation if you've opted for TDD option:.
5558

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
sidebar_label: 2 - Testing Library
3+
---
4+
5+
# Recipe Search Shallow with Testing Library
6+
7+
## Setup
8+
9+
```sh
10+
pnpm cook start 303-recipe-search-shallow-testing-library
11+
```
12+
13+
:::info ♻️ TDD option
14+
15+
You can choose to:
16+
17+
- go full-on TDD and implement the tests first then checkout the implementation later,
18+
- or checkout the implementation first and then implement the tests.
19+
20+
:::
21+
22+
## 🎯 Goal: Test `RecipeSearch`
23+
24+
`RecipeSearch` component should fetch recipes using `RecipeRepository` on startup and display them using `RecipePreview` component.
25+
But, this time, let's check that the loaded recipes are forwarded as inputs to children.
26+
27+
**Implement tests** for `RecipeSearch` and make sure that:
28+
29+
- recipes are passed to child components.
30+
31+
### πŸ“ Steps
32+
33+
#### 1. Run tests:
34+
35+
```sh
36+
pnpm test
37+
```
38+
39+
#### 2. Open `src/app/recipe/recipe-search.shallow.spec.ts`.
40+
41+
#### 3. Override component's imports & schema:
42+
43+
```ts
44+
await render(RecipeSearch, {
45+
...
46+
configureTestBed(testBed) {
47+
...
48+
testBed.overrideComponent(RecipeSearch, {
49+
set: {
50+
imports: [],
51+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
52+
},
53+
});
54+
...
55+
},
56+
});
57+
```
58+
59+
#### 4. Query DOM and check child components properties.
60+
61+
- Cf. [Query DOM with `fixture.debugElement`](#-tip-query-dom-with-fixturedebugelement)
62+
- Cf. [Access element properties](#-tip-access-element-properties)
63+
64+
:::tip
65+
Note that `RecipePreview` component host element has a `data-testid="recipe-preview"` attribute that you can query with Testing Library.
66+
:::
67+
68+
#### 5. [optional] Checkout the implementation if you've opted for TDD option:.
69+
70+
```sh
71+
pnpm cook checkout-impl
72+
```
73+
74+
#### 6. βœ… Make sure tests are passing.
75+
76+
## πŸ“– Appendices
77+
78+
### πŸ”— `@testing-library/angular`'s `render` docs
79+
80+
[https://testing-library.com/docs/angular-testing-library/api#render](https://testing-library.com/docs/angular-testing-library/api#render)
81+
82+
### πŸ”— Testing Library Queries docs β€” or how to choose the right query
83+
84+
[https://testing-library.com/docs/queries/about](https://testing-library.com/docs/queries/about)
85+
86+
### 🎁 Tip: Access element properties
87+
88+
You can transform any `HTMLElement` into an Angular `DebugElement` to access the properties forwarded by parent component:
89+
90+
```ts
91+
const element = screen.getBy...;
92+
93+
new DebugElement(element).properties.myInput;
94+
```

0 commit comments

Comments
Β (0)