diff options
author | Maximilian Hils <git@maximilianhils.com> | 2017-04-30 18:26:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-30 18:26:41 +0200 |
commit | 288448c5755e098a1f632b7ba13c0c62e0e8f0b7 (patch) | |
tree | 01bf68690775e348a18d97d31e48e27741c8bd20 /web/src | |
parent | fccc9bc45ef55e3e7f14ee4a51f376c53b418991 (diff) | |
parent | 365677006cc9f749a59ce5b5cf9b13d50dca8c0f (diff) | |
download | mitmproxy-288448c5755e098a1f632b7ba13c0c62e0e8f0b7.tar.gz mitmproxy-288448c5755e098a1f632b7ba13c0c62e0e8f0b7.tar.bz2 mitmproxy-288448c5755e098a1f632b7ba13c0c62e0e8f0b7.zip |
Merge pull request #2285 from MatthewShao/jest-dev
[web] Add coverage for js/urlState.js
Diffstat (limited to 'web/src')
-rw-r--r-- | web/src/js/__tests__/urlStateSpec.js | 100 | ||||
-rw-r--r-- | web/src/js/urlState.js | 4 |
2 files changed, 102 insertions, 2 deletions
diff --git a/web/src/js/__tests__/urlStateSpec.js b/web/src/js/__tests__/urlStateSpec.js new file mode 100644 index 00000000..c57c0a00 --- /dev/null +++ b/web/src/js/__tests__/urlStateSpec.js @@ -0,0 +1,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" }) + }) +}) diff --git a/web/src/js/urlState.js b/web/src/js/urlState.js index ca9187b2..7802bdb8 100644 --- a/web/src/js/urlState.js +++ b/web/src/js/urlState.js @@ -15,7 +15,7 @@ const Query = { SHOW_EVENTLOG: "e" }; -function updateStoreFromUrl(store) { +export function updateStoreFromUrl(store) { const [path, query] = window.location.hash.substr(1).split("?", 2) const path_components = path.substr(1).split("/") @@ -50,7 +50,7 @@ function updateStoreFromUrl(store) { } } -function updateUrlFromStore(store) { +export function updateUrlFromStore(store) { const state = store.getState() let query = { [Query.SEARCH]: state.flows.filter, |