@@ -23,7 +23,7 @@ describe('pop', () => {
23
23
}
24
24
}
25
25
}
26
- const result = pop ( [ 'foo' ] , state , { changeValue } )
26
+ const result = pop ( [ 'foo' ] , state , { changeValue, getIn , setIn } )
27
27
expect ( result ) . toBeUndefined ( )
28
28
expect ( changeValue ) . toHaveBeenCalled ( )
29
29
expect ( changeValue ) . toHaveBeenCalledTimes ( 1 )
@@ -33,71 +33,59 @@ describe('pop', () => {
33
33
} )
34
34
35
35
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
+ }
37
42
const state = {
38
43
formState : {
39
44
values : {
40
- foo : [ 'one' , 'two' ]
45
+ foo : undefined
41
46
}
42
47
} ,
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 : { }
55
49
}
56
- const returnValue = pop ( [ 'foo' ] , state , { changeValue } )
57
- const op = changeValue . mock . calls [ 0 ] [ 2 ]
50
+ const returnValue = pop ( [ 'foo' ] , state , { changeValue, getIn, setIn } )
58
51
expect ( returnValue ) . toBeUndefined ( )
59
- const result = op ( undefined )
52
+ const result = state . formState . foo
60
53
expect ( result ) . toBeUndefined ( )
61
54
} )
62
55
63
56
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
+ }
65
63
const state = {
66
64
formState : {
67
65
values : {
68
- foo : [ 'one' , 'two' ]
66
+ foo : [ ]
69
67
}
70
68
} ,
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 : { }
83
70
}
84
- const returnValue = pop ( [ 'foo' ] , state , { changeValue } )
85
- const op = changeValue . mock . calls [ 0 ] [ 2 ]
71
+ const returnValue = pop ( [ 'foo' ] , state , { changeValue, getIn, setIn } )
86
72
expect ( returnValue ) . toBeUndefined ( )
87
- const result = op ( [ ] )
73
+ const result = state . formState . values . foo
88
74
expect ( Array . isArray ( result ) ) . toBe ( true )
89
75
expect ( result . length ) . toBe ( 0 )
90
76
} )
91
77
92
78
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
+ } )
97
85
const state = {
98
86
formState : {
99
87
values : {
100
- foo : [ 'one ' , 'two ' ]
88
+ foo : [ 'a ' , 'b' , 'c ']
101
89
}
102
90
} ,
103
91
fields : {
@@ -113,7 +101,8 @@ describe('pop', () => {
113
101
}
114
102
}
115
103
}
116
- const returnValue = pop ( [ 'foo' ] , state , { changeValue } )
104
+ const returnValue = pop ( [ 'foo' ] , state , { changeValue, getIn, setIn } )
105
+ const result = state . formState . values . foo
117
106
expect ( returnValue ) . toBe ( 'c' )
118
107
expect ( Array . isArray ( result ) ) . toBe ( true )
119
108
expect ( result ) . toEqual ( [ 'a' , 'b' ] )
@@ -161,7 +150,7 @@ describe('pop', () => {
161
150
}
162
151
}
163
152
}
164
- const returnValue = pop ( [ 'foo' ] , state , { changeValue } )
153
+ const returnValue = pop ( [ 'foo' ] , state , { changeValue, getIn , setIn } )
165
154
expect ( returnValue ) . toBe ( 'd' )
166
155
expect ( Array . isArray ( state . formState . values . foo ) ) . toBe ( true )
167
156
expect ( state . formState . values . foo ) . not . toBe ( array ) // copied
@@ -238,7 +227,7 @@ describe('pop', () => {
238
227
}
239
228
}
240
229
}
241
- const returnValue = pop ( [ 'foo[0]' ] , state , { changeValue } )
230
+ const returnValue = pop ( [ 'foo[0]' ] , state , { changeValue, getIn , setIn } )
242
231
expect ( returnValue ) . toBe ( 'd' )
243
232
expect ( Array . isArray ( state . formState . values . foo ) ) . toBe ( true )
244
233
expect ( state . formState . values . foo ) . not . toBe ( array ) // copied
0 commit comments