1
- import { render , screen } from '@testing-library/angular' ;
1
+ import { render , screen , within } from '@testing-library/angular' ;
2
2
import { SignalInputComponent } from './22-signal-inputs.component' ;
3
3
import userEvent from '@testing-library/user-event' ;
4
4
@@ -10,7 +10,20 @@ test('works with signal inputs', async () => {
10
10
} ,
11
11
} ) ;
12
12
13
- expect ( screen . getByText ( / h e l l o w o r l d / i) ) . toBeInTheDocument ( ) ;
13
+ const inputValue = within ( screen . getByTestId ( 'input-value' ) ) ;
14
+ expect ( inputValue . getByText ( / h e l l o w o r l d / i) ) . toBeInTheDocument ( ) ;
15
+ } ) ;
16
+
17
+ test ( 'works with computed' , async ( ) => {
18
+ await render ( SignalInputComponent , {
19
+ componentInputs : {
20
+ greeting : 'Hello' ,
21
+ name : 'world' ,
22
+ } ,
23
+ } ) ;
24
+
25
+ const computedValue = within ( screen . getByTestId ( 'computed-value' ) ) ;
26
+ expect ( computedValue . getByText ( / h e l l o w o r l d / i) ) . toBeInTheDocument ( ) ;
14
27
} ) ;
15
28
16
29
test ( 'can update signal inputs' , async ( ) => {
@@ -21,11 +34,16 @@ test('can update signal inputs', async () => {
21
34
} ,
22
35
} ) ;
23
36
24
- expect ( screen . getByText ( / h e l l o w o r l d / i) ) . toBeInTheDocument ( ) ;
37
+ const inputValue = within ( screen . getByTestId ( 'input-value' ) ) ;
38
+ const computedValue = within ( screen . getByTestId ( 'computed-value' ) ) ;
39
+
40
+ expect ( inputValue . getByText ( / h e l l o w o r l d / i) ) . toBeInTheDocument ( ) ;
25
41
26
42
fixture . componentInstance . name . set ( 'updated' ) ;
27
43
// set doesn't trigger change detection within the test, findBy is needed to update the template
28
- expect ( await screen . findByText ( / h e l l o u p d a t e d / i) ) . toBeInTheDocument ( ) ;
44
+ expect ( await inputValue . findByText ( / h e l l o u p d a t e d / i) ) . toBeInTheDocument ( ) ;
45
+ expect ( await computedValue . findByText ( / h e l l o u p d a t e d / i) ) . toBeInTheDocument ( ) ;
46
+
29
47
// it's not recommended to access the model directly, but it's possible
30
48
expect ( fixture . componentInstance . name ( ) ) . toBe ( 'updated' ) ;
31
49
} ) ;
@@ -55,30 +73,41 @@ test('model update also updates the template', async () => {
55
73
} ,
56
74
} ) ;
57
75
58
- expect ( screen . getByText ( / h e l l o i n i t i a l / i) ) . toBeInTheDocument ( ) ;
76
+ const inputValue = within ( screen . getByTestId ( 'input-value' ) ) ;
77
+ const computedValue = within ( screen . getByTestId ( 'computed-value' ) ) ;
78
+
79
+ expect ( inputValue . getByText ( / h e l l o i n i t i a l / i) ) . toBeInTheDocument ( ) ;
80
+ expect ( computedValue . getByText ( / h e l l o i n i t i a l / i) ) . toBeInTheDocument ( ) ;
59
81
60
82
await userEvent . clear ( screen . getByRole ( 'textbox' ) ) ;
61
83
await userEvent . type ( screen . getByRole ( 'textbox' ) , 'updated' ) ;
62
84
63
- expect ( screen . getByText ( / h e l l o u p d a t e d / i) ) . toBeInTheDocument ( ) ;
85
+ expect ( inputValue . getByText ( / h e l l o u p d a t e d / i) ) . toBeInTheDocument ( ) ;
86
+ expect ( computedValue . getByText ( / h e l l o u p d a t e d / i) ) . toBeInTheDocument ( ) ;
64
87
expect ( fixture . componentInstance . name ( ) ) . toBe ( 'updated' ) ;
65
88
66
89
fixture . componentInstance . name . set ( 'new value' ) ;
67
90
// set doesn't trigger change detection within the test, findBy is needed to update the template
68
- expect ( await screen . findByText ( / h e l l o n e w v a l u e / i) ) . toBeInTheDocument ( ) ;
91
+ expect ( await inputValue . findByText ( / h e l l o n e w v a l u e / i) ) . toBeInTheDocument ( ) ;
92
+ expect ( await computedValue . findByText ( / h e l l o n e w v a l u e / i) ) . toBeInTheDocument ( ) ;
93
+
69
94
// it's not recommended to access the model directly, but it's possible
70
95
expect ( fixture . componentInstance . name ( ) ) . toBe ( 'new value' ) ;
71
96
} ) ;
72
97
73
- test ( 'works with signal inputs and rerenders' , async ( ) => {
98
+ test ( 'works with signal inputs, computed values, and rerenders' , async ( ) => {
74
99
const view = await render ( SignalInputComponent , {
75
100
componentInputs : {
76
101
greeting : 'Hello' ,
77
102
name : 'world' ,
78
103
} ,
79
104
} ) ;
80
105
81
- expect ( screen . getByText ( / h e l l o w o r l d / i) ) . toBeInTheDocument ( ) ;
106
+ const inputValue = within ( screen . getByTestId ( 'input-value' ) ) ;
107
+ const computedValue = within ( screen . getByTestId ( 'computed-value' ) ) ;
108
+
109
+ expect ( inputValue . getByText ( / h e l l o w o r l d / i) ) . toBeInTheDocument ( ) ;
110
+ expect ( computedValue . getByText ( / h e l l o w o r l d / i) ) . toBeInTheDocument ( ) ;
82
111
83
112
await view . rerender ( {
84
113
componentInputs : {
@@ -87,5 +116,6 @@ test('works with signal inputs and rerenders', async () => {
87
116
} ,
88
117
} ) ;
89
118
90
- expect ( screen . getByText ( / b y e t e s t / i) ) . toBeInTheDocument ( ) ;
119
+ expect ( inputValue . getByText ( / b y e t e s t / i) ) . toBeInTheDocument ( ) ;
120
+ expect ( computedValue . getByText ( / b y e t e s t / i) ) . toBeInTheDocument ( ) ;
91
121
} ) ;
0 commit comments