@@ -160,6 +160,118 @@ describe('remove', () => {
160
160
}
161
161
} )
162
162
} )
163
+
164
+
165
+ it ( 'should remove value from the specified index, and return it (nested arrays)' , ( ) => {
166
+ const array = [ 'a' , 'b' , 'c' , 'd' ]
167
+ // implementation of changeValue taken directly from Final Form
168
+ const changeValue = ( state , name , mutate ) => {
169
+ const before = getIn ( state . formState . values , name )
170
+ const after = mutate ( before )
171
+ state . formState . values = setIn ( state . formState . values , name , after ) || { }
172
+ }
173
+ function blur0 ( ) { }
174
+ function change0 ( ) { }
175
+ function focus0 ( ) { }
176
+ function blur1 ( ) { }
177
+ function change1 ( ) { }
178
+ function focus1 ( ) { }
179
+ function blur2 ( ) { }
180
+ function change2 ( ) { }
181
+ function focus2 ( ) { }
182
+ function blur3 ( ) { }
183
+ function change3 ( ) { }
184
+ function focus3 ( ) { }
185
+ const state = {
186
+ formState : {
187
+ values : {
188
+ foo : [ array ] ,
189
+ anotherField : 42
190
+ }
191
+ } ,
192
+ fields : {
193
+ 'foo[0][0]' : {
194
+ name : 'foo[0][0]' ,
195
+ blur : blur0 ,
196
+ change : change0 ,
197
+ focus : focus0 ,
198
+ touched : true ,
199
+ error : 'A Error'
200
+ } ,
201
+ 'foo[0][1]' : {
202
+ name : 'foo[0][1]' ,
203
+ blur : blur1 ,
204
+ change : change1 ,
205
+ focus : focus1 ,
206
+ touched : false ,
207
+ error : 'B Error'
208
+ } ,
209
+ 'foo[0][2]' : {
210
+ name : 'foo[0][2]' ,
211
+ blur : blur2 ,
212
+ change : change2 ,
213
+ focus : focus2 ,
214
+ touched : true ,
215
+ error : 'C Error'
216
+ } ,
217
+ 'foo[0][3]' : {
218
+ name : 'foo[0][3]' ,
219
+ blur : blur3 ,
220
+ change : change3 ,
221
+ focus : focus3 ,
222
+ touched : false ,
223
+ error : 'D Error'
224
+ } ,
225
+ anotherField : {
226
+ name : 'anotherField' ,
227
+ touched : false
228
+ }
229
+ }
230
+ }
231
+ const returnValue = remove ( [ 'foo[0]' , 1 ] , state , { changeValue } )
232
+ expect ( returnValue ) . toBe ( 'b' )
233
+ expect ( state . formState . values . foo ) . not . toBe ( array ) // copied
234
+ expect ( state ) . toEqual ( {
235
+ formState : {
236
+ values : {
237
+ foo : [ [ 'a' , 'c' , 'd' ] ] ,
238
+ anotherField : 42
239
+ }
240
+ } ,
241
+ fields : {
242
+ 'foo[0][0]' : {
243
+ name : 'foo[0][0]' ,
244
+ blur : blur0 ,
245
+ change : change0 ,
246
+ focus : focus0 ,
247
+ touched : true ,
248
+ error : 'A Error'
249
+ } ,
250
+ 'foo[0][1]' : {
251
+ name : 'foo[0][1]' ,
252
+ blur : blur1 ,
253
+ change : change1 ,
254
+ focus : focus1 ,
255
+ touched : true ,
256
+ error : 'C Error' ,
257
+ lastFieldState : undefined
258
+ } ,
259
+ 'foo[0][2]' : {
260
+ name : 'foo[0][2]' ,
261
+ blur : blur2 ,
262
+ change : change2 ,
263
+ focus : focus2 ,
264
+ touched : false ,
265
+ error : 'D Error' ,
266
+ lastFieldState : undefined
267
+ } ,
268
+ anotherField : {
269
+ name : 'anotherField' ,
270
+ touched : false
271
+ }
272
+ }
273
+ } )
274
+ } )
163
275
164
276
it ( 'should remove value from the specified index, and handle new fields' , ( ) => {
165
277
const array = [ 'a' , { key : 'val' } ]
0 commit comments