@@ -4,6 +4,7 @@ import userEvent from '@testing-library/user-event';
4
4
import { MaterialModule } from '../material.module' ;
5
5
import { MaterialFormsComponent } from './04-forms-with-material' ;
6
6
7
+
7
8
test ( '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 ( ) => {
8
9
const { fixture } = await render ( MaterialFormsComponent , {
9
10
imports : [ MaterialModule ] ,
@@ -37,12 +38,40 @@ test('is possible to fill in a form and verify error messages (with the help of
37
38
38
39
expect ( nameControl ) . toHaveValue ( 'Tim' ) ;
39
40
expect ( scoreControl ) . toHaveValue ( 7 ) ;
41
+ expect ( colorControl ) . toHaveTextContent ( 'Green' ) ;
40
42
41
43
const form = screen . getByRole ( 'form' ) ;
42
44
expect ( form ) . toHaveFormValues ( {
43
45
name : 'Tim' ,
44
46
score : 7 ,
45
47
} ) ;
46
-
47
48
expect ( ( fixture . componentInstance as MaterialFormsComponent ) . form ?. get ( 'color' ) ?. value ) . toBe ( 'G' ) ;
48
49
} ) ;
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