From 6d37812928bfbd269e0c09ba97673fd92aef1f61 Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Mon, 13 Mar 2017 15:56:14 +0800 Subject: [PATCH 1/2] [web] Add tests for ducks/utils/store.js --- web/src/js/__tests__/ducks/utils/storeSpec.js | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 web/src/js/__tests__/ducks/utils/storeSpec.js diff --git a/web/src/js/__tests__/ducks/utils/storeSpec.js b/web/src/js/__tests__/ducks/utils/storeSpec.js new file mode 100644 index 000000000..145272e92 --- /dev/null +++ b/web/src/js/__tests__/ducks/utils/storeSpec.js @@ -0,0 +1,77 @@ +jest.unmock('../../../ducks/utils/store') + +import reduceStore, * as storeActions from '../../../ducks/utils/store' + +describe('store reducer', () => { + it('should return initial state', () => { + expect(reduceStore(undefined, {})).toEqual({ + byId: {}, + list: [], + listIndex: {}, + view: [], + viewIndex: {}, + }) + }) + + it('should handle add action', () => { + let state = reduceStore(undefined, {}) + expect(state = reduceStore(state, storeActions.add({id: 1}))).toEqual({ + byId: { [1]: {id: 1} }, + listIndex: { [1]: 0 }, + list: [ {id: 1} ], + view: [ {id: 1} ], + viewIndex: { 1: 0 }, + }) + + expect(reduceStore(state, storeActions.add({id: 9}))).toEqual({ + byId: { [1]: {id:1}, [9]: {id:9} }, + listIndex: { [1]: 0, [9]: 1 }, + list: [ {id: 1}, {id: 9} ], + view: [ {id: 1}, {id: 9} ], + viewIndex: { 1: 0, 9: 1 }, + }) + }) + + it('should not add the item with duplicated id', () => { + let state = reduceStore(undefined, storeActions.add({id: 1})) + expect(reduceStore(state, storeActions.add({id: 1}))).toEqual(state) + }) + + it('should handle update action', () => { + let state = reduceStore(undefined, storeActions.add({id: 1, foo: "foo"})) + expect(reduceStore(state, storeActions.update({id:1, foo:"foo1"}))).toEqual({ + byId: { [1]: {id: 1, foo: "foo1"} }, + list: [ {id: 1, foo: "foo1" } ], + listIndex: { [1]: 0 }, + view: [ {id: 1, foo: "foo1"} ], + viewIndex: { [1]: 0 }, + }) + }) + + it('should handle update action with filter', () => { + let state = reduceStore(undefined, storeActions.add({id: 0})) + state = reduceStore(state, storeActions.add({id: 1})) + expect(reduceStore(state, storeActions.update({id:1}, + item => {return item.id < 1}))).toEqual({ + byId: { [0]: {id: 0}, [1]: {id: 1} }, + list: [ {id: 0}, {id: 1} ], + listIndex: { [0]: 0, [1]: 1 }, + view: [ {id: 0} ], + viewIndex: { [0]: 0 } + }) + }) + + it('should handle update action with sort', () => { + let state = reduceStore(undefined, storeActions.add({id: 2})) + state = reduceStore(state, storeActions.add({id:3})) + expect(reduceStore(state, storeActions.update({id: 2}, undefined, + (a, b) => {return b.id - a.id}))).toEqual({ + // sort by id in descending order + byId: { [2]: {id: 2}, [3]: {id: 3} }, + list: [ {id: 2}, {id: 3} ], + listIndex: {[2]: 0, [3]: 1}, + view: [ {id: 3}, {id: 2} ], + viewIndex: { [2]: 1, [3]: 0 }, + }) + }) +}) From 9ce92b1f4318a7a283c54d2ff4e46605654fbff4 Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Mon, 13 Mar 2017 22:47:44 +0800 Subject: [PATCH 2/2] [web] Improve the code style on tests --- web/src/js/__tests__/ducks/utils/storeSpec.js | 81 ++++++++++--------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/web/src/js/__tests__/ducks/utils/storeSpec.js b/web/src/js/__tests__/ducks/utils/storeSpec.js index 145272e92..6bfea2c79 100644 --- a/web/src/js/__tests__/ducks/utils/storeSpec.js +++ b/web/src/js/__tests__/ducks/utils/storeSpec.js @@ -14,64 +14,73 @@ describe('store reducer', () => { }) it('should handle add action', () => { - let state = reduceStore(undefined, {}) - expect(state = reduceStore(state, storeActions.add({id: 1}))).toEqual({ - byId: { [1]: {id: 1} }, - listIndex: { [1]: 0 }, - list: [ {id: 1} ], - view: [ {id: 1} ], + let a = {id: 1}, + b = {id: 9}, + state = reduceStore(undefined, {}) + expect(state = reduceStore(state, storeActions.add(a))).toEqual({ + byId: { 1: a }, + listIndex: { 1: 0 }, + list: [ a ], + view: [ a ], viewIndex: { 1: 0 }, }) - expect(reduceStore(state, storeActions.add({id: 9}))).toEqual({ - byId: { [1]: {id:1}, [9]: {id:9} }, - listIndex: { [1]: 0, [9]: 1 }, - list: [ {id: 1}, {id: 9} ], - view: [ {id: 1}, {id: 9} ], + expect(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 }, }) }) it('should not add the item with duplicated id', () => { - let state = reduceStore(undefined, storeActions.add({id: 1})) - expect(reduceStore(state, storeActions.add({id: 1}))).toEqual(state) + let a = {id: 1}, + state = reduceStore(undefined, storeActions.add(a)) + expect(reduceStore(state, storeActions.add(a))).toEqual(state) }) it('should handle update action', () => { - let state = reduceStore(undefined, storeActions.add({id: 1, foo: "foo"})) - expect(reduceStore(state, storeActions.update({id:1, foo:"foo1"}))).toEqual({ - byId: { [1]: {id: 1, foo: "foo1"} }, - list: [ {id: 1, foo: "foo1" } ], - listIndex: { [1]: 0 }, - view: [ {id: 1, foo: "foo1"} ], - viewIndex: { [1]: 0 }, + let a = {id: 1, foo: "foo"}, + updated = {...a, foo: "bar"}, + state = reduceStore(undefined, storeActions.add(a)) + expect(reduceStore(state, storeActions.update(updated))).toEqual({ + byId: { 1: updated }, + list: [ updated ], + listIndex: { 1: 0 }, + view: [ updated ], + viewIndex: { 1: 0 }, }) }) it('should handle update action with filter', () => { - let state = reduceStore(undefined, storeActions.add({id: 0})) - state = reduceStore(state, storeActions.add({id: 1})) - expect(reduceStore(state, storeActions.update({id:1}, + 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({ - byId: { [0]: {id: 0}, [1]: {id: 1} }, - list: [ {id: 0}, {id: 1} ], - listIndex: { [0]: 0, [1]: 1 }, - view: [ {id: 0} ], - viewIndex: { [0]: 0 } + byId: { 0: a, 1: b }, + list: [ a, b ], + listIndex: { 0: 0, 1: 1 }, + view: [ a ], + viewIndex: { 0: 0 } }) }) it('should handle update action with sort', () => { - let state = reduceStore(undefined, storeActions.add({id: 2})) - state = reduceStore(state, storeActions.add({id:3})) - expect(reduceStore(state, storeActions.update({id: 2}, undefined, + 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, (a, b) => {return b.id - a.id}))).toEqual({ // sort by id in descending order - byId: { [2]: {id: 2}, [3]: {id: 3} }, - list: [ {id: 2}, {id: 3} ], - listIndex: {[2]: 0, [3]: 1}, - view: [ {id: 3}, {id: 2} ], - viewIndex: { [2]: 1, [3]: 0 }, + byId: { 2: a, 3: b }, + list: [ a, b ], + listIndex: {2: 0, 3: 1}, + view: [ b, a ], + viewIndex: { 2: 1, 3: 0 }, }) }) })