@@ -69,53 +69,68 @@ describe('createForm', () => {
6969 expect ( instance . errors . subscribe ) . toBeDefined ( ) ;
7070 } ) ;
7171
72- it ( 'should match the shape of validationSchema' , ( done ) => {
73- const initialValues = {
74- objectWithPrimitives : { foo : '' , bar : '' , baz : '' } ,
75- arrayOfPrimitives : [ ] ,
76- arrayOfObjects : [ ] ,
77- objectWithMixedTypes : {
78- foo : '' ,
79- arrayOfPrimitives : [ ] ,
80- } ,
81- } ;
82- const validationSchema = yup . object ( ) . shape ( {
83- primitive : yup . string ( ) . required ( ) ,
84- arrayOfPrimitives : yup . array ( ) . of ( yup . string ( ) ) . required ( ) ,
85- objectWithMixedTypes : yup . object ( ) . shape ( {
86- foo : yup . string ( ) . required ( ) ,
87- arrayOfPrimitives : yup . array ( ) . of ( yup . string ( ) ) . required ( ) ,
88- } ) ,
89- objectWithPrimitives : yup . object ( ) . shape ( {
72+ describe ( 'using validationSchema' , ( ) => {
73+ it ( 'initialises primitive properties to empty strings' , async ( ) => {
74+ const initialValues = { foo : 'bar' , baz : 'quux' , nested : { foo : 'bar' } } ;
75+ const validationSchema = yup . object ( ) . shape ( {
9076 foo : yup . string ( ) . required ( ) ,
91- bar : yup . string ( ) . required ( ) ,
92- } ) ,
93- arrayOfObjects : yup . array ( ) . of (
94- yup . object ( {
95- foo : yup . string ( ) . required ( ) ,
96- bar : yup . string ( ) . required ( ) ,
97- } ) ,
98- ) ,
99- } ) ;
100- instance = getInstance ( {
101- initialValues,
102- validationSchema,
77+ baz : yup . string ( ) . required ( ) ,
78+ nested : yup . object ( ) . shape ( { foo : yup . string ( ) . required ( ) } ) ,
79+ } ) ;
80+ const instance = getInstance ( { initialValues, validationSchema} ) ;
81+ const $errors = await subscribeOnce ( instance . errors ) ;
82+
83+ expect ( $errors . foo ) . toBe ( '' ) ;
84+ expect ( $errors . baz ) . toBe ( '' ) ;
85+ expect ( $errors . nested . foo ) . toBe ( '' ) ;
10386 } ) ;
10487
105- subscribeOnce ( instance . errors ) . then ( ( errors ) => {
106- expect ( errors . primitive ) . toBe ( '' ) ;
107- expect ( errors . arrayOfPrimitives ) . toEqual ( [ ] ) ;
88+ it ( 'does not initialise initialValues not defined in schema' , async ( ) => {
89+ const initialValues = { notInSchema : '' } ;
90+ const validationSchema = yup
91+ . object ( )
92+ . shape ( { foo : yup . string ( ) . required ( ) } ) ;
93+ const instance = getInstance ( { initialValues, validationSchema} ) ;
94+ const $errors = await subscribeOnce ( instance . errors ) ;
95+
96+ expect ( $errors . notInSchema ) . toBeUndefined ( ) ;
97+ } ) ;
10898
109- expect ( errors . objectWithPrimitives . foo ) . toBe ( '' ) ;
110- expect ( errors . objectWithPrimitives . bar ) . toBe ( '' ) ;
111- expect ( errors . objectWithPrimitives . baz ) . not . toBeDefined ( ) ;
99+ it ( 'contains an empty array when initialValues property is empty' , async ( ) => {
100+ const initialValues = { foo : [ ] } ;
101+ const validationSchema = yup . object ( ) . shape ( {
102+ stringArray : yup . array ( ) . of ( yup . string ( ) . required ( ) ) ,
103+ objectArray : yup
104+ . array ( )
105+ . of (
106+ yup
107+ . object ( )
108+ . shape ( { foo : yup . array ( ) . of ( yup . string ( ) . required ( ) ) } ) ,
109+ ) ,
110+ nested : yup
111+ . object ( )
112+ . shape ( { foo : yup . array ( ) . of ( yup . string ( ) . required ( ) ) } ) ,
113+ } ) ;
114+ const instance = getInstance ( { initialValues, validationSchema} ) ;
115+ const $errors = await subscribeOnce ( instance . errors ) ;
112116
113- expect ( errors . objectWithMixedTypes . foo ) . toBe ( '' ) ;
114- expect ( errors . objectWithMixedTypes . arrayOfPrimitives ) . toEqual ( [ ] ) ;
117+ expect ( $errors . stringArray ) . toEqual ( [ ] ) ;
118+ expect ( $errors . objectArray ) . toEqual ( [ ] ) ;
119+ expect ( $errors . nested . foo ) . toEqual ( [ ] ) ;
120+ } ) ;
115121
116- expect ( errors . arrayOfObjects ) . toEqual ( [ ] ) ;
122+ it ( 'preserves number of initial values for array properties' , async ( ) => {
123+ const initialValues = { foo : [ { name : 'foo' } , { name : 'bar' } ] } ;
124+ const validationSchema = yup . object ( ) . shape ( {
125+ foo : yup
126+ . array ( )
127+ . of ( yup . object ( ) . shape ( { name : yup . string ( ) . required ( ) } ) ) ,
128+ } ) ;
129+ const instance = getInstance ( { initialValues, validationSchema} ) ;
130+ const $errors = await subscribeOnce ( instance . errors ) ;
131+ const $form = await subscribeOnce ( instance . form ) ;
117132
118- done ( ) ;
133+ expect ( $errors . foo . length ) . toEqual ( $form . foo . length ) ;
119134 } ) ;
120135 } ) ;
121136 } ) ;
0 commit comments