aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/ducks
diff options
context:
space:
mode:
authorMatthew Shao <me@matshao.com>2017-07-20 15:39:43 +0800
committerMatthew Shao <me@matshao.com>2017-07-20 15:39:43 +0800
commitf465f08c9ac302007b3aec6709a8e82d63c7ad65 (patch)
treef50a0700ba88ac91f1e82fafeb96c65a0e7f4fb4 /web/src/js/__tests__/ducks
parentcbf5db9a4ff77440276ca6bb4d395cc5bdee30ad (diff)
downloadmitmproxy-f465f08c9ac302007b3aec6709a8e82d63c7ad65.tar.gz
mitmproxy-f465f08c9ac302007b3aec6709a8e82d63c7ad65.tar.bz2
mitmproxy-f465f08c9ac302007b3aec6709a8e82d63c7ad65.zip
[web] Minor fix and update tests.
Diffstat (limited to 'web/src/js/__tests__/ducks')
-rw-r--r--web/src/js/__tests__/ducks/optionsSpec.js32
-rw-r--r--web/src/js/__tests__/ducks/tutils.js6
-rw-r--r--web/src/js/__tests__/ducks/ui/keyboardSpec.js7
-rw-r--r--web/src/js/__tests__/ducks/ui/optionEditorSpec.js32
-rw-r--r--web/src/js/__tests__/ducks/ui/optionSpec.js39
5 files changed, 113 insertions, 3 deletions
diff --git a/web/src/js/__tests__/ducks/optionsSpec.js b/web/src/js/__tests__/ducks/optionsSpec.js
index 62019715..0925fcc1 100644
--- a/web/src/js/__tests__/ducks/optionsSpec.js
+++ b/web/src/js/__tests__/ducks/optionsSpec.js
@@ -1,6 +1,9 @@
-jest.mock('../../utils')
-
import reduceOptions, * as OptionsActions from '../../ducks/options'
+import configureStore from 'redux-mock-store'
+import thunk from 'redux-thunk'
+import * as OptionsEditorActions from '../../ducks/ui/optionsEditor'
+
+const mockStore = configureStore([ thunk ])
describe('option reducer', () => {
it('should return initial state', () => {
@@ -18,8 +21,31 @@ describe('option reducer', () => {
})
})
+let store = mockStore()
+
describe('option actions', () => {
+
it('should be possible to update option', () => {
- expect(reduceOptions(undefined, OptionsActions.update())).toEqual({})
+ let mockResponse = { status: 200 },
+ promise = Promise.resolve(mockResponse)
+ global.fetch = r => { return promise }
+ store.dispatch(OptionsActions.update('foo', 'bar'))
+ expect(store.getActions()).toEqual([
+ { type: OptionsEditorActions.OPTION_UPDATE_START, option: 'foo', value: 'bar'}
+ ])
+ store.clearActions()
+ })
+})
+
+describe('sendUpdate', () => {
+
+ it('should handle error', () => {
+ let mockResponse = { status: 400, text: p => Promise.resolve('error') },
+ promise = Promise.resolve(mockResponse)
+ global.fetch = r => { return promise }
+ OptionsActions.pureSendUpdate('bar', 'error')
+ expect(store.getActions()).toEqual([
+ { type: OptionsEditorActions.OPTION_UPDATE_SUCCESS, option: 'foo'}
+ ])
})
})
diff --git a/web/src/js/__tests__/ducks/tutils.js b/web/src/js/__tests__/ducks/tutils.js
index a3e9c168..22240448 100644
--- a/web/src/js/__tests__/ducks/tutils.js
+++ b/web/src/js/__tests__/ducks/tutils.js
@@ -35,6 +35,12 @@ export function TStore(){
},
modal: {
activeModal: undefined
+ },
+ optionsEditor: {
+ booleanOption: { isUpdating: true, error: false },
+ strOption: { error: true },
+ intOption: {},
+ choiceOption: {},
}
},
settings: {
diff --git a/web/src/js/__tests__/ducks/ui/keyboardSpec.js b/web/src/js/__tests__/ducks/ui/keyboardSpec.js
index 500733cb..cf17943f 100644
--- a/web/src/js/__tests__/ducks/ui/keyboardSpec.js
+++ b/web/src/js/__tests__/ducks/ui/keyboardSpec.js
@@ -6,6 +6,7 @@ import reduceFlows from '../../../ducks/flows'
import reduceUI from '../../../ducks/ui/index'
import * as flowsActions from '../../../ducks/flows'
import * as UIActions from '../../../ducks/ui/flow'
+import * as modalActions from '../../../ducks/ui/modal'
import configureStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import { fetchApi } from '../../../utils'
@@ -154,4 +155,10 @@ describe('onKeyDown', () => {
expect(fetchApi).not.toBeCalled()
})
+ it('should close modal', () => {
+ store.getState().ui.modal.activeModal = true
+ store.dispatch(createKeyEvent(Key.ESC))
+ expect(store.getActions()).toEqual([ {type: modalActions.HIDE_MODAL} ])
+ })
+
})
diff --git a/web/src/js/__tests__/ducks/ui/optionEditorSpec.js b/web/src/js/__tests__/ducks/ui/optionEditorSpec.js
new file mode 100644
index 00000000..df9161a4
--- /dev/null
+++ b/web/src/js/__tests__/ducks/ui/optionEditorSpec.js
@@ -0,0 +1,32 @@
+import reduceOptionsEditor, * as optionsEditorActions from '../../../ducks/ui/optionsEditor'
+import { HIDE_MODAL } from '../../../ducks/ui/modal'
+
+describe('optionsEditor reducer', () => {
+
+ it('should return initial state', () => {
+ expect(reduceOptionsEditor(undefined, {})).toEqual({})
+ })
+
+ let state = undefined
+ it('should handle option update start', () => {
+ state = reduceOptionsEditor(undefined, optionsEditorActions.startUpdate('foo', 'bar'))
+ expect(state).toEqual({ foo: {error: false, isUpdating: true, value: 'bar'}})
+ })
+
+ it('should handle option update success', () => {
+ expect(reduceOptionsEditor(state, optionsEditorActions.updateSuccess('foo'))).toEqual({foo: undefined})
+ })
+
+ it('should handle option update error', () => {
+ state = reduceOptionsEditor(state, optionsEditorActions.updateError('foo', 'errorMsg'))
+ expect(state).toEqual({ foo: {error: 'errorMsg', isUpdating: false, value: 'bar'}})
+ // boolean type
+ state = reduceOptionsEditor(undefined, optionsEditorActions.startUpdate('foo', true))
+ state = reduceOptionsEditor(state, optionsEditorActions.updateError('foo', 'errorMsg'))
+ expect(state).toEqual({ foo: {error: 'errorMsg', isUpdating: false, value: false}})
+ })
+
+ it('should handle hide modal', () => {
+ expect(reduceOptionsEditor(undefined, {type: HIDE_MODAL})).toEqual({})
+ })
+})
diff --git a/web/src/js/__tests__/ducks/ui/optionSpec.js b/web/src/js/__tests__/ducks/ui/optionSpec.js
new file mode 100644
index 00000000..4b6b43cc
--- /dev/null
+++ b/web/src/js/__tests__/ducks/ui/optionSpec.js
@@ -0,0 +1,39 @@
+import reduceOption, * as optionActions from '../../../ducks/ui/option'
+
+describe('option reducer', () => {
+
+ it('should return the initial state', () => {
+ expect(reduceOption(undefined, {})).toEqual({})
+ })
+
+ let state = undefined
+ it('should handle option update start', () => {
+ state = reduceOption(undefined, {
+ type: optionActions.OPTION_UPDATE_START, option: 'foo', value: 'bar'
+ })
+ expect(state).toEqual({
+ foo: {
+ error: false,
+ isUpdating: true,
+ value: 'bar'
+ }
+ })
+ })
+
+ it('should handle option update success', () => {
+ expect(reduceOption(state, {
+ type: optionActions.OPTION_UPDATE_SUCCESS, option: 'foo'
+ })).toEqual({})
+ })
+
+ it('should handle option update error', () => {
+ expect(reduceOption(undefined, {
+ type: optionActions.OPTION_UPDATE_ERROR, option: 'foo', error: 'errorMsg'
+ })).toEqual({
+ foo: {
+ error: 'errorMsg',
+ isUpdating: false,
+ }
+ })
+ })
+})