aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/ducks/utils/view.js
blob: 1b07f72358b3ff1249b976726890f198229aab95 (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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
jest.unmock('../../../ducks/utils/view')
jest.unmock('lodash')

import reduce, * as view from '../../../ducks/utils/view'
import _ from 'lodash'

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

    it('should filter items', () => {
        const state = createState([
            { id: 1 },
            { id: 2 }
        ])
        const result = createState([
            { id: 1 }
        ])
        expect(reduce(state, view.updateFilter(state, item => item.id === 1))).toEqual(result)
    })

    it('should sort items', () => {
        const state = createState([
            { id: 1 },
            { id: 2 }
        ])
        const result = createState([
            { id: 2 },
            { id: 1 }
        ])
        expect(reduce(state, view.updateSort((a, b) => b.id - a.id))).toEqual(result)
    })

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

    it('should add item in place', () => {
        const state = createState([
            { id: 1 }
        ])
        const result = createState([
            { id: 3 },
            { id: 1 }
        ])
        expect(reduce(state, view.add({ id: 3 }, undefined, (a, b) => b.id - a.id))).toEqual(result)
    })

    it('should filter added item', () => {
        const state = createState([
            { id: 1 }
        ])
        const result = createState([
            { id: 1 }
        ])
        expect(reduce(state, view.add({ id: 3 }, i => i.id === 1))).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, view.update(2, { id: 2, val: 3 }))).toEqual(result)
    })

    it('should sort updated item', () => {
        const state = createState([
            { id: 1, val: 1 },
            { id: 2, val: 2 }
        ])
        const result = createState([
            { id: 2, val: 3 },
            { id: 1, val: 1 }
        ])
        expect(reduce(state, view.update(2, { id: 2, val: 3 }, undefined, (a, b) => b.id - a.id))).toEqual(result)
    })

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

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

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

    it('should sort received items', () => {
        const state = createState([
            { id: 1 },
            { id: 2 }
        ])
        const result = createState([
            { id: 2 },
            { id: 1 }
        ])
        expect(reduce(state, view.receive({ data: [{ id: 1 }, { id: 2 }] }, undefined, (a, b) => b.id - a.id))).toEqual(result)
    })

    it('should filter received', () => {
        const state = createState([
            { id: 1 },
            { id: 2 }
        ])
        const result = createState([
            { id: 1 }
        ])
        expect(reduce(state, view.receive({ data: [{ id: 1 }, { id: 2 }] }, i => i.id === 1))).toEqual(result)
    })
})

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