From 0bcc0fc0b8e31b5ac0190f46e52556c5118d4dae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rcio=20Gabriel?= Date: Sun, 27 Mar 2022 12:11:57 +0200 Subject: [PATCH 1/2] docs(angular): add faq with shallow rendering example --- .all-contributorsrc | 9 +++ docs/angular-testing-library/faq.mdx | 107 +++++++++++++++++++++++++++ sidebars.js | 1 + 3 files changed, 117 insertions(+) create mode 100644 docs/angular-testing-library/faq.mdx diff --git a/.all-contributorsrc b/.all-contributorsrc index 719c1237f..4f6aabf20 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -1817,6 +1817,15 @@ "contributions": [ "doc" ] + }, + { + "login": "marcioggs", + "name": "Márcio Gabriel", + "avatar_url": "https://avatars.githubusercontent.com/u/2506127?v=4", + "profile": "https://github.com/marcioggs", + "contributions": [ + "doc" + ] } ], "contributorsPerLine": 7, diff --git a/docs/angular-testing-library/faq.mdx b/docs/angular-testing-library/faq.mdx new file mode 100644 index 000000000..14284a986 --- /dev/null +++ b/docs/angular-testing-library/faq.mdx @@ -0,0 +1,107 @@ +--- +id: faq +title: FAQ +--- + +See also the [main FAQ](dom-testing-library/faq.mdx) for questions not specific +to Angular testing. + +
+ +Can I write unit tests with this library? + +Definitely yes! You can write unit and integration tests with this library. See +below for more on how to mock dependencies (because this library intentionally +does NOT support shallow rendering) if you want to unit test a high level +component. The tests in this project show several examples of unit testing with +this library. + +As you write your tests, keep in mind: + +> The more your tests resemble the way your software is used, the more +> confidence they can give you. - [17 Feb 2018][guiding-principle] + +
+ +
+ + + If I can't use shallow rendering, how do I mock out components in tests? + + +In general, you should avoid mocking out components (see +[the Guiding Principles section](guiding-principles.mdx)). However, if you need +to, then try to use [ng-mocks](https://ng-mocks.sudo.eu/). + +```typescript +import {Component, NgModule} from '@angular/core' +import {render, screen} from '@testing-library/angular' +import {ngMocks} from 'ng-mocks' + +@Component({ + selector: 'app-parent-component', + template: '', +}) +class ParentComponent {} + +@Component({ + selector: 'app-child-component', + template: '

Child component

', +}) +class ChildComponent {} + +@NgModule({ + declarations: [ParentComponent, ChildComponent], +}) +export class AppModule {} + +describe('ParentComponent', () => { + it('should not render ChildComponent when shallow rendering', async () => { + const dependencies = ngMocks.guts(null, AppModule, ParentComponent) + + await render(ParentComponent, dependencies) + + expect(screen.queryByText('Child component')).toBeNull() + }) +}) +``` + +
+ +
+ + + What level of a component tree should I test? Children, parents, or both? + + +Following the guiding principle of this library, it is useful to break down how +tests are organized around how the user experiences and interacts with +application functionality rather than around specific components themselves. In +some cases, for example for reusable component libraries, it might be useful to +include developers in the list of users to test for and test each of the +reusable components individually. Other times, the specific break down of a +component tree is just an implementation detail and testing every component +within that tree individually can cause issues (see +https://kentcdodds.com/blog/avoid-the-test-user). + +In practice this means that it is often preferable to test high enough up the +component tree to simulate realistic user interactions. The question of whether +it is worth additionally testing at a higher or lower level on top of this comes +down to a question of tradeoffs and what will provide enough value for the cost +(see https://kentcdodds.com/blog/unit-vs-integration-vs-e2e-tests on more info +on different levels of testing). + +For a more in-depth discussion of this topic see +[this video](https://youtu.be/0qmPdcV-rN8). + +
+ + + + + +[guiding-principle]: https://twitter.com/kentcdodds/status/977018512689455106 + + diff --git a/sidebars.js b/sidebars.js index 1b65be862..1aecc0e0d 100755 --- a/sidebars.js +++ b/sidebars.js @@ -144,6 +144,7 @@ module.exports = { 'angular-testing-library/intro', 'angular-testing-library/examples', 'angular-testing-library/api', + 'angular-testing-library/faq', ], }, { From 1af9d9cef11e829a7c4348f8c9fd3932f8ffbb67 Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Sat, 9 Apr 2022 09:46:30 +0200 Subject: [PATCH 2/2] Update .all-contributorsrc --- .all-contributorsrc | 9 --------- 1 file changed, 9 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index bbc5eee99..b6ecd3408 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -1835,15 +1835,6 @@ "contributions": [ "doc" ] - }, - { - "login": "marcioggs", - "name": "Márcio Gabriel", - "avatar_url": "https://avatars.githubusercontent.com/u/2506127?v=4", - "profile": "https://github.com/marcioggs", - "contributions": [ - "doc" - ] } ], "contributorsPerLine": 7,