aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/ducks/views/main.js
blob: 0edbf68f58b074c0fc3682c230e1d42e5417f902 (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
jest.unmock('../../../ducks/views/main');
jest.unmock('../../../ducks/utils/view');
jest.unmock('redux-thunk')
jest.unmock('redux')

import reduce, { selectRelative } from '../../../ducks/views/main';
import thunk from 'redux-thunk'
import { applyMiddleware, createStore, combineReducers } from 'redux'

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

    describe('select previous', () => {

        it('should not changed when first flow is selected', () => {
            const flows = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]
            const store = createTestStore(makeState(flows, 1))
            store.dispatch(selectRelative(-1))
            expect(store.getState().flows.views.main.selected).toEqual([1])
        })

        it('should select last flow if no flow is selected', () => {
            const flows = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]
            const store = createTestStore(makeState(flows))
            store.dispatch(selectRelative(-1))
            expect(store.getState().flows.views.main.selected).toEqual([4])
        })

    })

    describe('select next', () => {

        it('should not change when last flow is selected', () => {
            const flows = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]
            const store = createTestStore(makeState(flows, 4))
            store.dispatch(selectRelative(1))
            expect(store.getState().flows.views.main.selected).toEqual([4])
        })

        it('should select first flow if no flow is selected', () => {
            const flows = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]
            const store = createTestStore(makeState(flows, 1))
            store.dispatch(selectRelative(1))
            expect(store.getState().flows.views.main.selected).toEqual([2])
        })

    })
})

function createTestStore(defaultState) {
    return createStore(
        (state = defaultState, action) => ({
            flows: {
                ...state.flows,
                views: {
                    main: reduce(state.flows.views.main, action)
                }
            }
        }),
        defaultState,
        applyMiddleware(thunk)
    )
}

function makeState(flows, selected) {
    const list = {
        data: flows,
        byId: _.fromPairs(flows.map(flow => [flow.id, flow])),
        indexOf: _.fromPairs(flows.map((flow, index) => [flow.id, index])),
    }

    return {
        flows: {
            list,
            views: {
                main: {
                    selected: [selected],
                    view: list,
                }
            }
        }
    }
}