|
| 1 | +import remove from './remove' |
| 2 | +import { getIn, setIn, MutableState } from 'final-form' |
| 3 | +import { createMockTools } from './testUtils' |
| 4 | + |
| 5 | +describe('remove submitErrors regression #47', () => { |
| 6 | + it('should remove submitErrors when removing array item', () => { |
| 7 | + const changeValue = jest.fn((state: any, name: string, mutate: (value: any) => any) => { |
| 8 | + const before = getIn(state.formState.values, name) |
| 9 | + const after = mutate(before) |
| 10 | + state.formState.values = setIn(state.formState.values, name, after) || {} as any |
| 11 | + }) |
| 12 | + |
| 13 | + const state: MutableState<any> = { |
| 14 | + formState: { |
| 15 | + values: { |
| 16 | + customers: [ |
| 17 | + { name: 'Alice' }, |
| 18 | + { name: 'Bob' }, |
| 19 | + { name: 'Charlie' } |
| 20 | + ] |
| 21 | + } as any, |
| 22 | + submitErrors: { |
| 23 | + customers: [ |
| 24 | + { name: 'Error for Alice' }, |
| 25 | + { name: 'Error for Bob' }, |
| 26 | + { name: 'Error for Charlie' } |
| 27 | + ] |
| 28 | + } |
| 29 | + }, |
| 30 | + fields: { |
| 31 | + 'customers[0]': { name: 'customers[0]' } as any, |
| 32 | + 'customers[0].name': { name: 'customers[0].name' } as any, |
| 33 | + 'customers[1]': { name: 'customers[1]' } as any, |
| 34 | + 'customers[1].name': { name: 'customers[1].name' } as any, |
| 35 | + 'customers[2]': { name: 'customers[2]' } as any, |
| 36 | + 'customers[2].name': { name: 'customers[2].name' } as any |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // Remove middle item (index 1 - Bob) |
| 41 | + remove(['customers', 1], state, createMockTools({ changeValue, getIn, setIn })) |
| 42 | + |
| 43 | + // Values should have Bob removed |
| 44 | + expect(state.formState.values.customers).toHaveLength(2) |
| 45 | + expect(state.formState.values.customers[0].name).toBe('Alice') |
| 46 | + expect(state.formState.values.customers[1].name).toBe('Charlie') |
| 47 | + |
| 48 | + // Submit errors should also have Bob's error removed |
| 49 | + expect(state.formState.submitErrors.customers).toHaveLength(2) |
| 50 | + expect(state.formState.submitErrors.customers[0].name).toBe('Error for Alice') |
| 51 | + expect(state.formState.submitErrors.customers[1].name).toBe('Error for Charlie') |
| 52 | + }) |
| 53 | + |
| 54 | + it('should handle removing first item with submitErrors', () => { |
| 55 | + const changeValue = jest.fn((state: any, name: string, mutate: (value: any) => any) => { |
| 56 | + const before = getIn(state.formState.values, name) |
| 57 | + const after = mutate(before) |
| 58 | + state.formState.values = setIn(state.formState.values, name, after) || {} as any |
| 59 | + }) |
| 60 | + |
| 61 | + const state: MutableState<any> = { |
| 62 | + formState: { |
| 63 | + values: { |
| 64 | + items: ['A', 'B', 'C'] |
| 65 | + } as any, |
| 66 | + submitErrors: { |
| 67 | + items: ['Error A', 'Error B', 'Error C'] |
| 68 | + } |
| 69 | + }, |
| 70 | + fields: { |
| 71 | + 'items[0]': { name: 'items[0]' } as any, |
| 72 | + 'items[1]': { name: 'items[1]' } as any, |
| 73 | + 'items[2]': { name: 'items[2]' } as any |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Remove first item |
| 78 | + remove(['items', 0], state, createMockTools({ changeValue, getIn, setIn })) |
| 79 | + |
| 80 | + expect(state.formState.values.items).toEqual(['B', 'C']) |
| 81 | + expect(state.formState.submitErrors.items).toEqual(['Error B', 'Error C']) |
| 82 | + }) |
| 83 | + |
| 84 | + it('should not crash when no submitErrors exist', () => { |
| 85 | + const changeValue = jest.fn((state: any, name: string, mutate: (value: any) => any) => { |
| 86 | + const before = getIn(state.formState.values, name) |
| 87 | + const after = mutate(before) |
| 88 | + state.formState.values = setIn(state.formState.values, name, after) || {} as any |
| 89 | + }) |
| 90 | + |
| 91 | + const state: MutableState<any> = { |
| 92 | + formState: { |
| 93 | + values: { |
| 94 | + items: ['A', 'B'] |
| 95 | + } as any |
| 96 | + // No submitErrors |
| 97 | + }, |
| 98 | + fields: { |
| 99 | + 'items[0]': { name: 'items[0]' } as any, |
| 100 | + 'items[1]': { name: 'items[1]' } as any |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Should not crash |
| 105 | + expect(() => { |
| 106 | + remove(['items', 0], state, createMockTools({ changeValue, getIn, setIn })) |
| 107 | + }).not.toThrow() |
| 108 | + |
| 109 | + expect(state.formState.values.items).toEqual(['B']) |
| 110 | + }) |
| 111 | +}) |
0 commit comments