aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2017-03-17 15:15:55 +0100
committerGitHub <noreply@github.com>2017-03-17 15:15:55 +0100
commitc0882496e37bbe6985b67ad94bdff72524b5b668 (patch)
tree147da715f0698a11743078c40daa514b7a9dd118
parent253fa950189eebe3fd4941ad246aee0b11e3a98d (diff)
parent6c338c84a6750cb34e287a283a4ff164e0f4a754 (diff)
downloadmitmproxy-c0882496e37bbe6985b67ad94bdff72524b5b668.tar.gz
mitmproxy-c0882496e37bbe6985b67ad94bdff72524b5b668.tar.bz2
mitmproxy-c0882496e37bbe6985b67ad94bdff72524b5b668.zip
Merge pull request #2176 from MatthewShao/jest-dev
[web] Reach 100% coverage for ducks/utils/store.js
-rw-r--r--web/src/js/__tests__/ducks/utils/storeSpec.js105
1 files changed, 95 insertions, 10 deletions
diff --git a/web/src/js/__tests__/ducks/utils/storeSpec.js b/web/src/js/__tests__/ducks/utils/storeSpec.js
index 6bfea2c7..e4742490 100644
--- a/web/src/js/__tests__/ducks/utils/storeSpec.js
+++ b/web/src/js/__tests__/ducks/utils/storeSpec.js
@@ -25,13 +25,25 @@ describe('store reducer', () => {
viewIndex: { 1: 0 },
})
- expect(reduceStore(state, storeActions.add(b))).toEqual({
+ expect(state = reduceStore(state, storeActions.add(b))).toEqual({
byId: { 1: a, 9: b },
listIndex: { 1: 0, 9: 1 },
list: [ a, b ],
view: [ a, b ],
viewIndex: { 1: 0, 9: 1 },
})
+
+ // add item and sort them
+ let c = {id: 0}
+ expect(reduceStore(state, storeActions.add(c, undefined,
+ (a, b) => {return a.id - b.id}))).toEqual({
+ byId: {...state.byId, 0: c },
+ list: [...state.list, c ],
+ listIndex: {...state.listIndex, 0:2},
+ view: [c, ...state.view ],
+ viewIndex: {0: 0, 1: 1, 9: 2}
+
+ })
})
it('should not add the item with duplicated id', () => {
@@ -54,26 +66,32 @@ describe('store reducer', () => {
})
it('should handle update action with filter', () => {
- let a = {id: 0},
- b = {id: 1},
- state = reduceStore(undefined, storeActions.add(a))
- state = reduceStore(state, storeActions.add(b))
- expect(reduceStore(state, storeActions.update(b,
- item => {return item.id < 1}))).toEqual({
+ let a = {id: 0}, b = {id: 1},
+ state = reduceStore(undefined, storeActions.receive([a, b]))
+ state = reduceStore(state, storeActions.update(b,
+ item => {return item.id != 1}))
+ expect(state).toEqual({
byId: { 0: a, 1: b },
list: [ a, b ],
listIndex: { 0: 0, 1: 1 },
view: [ a ],
viewIndex: { 0: 0 }
})
+ expect(reduceStore(state, storeActions.update(b,
+ item => {return item.id != 0}))).toEqual({
+ byId: { 0: a, 1: b },
+ list: [ a, b ],
+ listIndex: { 0: 0, 1: 1 },
+ view: [ a, b ],
+ viewIndex: { 0: 0, 1: 1 }
+ })
})
it('should handle update action with sort', () => {
let a = {id: 2},
b = {id: 3},
- state = reduceStore(undefined, storeActions.add(a))
- state = reduceStore(state, storeActions.add(b))
- expect(reduceStore(state, storeActions.update(a, undefined,
+ state = reduceStore(undefined, storeActions.receive([a, b]))
+ expect(reduceStore(state, storeActions.update(b, undefined,
(a, b) => {return b.id - a.id}))).toEqual({
// sort by id in descending order
byId: { 2: a, 3: b },
@@ -82,5 +100,72 @@ describe('store reducer', () => {
view: [ b, a ],
viewIndex: { 2: 1, 3: 0 },
})
+
+ let state1 = reduceStore(undefined, storeActions.receive([b, a]))
+ expect(reduceStore(state1, storeActions.update(b, undefined,
+ (a, b) => {return a.id - b.id}))).toEqual({
+ // sort by id in ascending order
+ byId: { 2: a, 3: b },
+ list: [ b, a ],
+ listIndex: {2: 1, 3: 0},
+ view: [ a, b ],
+ viewIndex: { 2: 0, 3: 1 },
+ })
+ })
+
+ it('should set filter', () => {
+ let a = { id: 1 },
+ b = { id: 2 },
+ state = reduceStore(undefined, storeActions.receive([a, b]))
+ expect(reduceStore(state, storeActions.setFilter(
+ item => {return item.id != 1}
+ ))).toEqual({
+ byId: { 1 :a, 2: b },
+ list: [ a, b ],
+ listIndex:{ 1: 0, 2: 1},
+ view: [ b ],
+ viewIndex: { 2: 0 },
+ })
+ })
+
+ it('should set sort', () => {
+ let a = { id: 1 },
+ b = { id: 2 },
+ state = reduceStore(undefined, storeActions.receive([a, b]))
+ expect(reduceStore(state, storeActions.setSort(
+ (a, b) => { return b.id - a.id }
+ ))).toEqual({
+ byId: { 1: a, 2: b },
+ list: [ a, b ],
+ listIndex: {1: 0, 2: 1},
+ view: [ b, a ],
+ viewIndex: { 1: 1, 2: 0 },
+ })
+ })
+
+ it('should handle remove action', () => {
+ let a = { id: 1 }, b = { id: 2},
+ state = reduceStore(undefined, storeActions.receive([a, b]))
+ expect(reduceStore(state, storeActions.remove(1))).toEqual({
+ byId: { 2: b },
+ list: [ b ],
+ listIndex: { 2: 0 },
+ view: [ b ],
+ viewIndex: { 2: 0 },
+ })
+
+ expect(reduceStore(state, storeActions.remove(3))).toEqual(state)
+ })
+
+ it('should handle receive list', () => {
+ let a = { id: 1 }, b = { id: 2 },
+ list = [ a, b ]
+ expect(reduceStore(undefined, storeActions.receive(list))).toEqual({
+ byId: { 1: a, 2: b },
+ list: [ a, b ],
+ listIndex: {1: 0, 2: 1},
+ view: [ a, b ],
+ viewIndex: { 1: 0, 2: 1 },
+ })
})
})