@@ -4,6 +4,7 @@ import userEvent from '@testing-library/user-event';
44import { MaterialModule } from '../material.module' ;
55import { MaterialFormsComponent } from './04-forms-with-material' ;
66
7+
78test ( 'is possible to fill in a form and verify error messages (with the help of jest-dom https://testing-library.com/docs/ecosystem-jest-dom)' , async ( ) => {
89 const { fixture } = await render ( MaterialFormsComponent , {
910 imports : [ MaterialModule ] ,
@@ -37,12 +38,40 @@ test('is possible to fill in a form and verify error messages (with the help of
3738
3839 expect ( nameControl ) . toHaveValue ( 'Tim' ) ;
3940 expect ( scoreControl ) . toHaveValue ( 7 ) ;
41+ expect ( colorControl ) . toHaveTextContent ( 'Green' ) ;
4042
4143 const form = screen . getByRole ( 'form' ) ;
4244 expect ( form ) . toHaveFormValues ( {
4345 name : 'Tim' ,
4446 score : 7 ,
4547 } ) ;
46-
4748 expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'color' ) ?. value ) . toBe ( 'G' ) ;
4849} ) ;
50+
51+ test ( 'set and show pre-set form values' , async ( ) => {
52+ const { fixture, detectChanges } = await render ( MaterialFormsComponent , {
53+ imports : [ MaterialModule ] ,
54+ } ) ;
55+
56+ fixture . componentInstance . form . setValue ( {
57+ name : 'Max' ,
58+ score : 4 ,
59+ color : 'B'
60+ } )
61+ detectChanges ( ) ;
62+
63+ const nameControl = screen . getByLabelText ( / n a m e / i) ;
64+ const scoreControl = screen . getByRole ( 'spinbutton' , { name : / s c o r e / i } ) ;
65+ const colorControl = screen . getByRole ( 'combobox' , { name : / c o l o r / i } ) ;
66+
67+ expect ( nameControl ) . toHaveValue ( 'Max' ) ;
68+ expect ( scoreControl ) . toHaveValue ( 4 ) ;
69+ expect ( colorControl ) . toHaveTextContent ( 'Blue' ) ;
70+
71+ const form = screen . getByRole ( 'form' ) ;
72+ expect ( form ) . toHaveFormValues ( {
73+ name : 'Max' ,
74+ score : 4 ,
75+ } ) ;
76+ expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'color' ) ?. value ) . toBe ( 'B' ) ;
77+ } ) ;
0 commit comments