1- import { render , screen } from '@testing-library/angular' ;
1+ import { render , screen , within } from '@testing-library/angular' ;
22import { SignalInputComponent } from './22-signal-inputs.component' ;
33import userEvent from '@testing-library/user-event' ;
44
@@ -10,7 +10,20 @@ test('works with signal inputs', async () => {
1010 } ,
1111 } ) ;
1212
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 ( ) ;
1427} ) ;
1528
1629test ( 'can update signal inputs' , async ( ) => {
@@ -21,11 +34,16 @@ test('can update signal inputs', async () => {
2134 } ,
2235 } ) ;
2336
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 ( ) ;
2541
2642 fixture . componentInstance . name . set ( 'updated' ) ;
2743 // 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+
2947 // it's not recommended to access the model directly, but it's possible
3048 expect ( fixture . componentInstance . name ( ) ) . toBe ( 'updated' ) ;
3149} ) ;
@@ -55,30 +73,41 @@ test('model update also updates the template', async () => {
5573 } ,
5674 } ) ;
5775
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 ( ) ;
5981
6082 await userEvent . clear ( screen . getByRole ( 'textbox' ) ) ;
6183 await userEvent . type ( screen . getByRole ( 'textbox' ) , 'updated' ) ;
6284
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 ( ) ;
6487 expect ( fixture . componentInstance . name ( ) ) . toBe ( 'updated' ) ;
6588
6689 fixture . componentInstance . name . set ( 'new value' ) ;
6790 // 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+
6994 // it's not recommended to access the model directly, but it's possible
7095 expect ( fixture . componentInstance . name ( ) ) . toBe ( 'new value' ) ;
7196} ) ;
7297
73- test ( 'works with signal inputs and rerenders' , async ( ) => {
98+ test ( 'works with signal inputs, computed values, and rerenders' , async ( ) => {
7499 const view = await render ( SignalInputComponent , {
75100 componentInputs : {
76101 greeting : 'Hello' ,
77102 name : 'world' ,
78103 } ,
79104 } ) ;
80105
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 ( ) ;
82111
83112 await view . rerender ( {
84113 componentInputs : {
@@ -87,5 +116,6 @@ test('works with signal inputs and rerenders', async () => {
87116 } ,
88117 } ) ;
89118
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 ( ) ;
91121} ) ;
0 commit comments