forked from final-form/final-form-arrays
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.test.js
38 lines (34 loc) · 1.21 KB
/
update.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
import update from './update'
describe('update', () => {
const getOp = (index, value) => {
const changeValue = jest.fn()
update(['foo', index, value], {}, { changeValue })
return changeValue.mock.calls[0][2]
}
it('should call changeValue once', () => {
const changeValue = jest.fn()
const state = {}
const result = update(['foo', 0, 'bar'], state, { changeValue })
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 op = getOp(0, 'bar')
const result = op(undefined)
expect(Array.isArray(result)).toBe(true)
expect(result.length).toBe(1)
expect(result[0]).toBe('bar')
})
it('should update value of the specified index', () => {
const op = getOp(1, 'd')
const array = ['a', 'b', 'c']
const result = op(array)
expect(result).not.toBe(array) // copied
expect(Array.isArray(result)).toBe(true)
expect(result).toEqual(['a', 'd', 'c'])
})
})