[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', () => {
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 },
})
})
})