aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/urlStateSpec.js
blob: c57c0a002ac49ba0b5c020656d64e9f7f9a45ad1 (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
import initialize from '../urlState'
import { updateStoreFromUrl, updateUrlFromStore } from '../urlState'

import reduceFlows from '../ducks/flows'
import reduceUI from '../ducks/ui/index'
import reduceEventLog from '../ducks/eventLog'
import * as flowsActions from '../ducks/flows'

import configureStore from 'redux-mock-store'

const mockStore = configureStore()
history.replaceState = jest.fn()

describe('updateStoreFromUrl', () => {

    it('should handle search query', () => {
        window.location.hash = "#/flows?s=foo"
        let store = mockStore()
        updateStoreFromUrl(store)
        expect(store.getActions()).toEqual([{ filter: "foo", type: "FLOWS_SET_FILTER" }])
    })

    it('should handle highlight query', () => {
        window.location.hash = "#/flows?h=foo"
        let store = mockStore()
        updateStoreFromUrl(store)
        expect(store.getActions()).toEqual([{ highlight: "foo", type: "FLOWS_SET_HIGHLIGHT" }])
    })

    it('should handle show event log', () => {
        window.location.hash = "#/flows?e=true"
        let initialState = { eventLog: reduceEventLog(undefined, {}) },
            store = mockStore(initialState)
        updateStoreFromUrl(store)
        expect(store.getActions()).toEqual([{ type: "EVENTS_TOGGLE_VISIBILITY" }])
    })

    it('should handle unimplemented query argument', () => {
        window.location.hash = "#/flows?foo=bar"
        console.error = jest.fn()
        let store = mockStore()
        updateStoreFromUrl(store)
        expect(console.error).toBeCalledWith("unimplemented query arg: foo=bar")
    })

    it('should select flow and tab', () => {
        window.location.hash = "#/flows/123/request"
        let store = mockStore()
        updateStoreFromUrl(store)
        expect(store.getActions()).toEqual([
            {
                flowIds: ["123"],
                type: "FLOWS_SELECT"
            },
            {
                tab: "request",
                type: "UI_FLOWVIEW_SET_TAB"
            }
        ])
    })
})

describe('updateUrlFromStore', () => {
    let initialState = {
        flows:    reduceFlows(undefined, {}),
        ui:       reduceUI(undefined, {}),
        eventLog: reduceEventLog(undefined, {})
    }

    it('should update initial url', () => {
        let store = mockStore(initialState)
        updateUrlFromStore(store)
        expect(history.replaceState).toBeCalledWith(undefined, '', '/#/flows')
    })

    it('should update url', () => {
        let flows = reduceFlows(undefined, flowsActions.select(123)),
            state = {
                ...initialState,
                flows: reduceFlows(flows, flowsActions.setFilter('~u foo'))
            },
            store = mockStore(state)
        updateUrlFromStore(store)
        expect(history.replaceState).toBeCalledWith(undefined, '', '/#/flows/123/request?s=~u foo')
    })
})

describe('initialize', () => {
    let initialState = {
        flows:    reduceFlows(undefined, {}),
        ui:       reduceUI(undefined, {}),
        eventLog: reduceEventLog(undefined, {})
    }

    it('should handle initial state', () => {
        let store = mockStore(initialState)
        initialize(store)
        store.dispatch({ type: "foo" })
    })
})