@@ -13,22 +13,30 @@ test('is possible to fill in a form and verify error messages (with the help of
13
13
const scoreControl = screen . getByRole ( 'spinbutton' , { name : / s c o r e / i } ) ;
14
14
const colorControl = screen . getByPlaceholderText ( / c o l o r / i) ;
15
15
const dateControl = screen . getByRole ( 'textbox' , { name : / C h o o s e a d a t e / i } ) ;
16
+ const checkboxControl = screen . getByRole ( 'checkbox' , { name : / a g r e e / i } ) ;
16
17
17
18
const errors = screen . getByRole ( 'alert' ) ;
18
19
19
20
expect ( errors ) . toContainElement ( screen . queryByText ( 'name is required' ) ) ;
20
21
expect ( errors ) . toContainElement ( screen . queryByText ( 'score must be greater than 1' ) ) ;
21
22
expect ( errors ) . toContainElement ( screen . queryByText ( 'color is required' ) ) ;
23
+ expect ( errors ) . toContainElement ( screen . queryByText ( 'agree is required' ) ) ;
22
24
23
25
userEvent . type ( nameControl , 'Tim' ) ;
24
26
userEvent . clear ( scoreControl ) ;
25
27
userEvent . type ( scoreControl , '12' ) ;
26
28
userEvent . click ( colorControl ) ;
27
29
userEvent . click ( screen . getByText ( / g r e e n / i) ) ;
28
30
31
+ expect ( checkboxControl ) . not . toBeChecked ( ) ;
32
+ userEvent . click ( checkboxControl ) ;
33
+ expect ( checkboxControl ) . toBeChecked ( ) ;
34
+ expect ( checkboxControl ) . toBeValid ( ) ;
35
+
29
36
expect ( screen . queryByText ( 'name is required' ) ) . not . toBeInTheDocument ( ) ;
30
37
expect ( screen . getByText ( 'score must be lesser than 10' ) ) . toBeInTheDocument ( ) ;
31
38
expect ( screen . queryByText ( 'color is required' ) ) . not . toBeInTheDocument ( ) ;
39
+ expect ( screen . queryByText ( 'agree is required' ) ) . not . toBeInTheDocument ( ) ;
32
40
33
41
expect ( scoreControl ) . toBeInvalid ( ) ;
34
42
userEvent . clear ( scoreControl ) ;
@@ -42,6 +50,7 @@ test('is possible to fill in a form and verify error messages (with the help of
42
50
expect ( nameControl ) . toHaveValue ( 'Tim' ) ;
43
51
expect ( scoreControl ) . toHaveValue ( 7 ) ;
44
52
expect ( colorControl ) . toHaveTextContent ( 'Green' ) ;
53
+ expect ( checkboxControl ) . toBeChecked ( ) ;
45
54
46
55
const form = screen . getByRole ( 'form' ) ;
47
56
expect ( form ) . toHaveFormValues ( {
@@ -50,6 +59,7 @@ test('is possible to fill in a form and verify error messages (with the help of
50
59
} ) ;
51
60
52
61
// material doesn't add these to the form
62
+ expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'agree' ) ?. value ) . toBe ( true ) ;
53
63
expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'color' ) ?. value ) . toBe ( 'G' ) ;
54
64
expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'date' ) ?. value ) . toEqual ( new Date ( 2022 , 7 , 11 ) ) ;
55
65
} ) ;
@@ -64,22 +74,29 @@ test('set and show pre-set form values', async () => {
64
74
score : 4 ,
65
75
color : 'B' ,
66
76
date : new Date ( 2022 , 7 , 11 ) ,
77
+ agree : true ,
67
78
} ) ;
68
79
detectChanges ( ) ;
69
80
70
81
const nameControl = screen . getByLabelText ( / n a m e / i) ;
71
82
const scoreControl = screen . getByRole ( 'spinbutton' , { name : / s c o r e / i } ) ;
72
83
const colorControl = screen . getByPlaceholderText ( / c o l o r / i) ;
84
+ const checkboxControl = screen . getByRole ( 'checkbox' , { name : / a g r e e / i } ) ;
73
85
74
86
expect ( nameControl ) . toHaveValue ( 'Max' ) ;
75
87
expect ( scoreControl ) . toHaveValue ( 4 ) ;
76
88
expect ( colorControl ) . toHaveTextContent ( 'Blue' ) ;
89
+ expect ( checkboxControl ) . toBeChecked ( ) ;
90
+ userEvent . click ( checkboxControl ) ;
77
91
78
92
const form = screen . getByRole ( 'form' ) ;
79
93
expect ( form ) . toHaveFormValues ( {
80
94
name : 'Max' ,
81
95
score : 4 ,
82
96
} ) ;
97
+
98
+ // material doesn't add these to the form
99
+ expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'agree' ) ?. value ) . toBe ( false ) ;
83
100
expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'color' ) ?. value ) . toBe ( 'B' ) ;
84
101
expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'date' ) ?. value ) . toEqual ( new Date ( 2022 , 7 , 11 ) ) ;
85
102
} ) ;
0 commit comments