aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/ducks/utils
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2016-06-28 23:16:18 -0700
committerGitHub <noreply@github.com>2016-06-28 23:16:18 -0700
commitb7430c0775fd14df779b6d0a27f189d3bfbd8194 (patch)
tree2b1db1801c7de989080a06bd886a680d304c88e9 /web/src/js/__tests__/ducks/utils
parent3b53d3e393f30879420b4407583c42bb1f51563e (diff)
parent37c2b47c26dea9b143b2720761cdce21dafd3884 (diff)
downloadmitmproxy-b7430c0775fd14df779b6d0a27f189d3bfbd8194.tar.gz
mitmproxy-b7430c0775fd14df779b6d0a27f189d3bfbd8194.tar.bz2
mitmproxy-b7430c0775fd14df779b6d0a27f189d3bfbd8194.zip
Merge pull request #1296 from gzzhanghao/test
[web] add tests for ducks/utils
Diffstat (limited to 'web/src/js/__tests__/ducks/utils')
-rw-r--r--web/src/js/__tests__/ducks/utils/list.js63
-rw-r--r--web/src/js/__tests__/ducks/utils/view.js154
2 files changed, 217 insertions, 0 deletions
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]))
+ }
+}