From f8b76a62ff685d9aaf7b74155f6fe5d448c80805 Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Fri, 28 Apr 2017 22:06:17 +0800 Subject: [web] Add coverage for js/urlState.js --- web/src/js/__tests__/urlStateSpec.js | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 web/src/js/__tests__/urlStateSpec.js (limited to 'web/src') diff --git a/web/src/js/__tests__/urlStateSpec.js b/web/src/js/__tests__/urlStateSpec.js new file mode 100644 index 00000000..581e79e7 --- /dev/null +++ b/web/src/js/__tests__/urlStateSpec.js @@ -0,0 +1,66 @@ +import initialize from '../urlState' + +import reduceFlows from '../ducks/flows' +import reduceUI from '../ducks/ui/index' +import reduceEventLog from '../ducks/eventLog' + +import * as flowsAction from '../ducks/flows' +import * as uiFlowAction from '../ducks/ui/flow' +import * as eventLogAction from '../ducks/eventLog' + +import {createStore} from './ducks/tutils' + + +describe('test updateStoreFromUrl and updateUrlFromStore', () => { + + let store = createStore({ + flows: reduceFlows, + ui: reduceUI, + eventLog: reduceEventLog + }) + + history.replaceState = jest.fn() + + it('should handle search query', () => { + window.location.hash = "#/flows?s=foo" + let setFilter = jest.spyOn(flowsAction, 'setFilter') + + initialize(store) + expect(setFilter).toBeCalledWith('foo') + }) + + it('should handle highlight query', () => { + window.location.hash = "#/flows?h=foo" + let setHighlight = jest.spyOn(flowsAction, 'setHighlight') + + initialize(store) + expect(setHighlight).toBeCalledWith('foo') + }) + + it('should handle show event log', () => { + window.location.hash = "#/flows?e=true" + let toggleVisibility = jest.spyOn(eventLogAction, 'toggleVisibility') + + initialize(store) + expect(toggleVisibility).toHaveBeenCalled() + }) + + it('should handle unimplemented query argument', () => { + window.location.hash = "#/flows?foo=bar" + console.error = jest.fn() + + initialize(store) + expect(console.error).toBeCalledWith("unimplemented query arg: foo=bar") + }) + + it('should select flow and tab', () => { + window.location.hash = "#/flows/123/request" + let select = jest.spyOn(flowsAction, 'select'), + selectTab = jest.spyOn(uiFlowAction, 'selectTab') + + initialize(store) + expect(select).toBeCalledWith('123') + expect(selectTab).toBeCalledWith('request') + }) + +}) -- cgit v1.2.3 From a7feced5deb2e9b8a874260e59c8badc0153c2be Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Sun, 30 Apr 2017 20:00:36 +0800 Subject: [web] Introduce redux-mock-store and minor fix. --- web/src/js/__tests__/urlStateSpec.js | 71 +++++++++++++++++++++--------------- web/src/js/urlState.js | 2 +- 2 files changed, 43 insertions(+), 30 deletions(-) (limited to 'web/src') diff --git a/web/src/js/__tests__/urlStateSpec.js b/web/src/js/__tests__/urlStateSpec.js index 581e79e7..0b173d46 100644 --- a/web/src/js/__tests__/urlStateSpec.js +++ b/web/src/js/__tests__/urlStateSpec.js @@ -3,64 +3,77 @@ import initialize 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 * as flowsAction from '../ducks/flows' -import * as uiFlowAction from '../ducks/ui/flow' -import * as eventLogAction from '../ducks/eventLog' +import configureStore from 'redux-mock-store' -import {createStore} from './ducks/tutils' - - -describe('test updateStoreFromUrl and updateUrlFromStore', () => { - - let store = createStore({ - flows: reduceFlows, - ui: reduceUI, - eventLog: reduceEventLog - }) +const mockStore = configureStore() +describe('updateStoreFromUrl', () => { history.replaceState = jest.fn() + let initialState = { + flows: reduceFlows(undefined, {}), + ui: reduceUI(undefined, {}), + eventLog: reduceEventLog(undefined, {}) + } it('should handle search query', () => { window.location.hash = "#/flows?s=foo" - let setFilter = jest.spyOn(flowsAction, 'setFilter') - + let store = mockStore(initialState) initialize(store) - expect(setFilter).toBeCalledWith('foo') + expect(store.getActions()).toEqual([{ filter: "foo", type: "FLOWS_SET_FILTER" }]) }) it('should handle highlight query', () => { window.location.hash = "#/flows?h=foo" - let setHighlight = jest.spyOn(flowsAction, 'setHighlight') - + let store = mockStore(initialState) initialize(store) - expect(setHighlight).toBeCalledWith('foo') + expect(store.getActions()).toEqual([{ highlight: "foo", type: "FLOWS_SET_HIGHLIGHT" }]) }) it('should handle show event log', () => { window.location.hash = "#/flows?e=true" - let toggleVisibility = jest.spyOn(eventLogAction, 'toggleVisibility') - + let store = mockStore(initialState) initialize(store) - expect(toggleVisibility).toHaveBeenCalled() - }) + 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(initialState) initialize(store) expect(console.error).toBeCalledWith("unimplemented query arg: foo=bar") }) it('should select flow and tab', () => { window.location.hash = "#/flows/123/request" - let select = jest.spyOn(flowsAction, 'select'), - selectTab = jest.spyOn(uiFlowAction, 'selectTab') - + let store = mockStore(initialState) initialize(store) - expect(select).toBeCalledWith('123') - expect(selectTab).toBeCalledWith('request') + expect(store.getActions()).toEqual([ + { + flowIds: ["123"], + type: "FLOWS_SELECT" + }, + { + tab: "request", + type: "UI_FLOWVIEW_SET_TAB" + } + ]) }) +}) +describe('updateUrlFromStore', () => { + history.replaceState = jest.fn() + let flows = reduceFlows(undefined, flowsActions.select(123)), + initialState = { + flows: reduceFlows(flows, flowsActions.setFilter('~u foo')), + ui: reduceUI(undefined, {}), + eventLog: reduceEventLog(undefined, {}) + } + + it('should update url', () => { + let store = mockStore(initialState) + initialize(store) + expect(history.replaceState).toBeCalledWith(undefined, '', '/#/flows/123/request?s=~u foo') + }) }) diff --git a/web/src/js/urlState.js b/web/src/js/urlState.js index ca9187b2..fcad3de3 100644 --- a/web/src/js/urlState.js +++ b/web/src/js/urlState.js @@ -78,6 +78,6 @@ function updateUrlFromStore(store) { } export default function initialize(store) { - updateStoreFromUrl(store) store.subscribe(() => updateUrlFromStore(store)) + updateStoreFromUrl(store) } -- cgit v1.2.3 From 365677006cc9f749a59ce5b5cf9b13d50dca8c0f Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Sun, 30 Apr 2017 22:40:58 +0800 Subject: [web] Export updateUrlFromStore and updateStoreFromUrl --- web/src/js/__tests__/urlStateSpec.js | 65 ++++++++++++++++++++++++------------ web/src/js/urlState.js | 6 ++-- 2 files changed, 46 insertions(+), 25 deletions(-) (limited to 'web/src') diff --git a/web/src/js/__tests__/urlStateSpec.js b/web/src/js/__tests__/urlStateSpec.js index 0b173d46..c57c0a00 100644 --- a/web/src/js/__tests__/urlStateSpec.js +++ b/web/src/js/__tests__/urlStateSpec.js @@ -1,4 +1,5 @@ import initialize from '../urlState' +import { updateStoreFromUrl, updateUrlFromStore } from '../urlState' import reduceFlows from '../ducks/flows' import reduceUI from '../ducks/ui/index' @@ -8,47 +9,44 @@ import * as flowsActions from '../ducks/flows' import configureStore from 'redux-mock-store' const mockStore = configureStore() +history.replaceState = jest.fn() describe('updateStoreFromUrl', () => { - history.replaceState = jest.fn() - let initialState = { - flows: reduceFlows(undefined, {}), - ui: reduceUI(undefined, {}), - eventLog: reduceEventLog(undefined, {}) - } it('should handle search query', () => { window.location.hash = "#/flows?s=foo" - let store = mockStore(initialState) - initialize(store) + 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(initialState) - initialize(store) + 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 store = mockStore(initialState) - initialize(store) - expect(store.getActions()).toEqual([{ type: "EVENTS_TOGGLE_VISIBILITY" }]) }) + 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(initialState) - initialize(store) + 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(initialState) - initialize(store) + let store = mockStore() + updateStoreFromUrl(store) expect(store.getActions()).toEqual([ { flowIds: ["123"], @@ -63,17 +61,40 @@ describe('updateStoreFromUrl', () => { }) describe('updateUrlFromStore', () => { - history.replaceState = jest.fn() - let flows = reduceFlows(undefined, flowsActions.select(123)), - initialState = { - flows: reduceFlows(flows, flowsActions.setFilter('~u foo')), + 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) - expect(history.replaceState).toBeCalledWith(undefined, '', '/#/flows/123/request?s=~u foo') + store.dispatch({ type: "foo" }) }) }) diff --git a/web/src/js/urlState.js b/web/src/js/urlState.js index fcad3de3..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, @@ -78,6 +78,6 @@ function updateUrlFromStore(store) { } export default function initialize(store) { - store.subscribe(() => updateUrlFromStore(store)) updateStoreFromUrl(store) + store.subscribe(() => updateUrlFromStore(store)) } -- cgit v1.2.3