@@ -13,22 +13,30 @@ test('is possible to fill in a form and verify error messages (with the help of
1313 const scoreControl = screen . getByRole ( 'spinbutton' , { name : / s c o r e / i } ) ;
1414 const colorControl = screen . getByPlaceholderText ( / c o l o r / i) ;
1515 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 } ) ;
1617
1718 const errors = screen . getByRole ( 'alert' ) ;
1819
1920 expect ( errors ) . toContainElement ( screen . queryByText ( 'name is required' ) ) ;
2021 expect ( errors ) . toContainElement ( screen . queryByText ( 'score must be greater than 1' ) ) ;
2122 expect ( errors ) . toContainElement ( screen . queryByText ( 'color is required' ) ) ;
23+ expect ( errors ) . toContainElement ( screen . queryByText ( 'agree is required' ) ) ;
2224
2325 userEvent . type ( nameControl , 'Tim' ) ;
2426 userEvent . clear ( scoreControl ) ;
2527 userEvent . type ( scoreControl , '12' ) ;
2628 userEvent . click ( colorControl ) ;
2729 userEvent . click ( screen . getByText ( / g r e e n / i) ) ;
2830
31+ expect ( checkboxControl ) . not . toBeChecked ( ) ;
32+ userEvent . click ( checkboxControl ) ;
33+ expect ( checkboxControl ) . toBeChecked ( ) ;
34+ expect ( checkboxControl ) . toBeValid ( ) ;
35+
2936 expect ( screen . queryByText ( 'name is required' ) ) . not . toBeInTheDocument ( ) ;
3037 expect ( screen . getByText ( 'score must be lesser than 10' ) ) . toBeInTheDocument ( ) ;
3138 expect ( screen . queryByText ( 'color is required' ) ) . not . toBeInTheDocument ( ) ;
39+ expect ( screen . queryByText ( 'agree is required' ) ) . not . toBeInTheDocument ( ) ;
3240
3341 expect ( scoreControl ) . toBeInvalid ( ) ;
3442 userEvent . clear ( scoreControl ) ;
@@ -42,6 +50,7 @@ test('is possible to fill in a form and verify error messages (with the help of
4250 expect ( nameControl ) . toHaveValue ( 'Tim' ) ;
4351 expect ( scoreControl ) . toHaveValue ( 7 ) ;
4452 expect ( colorControl ) . toHaveTextContent ( 'Green' ) ;
53+ expect ( checkboxControl ) . toBeChecked ( ) ;
4554
4655 const form = screen . getByRole ( 'form' ) ;
4756 expect ( form ) . toHaveFormValues ( {
@@ -50,6 +59,7 @@ test('is possible to fill in a form and verify error messages (with the help of
5059 } ) ;
5160
5261 // material doesn't add these to the form
62+ expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'agree' ) ?. value ) . toBe ( true ) ;
5363 expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'color' ) ?. value ) . toBe ( 'G' ) ;
5464 expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'date' ) ?. value ) . toEqual ( new Date ( 2022 , 7 , 11 ) ) ;
5565} ) ;
@@ -64,22 +74,29 @@ test('set and show pre-set form values', async () => {
6474 score : 4 ,
6575 color : 'B' ,
6676 date : new Date ( 2022 , 7 , 11 ) ,
77+ agree : true ,
6778 } ) ;
6879 detectChanges ( ) ;
6980
7081 const nameControl = screen . getByLabelText ( / n a m e / i) ;
7182 const scoreControl = screen . getByRole ( 'spinbutton' , { name : / s c o r e / i } ) ;
7283 const colorControl = screen . getByPlaceholderText ( / c o l o r / i) ;
84+ const checkboxControl = screen . getByRole ( 'checkbox' , { name : / a g r e e / i } ) ;
7385
7486 expect ( nameControl ) . toHaveValue ( 'Max' ) ;
7587 expect ( scoreControl ) . toHaveValue ( 4 ) ;
7688 expect ( colorControl ) . toHaveTextContent ( 'Blue' ) ;
89+ expect ( checkboxControl ) . toBeChecked ( ) ;
90+ userEvent . click ( checkboxControl ) ;
7791
7892 const form = screen . getByRole ( 'form' ) ;
7993 expect ( form ) . toHaveFormValues ( {
8094 name : 'Max' ,
8195 score : 4 ,
8296 } ) ;
97+
98+ // material doesn't add these to the form
99+ expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'agree' ) ?. value ) . toBe ( false ) ;
83100 expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'color' ) ?. value ) . toBe ( 'B' ) ;
84101 expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'date' ) ?. value ) . toEqual ( new Date ( 2022 , 7 , 11 ) ) ;
85102} ) ;
0 commit comments