Skip to content

Commit b6d87c8

Browse files
authored
docs(angular): add faq with shallow rendering example (#1032)
1 parent bec11dc commit b6d87c8

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

docs/angular-testing-library/faq.mdx

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
id: faq
3+
title: FAQ
4+
---
5+
6+
See also the [main FAQ](dom-testing-library/faq.mdx) for questions not specific
7+
to Angular testing.
8+
9+
<details>
10+
11+
<summary>Can I write unit tests with this library?</summary>
12+
13+
Definitely yes! You can write unit and integration tests with this library. See
14+
below for more on how to mock dependencies (because this library intentionally
15+
does NOT support shallow rendering) if you want to unit test a high level
16+
component. The tests in this project show several examples of unit testing with
17+
this library.
18+
19+
As you write your tests, keep in mind:
20+
21+
> The more your tests resemble the way your software is used, the more
22+
> confidence they can give you. - [17 Feb 2018][guiding-principle]
23+
24+
</details>
25+
26+
<details>
27+
28+
<summary>
29+
If I can't use shallow rendering, how do I mock out components in tests?
30+
</summary>
31+
32+
In general, you should avoid mocking out components (see
33+
[the Guiding Principles section](guiding-principles.mdx)). However, if you need
34+
to, then try to use [ng-mocks](https://ng-mocks.sudo.eu/).
35+
36+
```typescript
37+
import {Component, NgModule} from '@angular/core'
38+
import {render, screen} from '@testing-library/angular'
39+
import {ngMocks} from 'ng-mocks'
40+
41+
@Component({
42+
selector: 'app-parent-component',
43+
template: '<app-child-component></app-child-component>',
44+
})
45+
class ParentComponent {}
46+
47+
@Component({
48+
selector: 'app-child-component',
49+
template: '<p>Child component</p>',
50+
})
51+
class ChildComponent {}
52+
53+
@NgModule({
54+
declarations: [ParentComponent, ChildComponent],
55+
})
56+
export class AppModule {}
57+
58+
describe('ParentComponent', () => {
59+
it('should not render ChildComponent when shallow rendering', async () => {
60+
const dependencies = ngMocks.guts(null, AppModule, ParentComponent)
61+
62+
await render(ParentComponent, dependencies)
63+
64+
expect(screen.queryByText('Child component')).toBeNull()
65+
})
66+
})
67+
```
68+
69+
</details>
70+
71+
<details>
72+
73+
<summary>
74+
What level of a component tree should I test? Children, parents, or both?
75+
</summary>
76+
77+
Following the guiding principle of this library, it is useful to break down how
78+
tests are organized around how the user experiences and interacts with
79+
application functionality rather than around specific components themselves. In
80+
some cases, for example for reusable component libraries, it might be useful to
81+
include developers in the list of users to test for and test each of the
82+
reusable components individually. Other times, the specific break down of a
83+
component tree is just an implementation detail and testing every component
84+
within that tree individually can cause issues (see
85+
https://kentcdodds.com/blog/avoid-the-test-user).
86+
87+
In practice this means that it is often preferable to test high enough up the
88+
component tree to simulate realistic user interactions. The question of whether
89+
it is worth additionally testing at a higher or lower level on top of this comes
90+
down to a question of tradeoffs and what will provide enough value for the cost
91+
(see https://kentcdodds.com/blog/unit-vs-integration-vs-e2e-tests on more info
92+
on different levels of testing).
93+
94+
For a more in-depth discussion of this topic see
95+
[this video](https://youtu.be/0qmPdcV-rN8).
96+
97+
</details>
98+
99+
<!--
100+
Links:
101+
-->
102+
103+
<!-- prettier-ignore-start -->
104+
105+
[guiding-principle]: https://twitter.com/kentcdodds/status/977018512689455106
106+
107+
<!-- prettier-ignore-end -->

sidebars.js

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ module.exports = {
144144
'angular-testing-library/intro',
145145
'angular-testing-library/examples',
146146
'angular-testing-library/api',
147+
'angular-testing-library/faq',
147148
],
148149
},
149150
{

0 commit comments

Comments
 (0)