[web] Improve the code style on tests

This commit is contained in:
Matthew Shao 2017-03-13 22:47:44 +08:00
parent 6d37812928
commit 9ce92b1f43

View File

@ -14,64 +14,73 @@ describe('store reducer', () => {
}) })
it('should handle add action', () => { it('should handle add action', () => {
let state = reduceStore(undefined, {}) let a = {id: 1},
expect(state = reduceStore(state, storeActions.add({id: 1}))).toEqual({ b = {id: 9},
byId: { [1]: {id: 1} }, state = reduceStore(undefined, {})
listIndex: { [1]: 0 }, expect(state = reduceStore(state, storeActions.add(a))).toEqual({
list: [ {id: 1} ], byId: { 1: a },
view: [ {id: 1} ], listIndex: { 1: 0 },
list: [ a ],
view: [ a ],
viewIndex: { 1: 0 }, viewIndex: { 1: 0 },
}) })
expect(reduceStore(state, storeActions.add({id: 9}))).toEqual({ expect(reduceStore(state, storeActions.add(b))).toEqual({
byId: { [1]: {id:1}, [9]: {id:9} }, byId: { 1: a, 9: b },
listIndex: { [1]: 0, [9]: 1 }, listIndex: { 1: 0, 9: 1 },
list: [ {id: 1}, {id: 9} ], list: [ a, b ],
view: [ {id: 1}, {id: 9} ], view: [ a, b ],
viewIndex: { 1: 0, 9: 1 }, viewIndex: { 1: 0, 9: 1 },
}) })
}) })
it('should not add the item with duplicated id', () => { it('should not add the item with duplicated id', () => {
let state = reduceStore(undefined, storeActions.add({id: 1})) let a = {id: 1},
expect(reduceStore(state, storeActions.add({id: 1}))).toEqual(state) state = reduceStore(undefined, storeActions.add(a))
expect(reduceStore(state, storeActions.add(a))).toEqual(state)
}) })
it('should handle update action', () => { it('should handle update action', () => {
let state = reduceStore(undefined, storeActions.add({id: 1, foo: "foo"})) let a = {id: 1, foo: "foo"},
expect(reduceStore(state, storeActions.update({id:1, foo:"foo1"}))).toEqual({ updated = {...a, foo: "bar"},
byId: { [1]: {id: 1, foo: "foo1"} }, state = reduceStore(undefined, storeActions.add(a))
list: [ {id: 1, foo: "foo1" } ], expect(reduceStore(state, storeActions.update(updated))).toEqual({
listIndex: { [1]: 0 }, byId: { 1: updated },
view: [ {id: 1, foo: "foo1"} ], list: [ updated ],
viewIndex: { [1]: 0 }, listIndex: { 1: 0 },
view: [ updated ],
viewIndex: { 1: 0 },
}) })
}) })
it('should handle update action with filter', () => { it('should handle update action with filter', () => {
let state = reduceStore(undefined, storeActions.add({id: 0})) let a = {id: 0},
state = reduceStore(state, storeActions.add({id: 1})) b = {id: 1},
expect(reduceStore(state, storeActions.update({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({ item => {return item.id < 1}))).toEqual({
byId: { [0]: {id: 0}, [1]: {id: 1} }, byId: { 0: a, 1: b },
list: [ {id: 0}, {id: 1} ], list: [ a, b ],
listIndex: { [0]: 0, [1]: 1 }, listIndex: { 0: 0, 1: 1 },
view: [ {id: 0} ], view: [ a ],
viewIndex: { [0]: 0 } viewIndex: { 0: 0 }
}) })
}) })
it('should handle update action with sort', () => { it('should handle update action with sort', () => {
let state = reduceStore(undefined, storeActions.add({id: 2})) let a = {id: 2},
state = reduceStore(state, storeActions.add({id:3})) b = {id: 3},
expect(reduceStore(state, storeActions.update({id: 2}, undefined, 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({ (a, b) => {return b.id - a.id}))).toEqual({
// sort by id in descending order // sort by id in descending order
byId: { [2]: {id: 2}, [3]: {id: 3} }, byId: { 2: a, 3: b },
list: [ {id: 2}, {id: 3} ], list: [ a, b ],
listIndex: {[2]: 0, [3]: 1}, listIndex: {2: 0, 3: 1},
view: [ {id: 3}, {id: 2} ], view: [ b, a ],
viewIndex: { [2]: 1, [3]: 0 }, viewIndex: { 2: 1, 3: 0 },
}) })
}) })
}) })