Skip to content

Commit ced46c6

Browse files
dekhanoverikras
authored andcommitted
Test case for issue 19 (#20)
1 parent 8a2ad42 commit ced46c6

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/move.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,64 @@ describe('move', () => {
433433
})
434434
})
435435

436+
it('should move fields with different shapes', () => {
437+
// implementation of changeValue taken directly from Final Form
438+
const changeValue = (state, name, mutate) => {
439+
const before = getIn(state.formState.values, name)
440+
const after = mutate(before)
441+
state.formState.values = setIn(state.formState.values, name, after) || {}
442+
}
443+
const state = {
444+
formState: {
445+
values: {
446+
foo: [{ dog: 'apple dog', cat: 'apple cat' }, { dog: 'banana dog' }]
447+
}
448+
},
449+
fields: {
450+
'foo[0].dog': {
451+
name: 'foo[0].dog',
452+
touched: true,
453+
error: 'Error A Dog'
454+
},
455+
'foo[0].cat': {
456+
name: 'foo[0].cat',
457+
touched: false,
458+
error: 'Error A Cat'
459+
},
460+
'foo[1].dog': {
461+
name: 'foo[1].dog',
462+
touched: true,
463+
error: 'Error B Dog'
464+
}
465+
}
466+
}
467+
move(['foo', 0, 1], state, { changeValue })
468+
expect(state).toEqual({
469+
formState: {
470+
values: {
471+
foo: [{ dog: 'banana dog' }, { dog: 'apple dog', cat: 'apple cat' }]
472+
}
473+
},
474+
fields: {
475+
'foo[0].dog': {
476+
name: 'foo[0].dog',
477+
touched: true,
478+
error: 'Error B Dog'
479+
},
480+
'foo[1].dog': {
481+
name: 'foo[1].dog',
482+
touched: true,
483+
error: 'Error A Dog'
484+
},
485+
'foo[1].cat': {
486+
name: 'foo[1].cat',
487+
touched: false,
488+
error: 'Error A Cat'
489+
}
490+
}
491+
})
492+
})
493+
436494
it('should preserve functions in field state', () => {
437495
// implementation of changeValue taken directly from Final Form
438496
const changeValue = (state, name, mutate) => {

0 commit comments

Comments
 (0)