diff options
author | Jason <jason.daurus@gmail.com> | 2016-06-27 18:36:21 +0800 |
---|---|---|
committer | Jason <jason.daurus@gmail.com> | 2016-06-27 18:42:20 +0800 |
commit | 37c2b47c26dea9b143b2720761cdce21dafd3884 (patch) | |
tree | 5ec4f45e38e69b3af96ecc4372d70cda5bbe0c83 /web/src/js/__tests__ | |
parent | 28531a4dd7acd6dfd32dafca99dcad286041003b (diff) | |
download | mitmproxy-37c2b47c26dea9b143b2720761cdce21dafd3884.tar.gz mitmproxy-37c2b47c26dea9b143b2720761cdce21dafd3884.tar.bz2 mitmproxy-37c2b47c26dea9b143b2720761cdce21dafd3884.zip |
[web] add tests for utils
Diffstat (limited to 'web/src/js/__tests__')
-rw-r--r-- | web/src/js/__tests__/ducks/ui.js | 10 | ||||
-rw-r--r-- | web/src/js/__tests__/ducks/utils/list.js | 63 | ||||
-rw-r--r-- | web/src/js/__tests__/ducks/utils/view.js | 154 | ||||
-rw-r--r-- | web/src/js/__tests__/utils.js | 15 |
4 files changed, 222 insertions, 20 deletions
diff --git a/web/src/js/__tests__/ducks/ui.js b/web/src/js/__tests__/ducks/ui.js index 44a71aa3..2388a9ad 100644 --- a/web/src/js/__tests__/ducks/ui.js +++ b/web/src/js/__tests__/ducks/ui.js @@ -1,9 +1,9 @@ jest.unmock("../../ducks/ui"); // @todo fix it ( this is why I don't like to add tests until our architecture is stable :P ) -jest.unmock("../../ducks/flows"); +jest.unmock("../../ducks/views/main"); import reducer, { setActiveMenu } from '../../ducks/ui'; -import { SELECT_FLOW } from '../../ducks/flows'; +import { SELECT } from '../../ducks/views/main'; describe("ui reducer", () => { it("should return the initial state", () => { @@ -14,21 +14,21 @@ describe("ui reducer", () => { }), it("should change the state to Start when deselecting a flow and we a currently at the flow tab", () => { expect(reducer({activeMenu: 'Flow'}, - { type: SELECT_FLOW, + { type: SELECT, currentSelection: '1', flowId : undefined })).toEqual({ activeMenu: 'Start'}) }), it("should change the state to Flow when we selected a flow and no flow was selected before", () => { expect(reducer({activeMenu: 'Start'}, - { type: SELECT_FLOW, + { type: SELECT, currentSelection: undefined, flowId : '1' })).toEqual({ activeMenu: 'Flow'}) }), it("should not change the state to Flow when OPTIONS tab is selected and we selected a flow and a flow as selected before", () => { expect(reducer({activeMenu: 'Options'}, - { type: SELECT_FLOW, + { type: SELECT, currentSelection: '1', flowId : '2' })).toEqual({ activeMenu: 'Options'}) diff --git a/web/src/js/__tests__/ducks/utils/list.js b/web/src/js/__tests__/ducks/utils/list.js new file mode 100644 index 00000000..8cae91ec --- /dev/null +++ b/web/src/js/__tests__/ducks/utils/list.js @@ -0,0 +1,63 @@ +jest.unmock('lodash') +jest.unmock('../../../ducks/utils/list') + +import reduce, * as list from '../../../ducks/utils/list' + +describe('list reduce', () => { + + it('should add item', () => { + const state = createState([ + { id: 1 }, + { id: 2 } + ]) + const result = createState([ + { id: 1 }, + { id: 2 }, + { id: 3 } + ]) + expect(reduce(state, list.add({ id: 3 }))).toEqual(result) + }) + + it('should update item', () => { + const state = createState([ + { id: 1, val: 1 }, + { id: 2, val: 2 } + ]) + const result = createState([ + { id: 1, val: 1 }, + { id: 2, val: 3 } + ]) + expect(reduce(state, list.update(2, { id: 2, val: 3 }))).toEqual(result) + }) + + it('should remove item', () => { + const state = createState([ + { id: 1 }, + { id: 2 } + ]) + const result = createState([ + { id: 1 } + ]) + result.byId[2] = result.indexOf[2] = null + expect(reduce(state, list.remove(2))).toEqual(result) + }) + + it('should replace all items', () => { + const state = createState([ + { id: 1 }, + { id: 2 } + ]) + const result = createState([ + { id: 1 } + ]) + expect(reduce(state, list.receive([{ id: 1 }]))).toEqual(result) + }) +}) + +function createState(items) { + return { + data: items, + byId: _.fromPairs(items.map((item, index) => [item.id, item])), + indexOf: _.fromPairs(items.map((item, index) => [item.id, index])) + } +} diff --git a/web/src/js/__tests__/ducks/utils/view.js b/web/src/js/__tests__/ducks/utils/view.js new file mode 100644 index 00000000..1b07f723 --- /dev/null +++ b/web/src/js/__tests__/ducks/utils/view.js @@ -0,0 +1,154 @@ +jest.unmock('../../../ducks/utils/view') +jest.unmock('lodash') + +import reduce, * as view from '../../../ducks/utils/view' +import _ from 'lodash' + +describe('view reduce', () => { + + it('should filter items', () => { + const state = createState([ + { id: 1 }, + { id: 2 } + ]) + const result = createState([ + { id: 1 } + ]) + expect(reduce(state, view.updateFilter(state, item => item.id === 1))).toEqual(result) + }) + + it('should sort items', () => { + const state = createState([ + { id: 1 }, + { id: 2 } + ]) + const result = createState([ + { id: 2 }, + { id: 1 } + ]) + expect(reduce(state, view.updateSort((a, b) => b.id - a.id))).toEqual(result) + }) + + it('should add item', () => { + const state = createState([ + { id: 1 }, + { id: 2 } + ]) + const result = createState([ + { id: 1 }, + { id: 2 }, + { id: 3 } + ]) + expect(reduce(state, view.add({ id: 3 }))).toEqual(result) + }) + + it('should add item in place', () => { + const state = createState([ + { id: 1 } + ]) + const result = createState([ + { id: 3 }, + { id: 1 } + ]) + expect(reduce(state, view.add({ id: 3 }, undefined, (a, b) => b.id - a.id))).toEqual(result) + }) + + it('should filter added item', () => { + const state = createState([ + { id: 1 } + ]) + const result = createState([ + { id: 1 } + ]) + expect(reduce(state, view.add({ id: 3 }, i => i.id === 1))).toEqual(result) + }) + + it('should update item', () => { + const state = createState([ + { id: 1, val: 1 }, + { id: 2, val: 2 } + ]) + const result = createState([ + { id: 1, val: 1 }, + { id: 2, val: 3 } + ]) + expect(reduce(state, view.update(2, { id: 2, val: 3 }))).toEqual(result) + }) + + it('should sort updated item', () => { + const state = createState([ + { id: 1, val: 1 }, + { id: 2, val: 2 } + ]) + const result = createState([ + { id: 2, val: 3 }, + { id: 1, val: 1 } + ]) + expect(reduce(state, view.update(2, { id: 2, val: 3 }, undefined, (a, b) => b.id - a.id))).toEqual(result) + }) + + it('should filter updated item', () => { + const state = createState([ + { id: 1, val: 1 }, + { id: 2, val: 2 } + ]) + const result = createState([ + { id: 1, val: 1 } + ]) + result.indexOf[2] = null + expect(reduce(state, view.update(2, { id: 2, val: 3 }, i => i.id === i.val))).toEqual(result) + }) + + it('should remove item', () => { + const state = createState([ + { id: 1 }, + { id: 2 } + ]) + const result = createState([ + { id: 1 } + ]) + result.indexOf[2] = null + expect(reduce(state, view.remove(2))).toEqual(result) + }) + + it('should replace items', () => { + const state = createState([ + { id: 1 }, + { id: 2 } + ]) + const result = createState([ + { id: 1 } + ]) + expect(reduce(state, view.receive({ data: [{ id: 1 }] }))).toEqual(result) + }) + + it('should sort received items', () => { + const state = createState([ + { id: 1 }, + { id: 2 } + ]) + const result = createState([ + { id: 2 }, + { id: 1 } + ]) + expect(reduce(state, view.receive({ data: [{ id: 1 }, { id: 2 }] }, undefined, (a, b) => b.id - a.id))).toEqual(result) + }) + + it('should filter received', () => { + const state = createState([ + { id: 1 }, + { id: 2 } + ]) + const result = createState([ + { id: 1 } + ]) + expect(reduce(state, view.receive({ data: [{ id: 1 }, { id: 2 }] }, i => i.id === 1))).toEqual(result) + }) +}) + +function createState(items) { + return { + data: items, + indexOf: _.fromPairs(items.map((item, index) => [item.id, index])) + } +} diff --git a/web/src/js/__tests__/utils.js b/web/src/js/__tests__/utils.js deleted file mode 100644 index b216d7d4..00000000 --- a/web/src/js/__tests__/utils.js +++ /dev/null @@ -1,15 +0,0 @@ -jest.unmock("../utils"); - -import {formatSize} from "../utils" - -describe("utils", () => { - it("formatSize", () => { - expect(formatSize(1024)).toEqual("1kb"); - expect(formatSize(0)).toEqual("0"); - expect(formatSize(10)).toEqual("10b"); - expect(formatSize(1025)).toEqual("1.0kb"); - expect(formatSize(1024*1024)).toEqual("1mb"); - expect(formatSize(1024*1024+1)).toEqual("1.0mb"); - }); -}); - |