-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathshift.test.js
130 lines (127 loc) · 3.26 KB
/
shift.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import shift from './shift'
import { getIn, setIn } from 'final-form'
describe('shift', () => {
it('should call changeValue once', () => {
const changeValue = jest.fn()
const state = {
formState: {
values: {
foo: ['one', 'two']
}
},
fields: {
'foo[0]': {
name: 'foo[0]',
touched: true,
error: 'First Error'
},
'foo[1]': {
name: 'foo[1]',
touched: false,
error: 'Second Error'
}
}
}
const result = shift(['foo'], state, { changeValue, getIn, setIn })
expect(result).toBeUndefined()
expect(changeValue).toHaveBeenCalled()
expect(changeValue).toHaveBeenCalledTimes(1)
expect(changeValue.mock.calls[0][0]).toBe(state)
expect(changeValue.mock.calls[0][1]).toBe('foo')
expect(typeof changeValue.mock.calls[0][2]).toBe('function')
})
it('should treat undefined like an empty array', () => {
const changeValue = jest.fn()
const state = {
formState: {
values: {
foo: undefined
}
},
fields: {}
}
const returnValue = shift(['foo'], state, { changeValue, getIn, setIn })
expect(returnValue).toBeUndefined()
const op = changeValue.mock.calls[0][2]
const result = op(undefined)
expect(result).toBeUndefined()
})
it('should remove first value from array and return it', () => {
const array = ['a', 'b', 'c', 'd']
// implementation of changeValue taken directly from Final Form
const changeValue = (state, name, mutate) => {
const before = getIn(state.formState.values, name)
const after = mutate(before)
state.formState.values = setIn(state.formState.values, name, after) || {}
}
const state = {
formState: {
values: {
foo: array,
anotherField: 42
}
},
fields: {
'foo[0]': {
name: 'foo[0]',
touched: true,
error: 'A Error'
},
'foo[1]': {
name: 'foo[1]',
touched: false,
error: 'B Error'
},
'foo[2]': {
name: 'foo[2]',
touched: true,
error: 'C Error'
},
'foo[3]': {
name: 'foo[3]',
touched: false,
error: 'D Error'
},
anotherField: {
name: 'anotherField',
touched: false
}
}
}
const returnValue = shift(['foo'], state, { changeValue, getIn, setIn })
expect(returnValue).toBe('a')
expect(state.formState.values.foo).not.toBe(array) // copied
expect(state).toEqual({
formState: {
values: {
foo: ['b', 'c', 'd'],
anotherField: 42
}
},
fields: {
'foo[0]': {
name: 'foo[0]',
touched: false,
error: 'B Error',
lastFieldState: undefined
},
'foo[1]': {
name: 'foo[1]',
touched: true,
error: 'C Error',
lastFieldState: undefined
},
'foo[2]': {
name: 'foo[2]',
touched: false,
error: 'D Error',
lastFieldState: undefined
},
anotherField: {
name: 'anotherField',
touched: false
}
}
})
})
})