aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/ducks/ui.js
blob: 289192d9981be71329808aa727a3d23d42d95131 (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
jest.unmock('lodash')
jest.unmock('redux')
jest.unmock('redux-thunk')
jest.unmock('../../ducks/ui')
jest.unmock('../../ducks/views/main')

import _ from 'lodash'
import thunk from 'redux-thunk'
import { applyMiddleware, createStore, combineReducers } from 'redux'
import reducer, { setActiveMenu, selectTabRelative } from '../../ducks/ui'
import { SELECT } from '../../ducks/views/main'

describe('ui reducer', () => {
    it('should return the initial state', () => {
        expect(reducer(undefined, {}).activeMenu).toEqual('Start')
    })

    it('should return the state for view', () => {
        expect(reducer(undefined, setActiveMenu('View')).activeMenu).toEqual('View')
    })

    it('should change the state to Start when deselecting a flow and we a currently at the flow tab', () => {
        expect(reducer({ activeMenu: 'Flow' }, {
            type: SELECT,
            currentSelection: 1,
            flowId : undefined,
        }).activeMenu).toEqual('Start')
    })

    it('should change the state to Flow when we selected a flow and no flow was selected before', () => {
        expect(reducer({ activeMenu: 'Start' }, {
            type: SELECT,
            currentSelection: undefined,
            flowId : 1,
        }).activeMenu).toEqual('Flow')
    })

    it('should not change the state to Flow when OPTIONS tab is selected and we selected a flow and a flow as selected before', () => {
        expect(reducer({activeMenu: 'Options'}, {
            type: SELECT,
            currentSelection: 1,
            flowId : '2',
        }).activeMenu).toEqual('Options')
    })

    describe('select tab relative', () => {

        it('should select tab according to flow properties', () => {
            const store = createTestStore(makeState([{ id: 1 }], 1))
            store.dispatch(selectTabRelative(1))
            expect(store.getState().ui.panel).toEqual('details')
        })

        it('should select last tab when first tab is selected', () => {
            const store = createTestStore(makeState([{ id: 1, request: true, response: true, error: true }], 1))
            store.dispatch(selectTabRelative(-1))
            expect(store.getState().ui.panel).toEqual('details')
        })

    })
})

function createTestStore(state) {
    return createStore(
        combineReducers({ ui: reducer, flows: (state = {}) => state }),
        state,
        applyMiddleware(thunk)
    )
}

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