aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/ducks/utils/listSpec.js
blob: 72d162f2b11ba71af2b19eb035375723890e772d (plain)
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
jest.unmock('lodash')
jest.unmock('../../../ducks/utils/list')

import reduce, * as list from '../../../ducks/utils/list'

describe('list reduce', () => {

    it('should add item', () => {
        const state = createState([
            { id: 1 },
            { id: 2 }
        ])
        const result = createState([
            { id: 1 },
            { id: 2 },
            { id: 3 }
        ])
        expect(reduce(state, list.add({ id: 3 }))).toEqual(result)
    })

    it('should update item', () => {
        const state = createState([
            { id: 1, val: 1 },
            { id: 2, val: 2 }
        ])
        const result = createState([
            { id: 1, val: 1 },
            { id: 2, val: 3 }
        ])
        expect(reduce(state, list.update({ id: 2, val: 3 }))).toEqual(result)
    })

    it('should remove item', () => {
        const state = createState([
            { id: 1 },
            { id: 2 }
        ])
        const result = createState([
            { id: 1 }
        ])
        result.byId[2] = result.indexOf[2] = null
        expect(reduce(state, list.remove(2))).toEqual(result)
    })

    it('should replace all items', () => {
        const state = createState([
            { id: 1 },
            { id: 2 }
        ])
        const result = createState([
            { id: 1 }
        ])
        expect(reduce(state, list.receive([{ id: 1 }]))).toEqual(result)
    })
})

function createState(items) {
    return {
        data: items,
        byId: _.fromPairs(items.map((item, index) => [item.id, item])),
        indexOf: _.fromPairs(items.map((item, index) => [item.id, index]))
    }
}