-
Notifications
You must be signed in to change notification settings - Fork 724
docs(angular): add faq with shallow rendering example #1032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
<details> | ||
timdeschryver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<summary>Can I write unit tests with this library?</summary> | ||
|
||
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] | ||
|
||
</details> | ||
|
||
<details> | ||
|
||
<summary> | ||
If I can't use shallow rendering, how do I mock out components in tests? | ||
</summary> | ||
|
||
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: '<app-child-component></app-child-component>', | ||
}) | ||
class ParentComponent {} | ||
|
||
@Component({ | ||
selector: 'app-child-component', | ||
template: '<p>Child component</p>', | ||
}) | ||
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() | ||
}) | ||
}) | ||
``` | ||
|
||
</details> | ||
|
||
<details> | ||
|
||
<summary> | ||
What level of a component tree should I test? Children, parents, or both? | ||
</summary> | ||
|
||
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). | ||
|
||
</details> | ||
|
||
<!-- | ||
Links: | ||
--> | ||
|
||
<!-- prettier-ignore-start --> | ||
|
||
[guiding-principle]: https://twitter.com/kentcdodds/status/977018512689455106 | ||
|
||
<!-- prettier-ignore-end --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.