@@ -8,126 +8,72 @@ title: Examples
8
8
> or follow the
9
9
> [ guided example] ( https://timdeschryver.dev/blog/getting-the-most-value-out-of-your-angular-component-tests )
10
10
11
- ``` ts title="counter.component.ts"
12
- @Component ({
13
- selector: ' counter' ,
14
- template: `
15
- <button (click)="decrement()">-</button>
16
- <span data-testid="count">Current Count: {{ counter }}</span>
17
- <button (click)="increment()">+</button>
18
- ` ,
19
- })
20
- export class CounterComponent {
21
- @Input () counter = 0
22
-
23
- increment() {
24
- this .counter += 1
25
- }
26
-
27
- decrement() {
28
- this .counter -= 1
29
- }
30
- }
31
- ```
32
-
33
- ``` ts title="counter.component.spec.ts"
34
- import {render , screen , fireEvent } from ' @testing-library/angular'
35
- import {CounterComponent } from ' ./counter.component.ts'
36
-
37
- describe (' Counter' , () => {
38
- test (' should render counter' , async () => {
39
- await render (CounterComponent , {
40
- componentProperties: {counter: 5 },
41
- })
42
-
43
- expect (screen .getByText (' Current Count: 5' )).toBeInTheDocument ()
44
- })
45
-
46
- test (' should increment the counter on click' , async () => {
47
- await render (CounterComponent , {
48
- componentProperties: {counter: 5 },
49
- })
50
-
51
- fireEvent .click (screen .getByText (' +' ))
52
-
53
- expect (screen .getByText (' Current Count: 6' )).toBeInTheDocument ()
54
- })
55
- })
56
- ```
57
-
58
- ## With Standalone Components
59
-
60
- Angular Testing Library can also test your standalone components. In fact, it
61
- even becomes easier because you don't have to set up the whole Angular TestBed.
62
- This was previously only
63
- [ possible when you used Single Component Angular Modules (SCAMs)] ( https://timdeschryver.dev/blog/single-component-angular-modules-and-component-tests-go-hand-in-hand ) .
64
-
65
- Let's migrate the counter component to a standalone component, and let's take a
66
- look at how this affects the test.
11
+ Angular Testing Library can be used with standalone components and also with Angular components that uses Modules.
12
+ The example below shows how to test a standalone component, but the same principles apply to Angular components that uses Modules.
13
+ In fact, there should be no difference in how you test both types of components, only the setup might be different.
67
14
68
15
``` ts title="counter.component.ts"
69
16
@Component ({
70
- selector: ' counter' ,
17
+ selector: ' app- counter' ,
71
18
template: `
19
+ <span>{{ hello() }}</span>
72
20
<button (click)="decrement()">-</button>
73
- <span data-testid="count" >Current Count: {{ counter }}</span>
21
+ <span>Current Count: {{ counter() }}</span>
74
22
<button (click)="increment()">+</button>
75
23
` ,
76
- standalone: true ,
77
24
})
78
25
export class CounterComponent {
79
- @Input () counter = 0
26
+ counter = model (0 );
27
+ hello = input (' Hi' , { alias: ' greeting' });
80
28
81
29
increment() {
82
- this .counter += 1
30
+ this .counter . set ( this . counter () + 1 );
83
31
}
84
32
85
33
decrement() {
86
- this .counter -= 1
34
+ this .counter . set ( this . counter () - 1 );
87
35
}
88
36
}
89
37
```
90
38
91
- Just as you would've expected, this doesn't affect the test cases. Writing tests
92
- for standalone components remains the same as for "regular" components.
93
-
94
- ``` ts title="counter.component.spec.ts"
95
- import {render , screen , fireEvent } from ' @testing-library/angular'
96
- import {CounterComponent } from ' ./counter.component.ts'
39
+ ``` typescript title="counter.component.spec.ts"
40
+ import { render , screen , fireEvent , aliasedInput } from ' @testing-library/angular' ;
41
+ import { CounterComponent } from ' ./counter.component' ;
97
42
98
43
describe (' Counter' , () => {
99
- test (' should render counter' , async () => {
100
- await render (CounterComponent , {
101
- componentProperties: {counter: 5 },
102
- })
103
-
104
- expect (screen .getByText (' Current Count: 5' )).toBeInTheDocument ()
105
- })
106
-
107
- test (' should increment the counter on click' , async () => {
44
+ it (' should render counter' , async () => {
108
45
await render (CounterComponent , {
109
- componentProperties: {counter: 5 },
110
- })
111
-
112
- fireEvent .click (screen .getByText (' +' ))
113
-
114
- expect (screen .getByText (' Current Count: 6' )).toBeInTheDocument ()
115
- })
116
- })
46
+ inputs: {
47
+ counter: 5 ,
48
+ // aliases need to be specified using aliasedInput
49
+ ... aliasedInput (' greeting' , ' Hello Alias!' ),
50
+ },
51
+ });
52
+
53
+ expect (screen .getByText (' Current Count: 5' )).toBeVisible ();
54
+ expect (screen .getByText (' Hello Alias!' )).toBeVisible ();
55
+ });
56
+
57
+ it (' should increment the counter on click' , async () => {
58
+ await render (CounterComponent , { inputs: { counter: 5 } });
59
+
60
+ const incrementButton = screen .getByRole (' button' , { name: ' +' });
61
+ fireEvent .click (incrementButton );
62
+
63
+ expect (screen .getByText (' Current Count: 6' )).toBeVisible ();
64
+ });
65
+ });
117
66
```
118
67
119
- To override an import of a standalone component for your component test, you can
120
- use the [ ` componentImports ` property] ( api.mdx#componentImports ) .
121
-
122
68
## More examples
123
69
124
70
More examples can be found in the
125
71
[ GitHub project] ( https://github.com/testing-library/angular-testing-library/tree/master/apps/example-app/src/app/examples ) .
126
72
These examples include:
127
73
128
- - ` @Input ` and ` @Output ` properties
74
+ - ` input ` and ` output ` properties
129
75
- Forms
130
- - Integration with services
76
+ - Integration injected services
131
77
- And
132
78
[ more] ( https://github.com/testing-library/angular-testing-library/tree/master/apps/example-app/src/app/examples )
133
79
0 commit comments