@@ -23,7 +23,7 @@ describe('pop', () => {
2323 }
2424 }
2525 }
26- const result = pop ( [ 'foo' ] , state , { changeValue } )
26+ const result = pop ( [ 'foo' ] , state , { changeValue, getIn , setIn } )
2727 expect ( result ) . toBeUndefined ( )
2828 expect ( changeValue ) . toHaveBeenCalled ( )
2929 expect ( changeValue ) . toHaveBeenCalledTimes ( 1 )
@@ -33,71 +33,59 @@ describe('pop', () => {
3333 } )
3434
3535 it ( 'should return undefined if array is undefined' , ( ) => {
36- const changeValue = jest . fn ( )
36+ // implementation of changeValue taken directly from Final Form
37+ const changeValue = ( state , name , mutate ) => {
38+ const before = getIn ( state . formState . values , name )
39+ const after = mutate ( before )
40+ state . formState . values = setIn ( state . formState . values , name , after ) || { }
41+ }
3742 const state = {
3843 formState : {
3944 values : {
40- foo : [ 'one' , 'two' ]
45+ foo : undefined
4146 }
4247 } ,
43- fields : {
44- 'foo[0]' : {
45- name : 'foo[0]' ,
46- touched : true ,
47- error : 'First Error'
48- } ,
49- 'foo[1]' : {
50- name : 'foo[1]' ,
51- touched : false ,
52- error : 'Second Error'
53- }
54- }
48+ fields : { }
5549 }
56- const returnValue = pop ( [ 'foo' ] , state , { changeValue } )
57- const op = changeValue . mock . calls [ 0 ] [ 2 ]
50+ const returnValue = pop ( [ 'foo' ] , state , { changeValue, getIn, setIn } )
5851 expect ( returnValue ) . toBeUndefined ( )
59- const result = op ( undefined )
52+ const result = state . formState . foo
6053 expect ( result ) . toBeUndefined ( )
6154 } )
6255
6356 it ( 'should return empty array if array is empty' , ( ) => {
64- const changeValue = jest . fn ( )
57+ // implementation of changeValue taken directly from Final Form
58+ const changeValue = ( state , name , mutate ) => {
59+ const before = getIn ( state . formState . values , name )
60+ const after = mutate ( before )
61+ state . formState . values = setIn ( state . formState . values , name , after ) || { }
62+ }
6563 const state = {
6664 formState : {
6765 values : {
68- foo : [ 'one' , 'two' ]
66+ foo : [ ]
6967 }
7068 } ,
71- fields : {
72- 'foo[0]' : {
73- name : 'foo[0]' ,
74- touched : true ,
75- error : 'First Error'
76- } ,
77- 'foo[1]' : {
78- name : 'foo[1]' ,
79- touched : false ,
80- error : 'Second Error'
81- }
82- }
69+ fields : { }
8370 }
84- const returnValue = pop ( [ 'foo' ] , state , { changeValue } )
85- const op = changeValue . mock . calls [ 0 ] [ 2 ]
71+ const returnValue = pop ( [ 'foo' ] , state , { changeValue, getIn, setIn } )
8672 expect ( returnValue ) . toBeUndefined ( )
87- const result = op ( [ ] )
73+ const result = state . formState . values . foo
8874 expect ( Array . isArray ( result ) ) . toBe ( true )
8975 expect ( result . length ) . toBe ( 0 )
9076 } )
9177
9278 it ( 'should pop value off the end of array and return it' , ( ) => {
93- let result
94- const changeValue = jest . fn ( ( args , state , op ) => {
95- result = op ( [ 'a' , 'b' , 'c' ] )
96- } )
79+ // implementation of changeValue taken directly from Final Form
80+ const changeValue = jest . fn ( ( state , name , mutate ) => {
81+ const before = getIn ( state . formState . values , name )
82+ const after = mutate ( before )
83+ state . formState . values = setIn ( state . formState . values , name , after ) || { }
84+ } )
9785 const state = {
9886 formState : {
9987 values : {
100- foo : [ 'one ' , 'two ' ]
88+ foo : [ 'a ' , 'b' , 'c ']
10189 }
10290 } ,
10391 fields : {
@@ -113,7 +101,8 @@ describe('pop', () => {
113101 }
114102 }
115103 }
116- const returnValue = pop ( [ 'foo' ] , state , { changeValue } )
104+ const returnValue = pop ( [ 'foo' ] , state , { changeValue, getIn, setIn } )
105+ const result = state . formState . values . foo
117106 expect ( returnValue ) . toBe ( 'c' )
118107 expect ( Array . isArray ( result ) ) . toBe ( true )
119108 expect ( result ) . toEqual ( [ 'a' , 'b' ] )
@@ -161,7 +150,7 @@ describe('pop', () => {
161150 }
162151 }
163152 }
164- const returnValue = pop ( [ 'foo' ] , state , { changeValue } )
153+ const returnValue = pop ( [ 'foo' ] , state , { changeValue, getIn , setIn } )
165154 expect ( returnValue ) . toBe ( 'd' )
166155 expect ( Array . isArray ( state . formState . values . foo ) ) . toBe ( true )
167156 expect ( state . formState . values . foo ) . not . toBe ( array ) // copied
@@ -238,7 +227,7 @@ describe('pop', () => {
238227 }
239228 }
240229 }
241- const returnValue = pop ( [ 'foo[0]' ] , state , { changeValue } )
230+ const returnValue = pop ( [ 'foo[0]' ] , state , { changeValue, getIn , setIn } )
242231 expect ( returnValue ) . toBe ( 'd' )
243232 expect ( Array . isArray ( state . formState . values . foo ) ) . toBe ( true )
244233 expect ( state . formState . values . foo ) . not . toBe ( array ) // copied
0 commit comments