[web] Update tests for js/ducks/ui/keyboard.js

This commit is contained in:
Matthew Shao 2017-05-09 18:55:24 +08:00
parent aca30ad8d8
commit fd24e15bfb

View File

@ -7,151 +7,142 @@ import reduceUI from '../../../ducks/ui/index'
import * as flowsActions from '../../../ducks/flows' import * as flowsActions from '../../../ducks/flows'
import * as UIActions from '../../../ducks/ui/flow' import * as UIActions from '../../../ducks/ui/flow'
import configureStore from 'redux-mock-store' import configureStore from 'redux-mock-store'
import { createStore } from '../tutils' import thunk from 'redux-thunk'
import { fetchApi } from '../../../utils' import { fetchApi } from '../../../utils'
const mockStore = configureStore() const mockStore = configureStore([ thunk ])
console.debug = jest.fn() console.debug = jest.fn()
describe('onKeyDown', () => { describe('onKeyDown', () => {
let flows = undefined let flows = undefined
for( let i=1; i <= 12; i++ ) { for( let i=1; i <= 12; i++ ) {
flows = reduceFlows(flows, {type: flowsActions.ADD, data: {id: i}, cmd: 'add'}) flows = reduceFlows(flows, {type: flowsActions.ADD, data: {id: i, request: true, response: true}, cmd: 'add'})
} }
let store = mockStore({ flows, ui: reduceUI(undefined, {}) }) let store = mockStore({ flows, ui: reduceUI(undefined, {}) })
let createKeyEvent = (keyCode, shiftKey = undefined, ctrlKey = undefined) => { let createKeyEvent = (keyCode, shiftKey = undefined, ctrlKey = undefined) => {
return { keyCode, shiftKey, ctrlKey, preventDefault: jest.fn() } return onKeyDown({ keyCode, shiftKey, ctrlKey, preventDefault: jest.fn() })
} }
it('should handle cursor up', () => { it('should handle cursor up', () => {
store.getState().flows = reduceFlows(flows, flowsActions.select(2)) store.getState().flows = reduceFlows(flows, flowsActions.select(2))
onKeyDown(createKeyEvent(Key.K))(store.dispatch, store.getState) store.dispatch(createKeyEvent(Key.K))
expect(store.getActions().pop()).toEqual({ flowIds: [1], type: flowsActions.SELECT }) expect(store.getActions()).toEqual([{ flowIds: [1], type: flowsActions.SELECT }])
onKeyDown(createKeyEvent(Key.UP))(store.dispatch, store.getState) store.dispatch(createKeyEvent(Key.UP))
expect(store.getActions().pop()).toEqual({ flowIds: [1], type: flowsActions.SELECT }) expect(store.getActions().pop()).toEqual({ flowIds: [1], type: flowsActions.SELECT })
}) })
it('should handle cursor down', () => { it('should handle cursor down', () => {
onKeyDown(createKeyEvent(Key.J))(store.dispatch, store.getState) store.dispatch(createKeyEvent(Key.J))
expect(store.getActions().pop()).toEqual({ flowIds: [3], type: flowsActions.SELECT }) expect(store.getActions().pop()).toEqual({ flowIds: [3], type: flowsActions.SELECT })
onKeyDown(createKeyEvent(Key.DOWN))(store.dispatch, store.getState) store.dispatch(createKeyEvent(Key.DOWN))
expect(store.getActions().pop()).toEqual({ flowIds: [3], type: flowsActions.SELECT }) expect(store.getActions().pop()).toEqual({ flowIds: [3], type: flowsActions.SELECT })
}) })
it('should handle page down', () => { it('should handle page down', () => {
onKeyDown(createKeyEvent(Key.SPACE))(store.dispatch, store.getState) store.dispatch(createKeyEvent(Key.SPACE))
expect(store.getActions().pop()).toEqual({ flowIds: [12], type: flowsActions.SELECT }) expect(store.getActions().pop()).toEqual({ flowIds: [12], type: flowsActions.SELECT })
store.getState().flows = reduceFlows(flows, flowsActions.select(1)) store.getState().flows = reduceFlows(flows, flowsActions.select(1))
onKeyDown(createKeyEvent(Key.PAGE_DOWN))(store.dispatch, store.getState) store.dispatch(createKeyEvent(Key.PAGE_DOWN))
expect(store.getActions().pop()).toEqual({ flowIds: [11], type: flowsActions.SELECT }) expect(store.getActions().pop()).toEqual({ flowIds: [11], type: flowsActions.SELECT })
}) })
it('should handle page up', () => { it('should handle page up', () => {
store.getState().flows = reduceFlows(flows, flowsActions.select(11)) store.getState().flows = reduceFlows(flows, flowsActions.select(11))
onKeyDown(createKeyEvent(Key.PAGE_UP))(store.dispatch, store.getState) store.dispatch(createKeyEvent(Key.PAGE_UP))
expect(store.getActions().pop()).toEqual({ flowIds: [1], type: flowsActions.SELECT }) expect(store.getActions().pop()).toEqual({ flowIds: [1], type: flowsActions.SELECT })
}) })
it('should handle select first', () => { it('should handle select first', () => {
onKeyDown(createKeyEvent(Key.HOME))(store.dispatch, store.getState) store.dispatch(createKeyEvent(Key.HOME))
expect(store.getActions().pop()).toEqual({ flowIds: [1], type: flowsActions.SELECT }) expect(store.getActions().pop()).toEqual({ flowIds: [1], type: flowsActions.SELECT })
}) })
it('should handle select last', () => { it('should handle select last', () => {
store.getState().flows = reduceFlows(flows, flowsActions.select(1)) store.getState().flows = reduceFlows(flows, flowsActions.select(1))
onKeyDown(createKeyEvent(Key.END))(store.dispatch, store.getState) store.dispatch(createKeyEvent(Key.END))
expect(store.getActions().pop()).toEqual({ flowIds: [12], type: flowsActions.SELECT }) expect(store.getActions().pop()).toEqual({ flowIds: [12], type: flowsActions.SELECT })
}) })
it('should handle deselect', () => { it('should handle deselect', () => {
onKeyDown(createKeyEvent(Key.ESC))(store.dispatch, store.getState) store.dispatch(createKeyEvent(Key.ESC))
expect(store.getActions().pop()).toEqual({ flowIds: [], type: flowsActions.SELECT }) expect(store.getActions().pop()).toEqual({ flowIds: [], type: flowsActions.SELECT })
}) })
it('should handle switch to left tab', () => { it('should handle switch to left tab', () => {
onKeyDown(createKeyEvent(Key.LEFT))(store.dispatch, store.getState) store.dispatch(createKeyEvent(Key.LEFT))
expect(store.getActions().pop()).toEqual({ tab: 'details', type: UIActions.SET_TAB }) expect(store.getActions().pop()).toEqual({ tab: 'details', type: UIActions.SET_TAB })
}) })
it('should handle switch to right tab', () => { it('should handle switch to right tab', () => {
onKeyDown(createKeyEvent(Key.TAB))(store.dispatch, store.getState) store.dispatch(createKeyEvent(Key.TAB))
expect(store.getActions().pop()).toEqual({ tab: 'details', type: UIActions.SET_TAB }) expect(store.getActions().pop()).toEqual({ tab: 'response', type: UIActions.SET_TAB })
})
it('should handle switch to left tab', () => { store.dispatch(createKeyEvent(Key.RIGHT))
onKeyDown(createKeyEvent(Key.LEFT))(store.dispatch, store.getState) expect(store.getActions().pop()).toEqual({ tab: 'response', type: UIActions.SET_TAB })
expect(store.getActions().pop()).toEqual({ tab: 'details', type: UIActions.SET_TAB })
}) })
it('should handle switch to right tab', () => {
onKeyDown(createKeyEvent(Key.TAB))(store.dispatch, store.getState)
expect(store.getActions().pop()).toEqual({ tab: 'details', type: UIActions.SET_TAB })
})
let tStore = createStore({ reduceFlows })
// we need to use the real dispatch to test the actions below
it('should handle delete action', () => { it('should handle delete action', () => {
onKeyDown(createKeyEvent(Key.D))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.D))
expect(fetchApi).toBeCalledWith('/flows/1', { method: 'DELETE' }) expect(fetchApi).toBeCalledWith('/flows/1', { method: 'DELETE' })
}) })
it('should handle duplicate action', () => { it('should handle duplicate action', () => {
onKeyDown(createKeyEvent(Key.D, true))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.D, true))
expect(fetchApi).toBeCalledWith('/flows/1/duplicate', { method: 'POST' }) expect(fetchApi).toBeCalledWith('/flows/1/duplicate', { method: 'POST' })
}) })
it('should handle resume action', () => { it('should handle resume action', () => {
// resume all // resume all
onKeyDown(createKeyEvent(Key.A, true))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.A, true))
expect(fetchApi).toBeCalledWith('/flows/resume', { method: 'POST' }) expect(fetchApi).toBeCalledWith('/flows/resume', { method: 'POST' })
// resume // resume
store.getState().flows.byId[store.getState().flows.selected[0]].intercepted = true store.getState().flows.byId[store.getState().flows.selected[0]].intercepted = true
onKeyDown(createKeyEvent(Key.A))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.A))
expect(fetchApi).toBeCalledWith('/flows/1/resume', { method: 'POST' }) expect(fetchApi).toBeCalledWith('/flows/1/resume', { method: 'POST' })
}) })
it('should handle replay action', () => { it('should handle replay action', () => {
onKeyDown(createKeyEvent(Key.R))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.R))
expect(fetchApi).toBeCalledWith('/flows/1/replay', { method: 'POST' }) expect(fetchApi).toBeCalledWith('/flows/1/replay', { method: 'POST' })
}) })
it('should handle revert action', () => { it('should handle revert action', () => {
store.getState().flows.byId[store.getState().flows.selected[0]].modified = true store.getState().flows.byId[store.getState().flows.selected[0]].modified = true
onKeyDown(createKeyEvent(Key.V))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.V))
expect(fetchApi).toBeCalledWith('/flows/1/revert', { method: 'POST' }) expect(fetchApi).toBeCalledWith('/flows/1/revert', { method: 'POST' })
}) })
it('should handle kill action', () => { it('should handle kill action', () => {
// kill all // kill all
onKeyDown(createKeyEvent(Key.X, true))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.X, true))
expect(fetchApi).toBeCalledWith('/flows/kill', { method: 'POST' }) expect(fetchApi).toBeCalledWith('/flows/kill', { method: 'POST' })
// kill // kill
onKeyDown(createKeyEvent(Key.X))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.X))
expect(fetchApi).toBeCalledWith('/flows/1/kill', { method: 'POST' }) expect(fetchApi).toBeCalledWith('/flows/1/kill', { method: 'POST' })
}) })
it('should handle clear action', () => { it('should handle clear action', () => {
onKeyDown(createKeyEvent(Key.Z))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.Z))
expect(fetchApi).toBeCalledWith('/clear', { method: 'POST' }) expect(fetchApi).toBeCalledWith('/clear', { method: 'POST' })
}) })
it('should stop on some action with no flow is selected', () => { it('should stop on some action with no flow is selected', () => {
fetchApi.mockClear() fetchApi.mockClear()
store.getState().flows = reduceFlows(undefined, {}) store.getState().flows = reduceFlows(undefined, {})
onKeyDown(createKeyEvent(Key.LEFT))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.LEFT))
onKeyDown(createKeyEvent(Key.TAB))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.TAB))
onKeyDown(createKeyEvent(Key.RIGHT))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.RIGHT))
onKeyDown(createKeyEvent(Key.D))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.D))
expect(fetchApi).not.toBeCalled() expect(fetchApi).not.toBeCalled()
}) })
it('should do nothing when Ctrl and undefined key is pressed ', () => { it('should do nothing when Ctrl and undefined key is pressed ', () => {
onKeyDown(createKeyEvent(Key.BACKSPACE, false, true))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(Key.BACKSPACE, false, true))
onKeyDown(createKeyEvent(0))(tStore.dispatch, store.getState) store.dispatch(createKeyEvent(0))
expect(fetchApi).not.toBeCalled() expect(fetchApi).not.toBeCalled()
}) })