@@ -3,126 +3,84 @@ id: intro
3
3
title : Angular Testing Library
4
4
---
5
5
6
- [ ` @angular-extensions/testing-library ` ] [ gh ] is an [ Angular] [ angular ] adapter
7
- around ` dom-testing-library ` .
6
+ 🦔 [ ` @angular-extensions/testing-library ` ] [ gh ] Simple and complete
7
+ [ Angular] ( https://angular.io ) testing utilities that encourage good testing
8
+ practices.
8
9
9
10
``` bash
10
- npm install --save-dev @ angular-extensions/testing-library
11
+ npm install --save-dev angular-extensions/testing-library
11
12
```
12
13
13
- - [ @angular-extensions/testing-library on GitHub] [ gh ]
14
+ - [ ` @angular-extensions/testing-library ` on GitHub] [ gh ]
14
15
15
16
## Usage
16
17
17
- Use the ` createComponent ` function to create the component and the Angular
18
- TestBed.
18
+ counter.component.ts
19
19
20
- After this, you can use all of ` dom-testing-library ` 's ` getBy ` , ` getAllBy ` ,
21
- ` queryBy ` and ` queryAllBy ` queries. See
22
- [ here] ( dom-testing-library/api-queries.md ) for more info.
23
-
24
- Besides the ` dom-testing-library ` 's queries, all of the events (e.g. ` click ` ,
25
- ` input ` , ...) are also exposed. After every event, this library will run the
26
- Angular change detection. See [ here] ( dom-testing-library/api-events.md ) for more
27
- info.
20
+ ``` typescript
21
+ @Component ({
22
+ selector: ' counter' ,
23
+ template: `
24
+ <button (click)="decrement()">-</button>
25
+ <span data-testid="count">Current Count: {{ counter }}</span>
26
+ <button (click)="increment()">+</button>
27
+ ` ,
28
+ })
29
+ export class CounterComponent {
30
+ @Input () counter = 0
28
31
29
- ## Examples
32
+ increment() {
33
+ this .counter += 1
34
+ }
30
35
31
- > ** Note**
32
- >
33
- > There are two different ways to create a component, in both ways it's still
34
- > required to provide the Angular TestBed.
36
+ decrement() {
37
+ this .counter -= 1
38
+ }
39
+ }
40
+ ```
35
41
36
- One way to create the component is via the component's selector:
42
+ counter. component.spec.ts
37
43
38
44
``` typescript
39
- test (' a user can login' , async () => {
40
- const {
41
- container,
42
- getByLabelText,
43
- getByText,
44
- input,
45
- submit,
46
- } = await createComponent <LoginFormComponent >(' <login-form></login-form>' , {
47
- declarations: [LoginFormComponent ],
48
- imports: [ReactiveFormsModule ],
49
- })
45
+ import { render } from ' @angular-extensions/testing-library'
46
+ import CounterComponent from ' ./counter.component.ts'
50
47
51
- const usernameNode = getByLabelText (/ username/ i )
52
- const passwordNode = getByLabelText (/ password/ i )
53
- const submitButtonNode = getByText (/ submit/ i )
54
- const formNode = container .querySelector (' form' )
48
+ describe (' Counter' , () => {
49
+ test (' should render counter' , async () => {
50
+ const { getByText } = await render (CounterComponent , {
51
+ componentProperties: { counter: 5 },
52
+ })
55
53
56
- const fakeUser = { username: ' jackiechan' , password: ' supersecurepassword' }
57
-
58
- input (usernameNode , {
59
- target: {
60
- value: fakeUser .username ,
61
- },
54
+ expect (getByText (' Current Count: 5' ))
62
55
})
63
56
64
- passwordNode .value = fakeUser .password
65
- input (passwordNode )
57
+ test (' should increment the counter on click' , async () => {
58
+ const { getByText, click } = await render (CounterComponent , {
59
+ componentProperties: { counter: 5 },
60
+ })
61
+
62
+ click (getByText (' +' ))
66
63
67
- submit (formNode )
64
+ expect (getByText (' Current Count: 6' ))
65
+ })
68
66
})
69
67
```
70
68
71
- Another way to create the component is via the ` object ` syntax. The only
72
- difference is the setup of the component, the assertions remain the same. This
73
- can come in handy in order to provide more complex parameters or to use a spy to
74
- verify output events.
75
-
76
- ``` typescript
77
- test (' a user can login' , async () => {
78
- const login = {
79
- emit: jest .fn (),
80
- }
69
+ ## API
81
70
82
- const {
83
- container,
84
- getByLabelText,
85
- getByText,
86
- input,
87
- submit,
88
- } = await createComponent (
89
- {
90
- component: LoginFormComponent ,
91
- parameters: {
92
- login ,
93
- },
94
- },
95
- {
96
- declarations: [LoginFormComponent ],
97
- imports: [ReactiveFormsModule ],
98
- }
99
- )
100
-
101
- const usernameNode = getByLabelText (/ username/ i )
102
- const passwordNode = getByLabelText (/ password/ i )
103
- const submitButtonNode = getByText (/ submit/ i )
104
- const formNode = container .querySelector (' form' )
105
-
106
- const fakeUser = { username: ' jackiechan' , password: ' supersecurepassword' }
107
-
108
- input (usernameNode , {
109
- target: {
110
- value: fakeUser .username ,
111
- },
112
- })
71
+ There is a small difference between ` @angular-extensions/testing-library ` and
72
+ the ` testing-library ` family, in this library we also expose all of the events
73
+ via the ` render ` function. This is done to trigger Angular's change detection
74
+ after each interaction.
113
75
114
- passwordNode . value = fakeUser . password
115
- input ( passwordNode )
76
+ You can also import these event via ` @angular-extensions/testing-library ` , but
77
+ the Angular's change detection will not be triggered automatically.
116
78
117
- submit (formNode )
79
+ The same counts for all the queries provided by ` dom-testing-library ` , these are
80
+ also re-exported and can be imported via ` @angular-extensions/testing-library ` .
118
81
119
- expect ( handleLogin . emit ). toHaveBeenCalledWith ( fakeUser )
120
- })
82
+ ``` typescript
83
+ import { getByText , click } from ' @angular-extensions/testing-library '
121
84
```
122
85
123
- Inside the project
124
- [ tests folder] ( https://github.com/angular-extensions/testing-library/tree/master/projects/testing-library/tests )
125
- you can find some more simple tests.
126
-
127
- [ gh ] : https://github.com/angular-extensions/testing-library
128
- [ angular ] : https://angular.io/
86
+ [ gh ] : https://github.com/testing-library/angular-testing-library
0 commit comments