@@ -8,126 +8,72 @@ title: Examples
88> or follow the
99> [ guided example] ( https://timdeschryver.dev/blog/getting-the-most-value-out-of-your-angular-component-tests )
1010
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.
6714
6815``` ts title="counter.component.ts"
6916@Component ({
70- selector: ' counter' ,
17+ selector: ' app- counter' ,
7118 template: `
19+ <span>{{ hello() }}</span>
7220 <button (click)="decrement()">-</button>
73- <span data-testid="count" >Current Count: {{ counter }}</span>
21+ <span>Current Count: {{ counter() }}</span>
7422 <button (click)="increment()">+</button>
7523 ` ,
76- standalone: true ,
7724})
7825export class CounterComponent {
79- @Input () counter = 0
26+ counter = model (0 );
27+ hello = input (' Hi' , { alias: ' greeting' });
8028
8129 increment() {
82- this .counter += 1
30+ this .counter . set ( this . counter () + 1 );
8331 }
8432
8533 decrement() {
86- this .counter -= 1
34+ this .counter . set ( this . counter () - 1 );
8735 }
8836}
8937```
9038
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' ;
9742
9843describe (' 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 () => {
10845 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+ });
11766```
11867
119- To override an import of a standalone component for your component test, you can
120- use the [ ` componentImports ` property] ( api.mdx#componentImports ) .
121-
12268## More examples
12369
12470More examples can be found in the
12571[ GitHub project] ( https://github.com/testing-library/angular-testing-library/tree/master/apps/example-app/src/app/examples ) .
12672These examples include:
12773
128- - ` @Input ` and ` @Output ` properties
74+ - ` input ` and ` output ` properties
12975- Forms
130- - Integration with services
76+ - Integration injected services
13177- And
13278 [ more] ( https://github.com/testing-library/angular-testing-library/tree/master/apps/example-app/src/app/examples )
13379
0 commit comments