相关的Redux状态包括一个表示图层的对象数组。
例子:
我有一个名为moveLayerIndex的Redux action:
actions.js
我希望reducer通过交换数组中元素的位置来处理这个action。 reducers/layers.js
/* 我应该在这里放什么才能让下面的测试通过 */
这个测试验证了Redux reducer以不可变的方式交换数组元素。使用Deep-freeze检查初始状态是否没有被改变。你如何让这个测试通过?请在test/reducers/index.js中进行操作。
例子:
let state = [
{ id: 1 }, { id: 2 }, { id: 3 }
]
我有一个名为moveLayerIndex的Redux action:
actions.js
export const moveLayerIndex = (id, destinationIndex) => ({
type: MOVE_LAYER_INDEX,
id,
destinationIndex
})
我希望reducer通过交换数组中元素的位置来处理这个action。 reducers/layers.js
const layers = (state=[], action) => {
switch(action.type) {
case 'MOVE_LAYER_INDEX':
/* 我应该在这里放什么才能让下面的测试通过 */
default:
return state
}
}
这个测试验证了Redux reducer以不可变的方式交换数组元素。使用Deep-freeze检查初始状态是否没有被改变。你如何让这个测试通过?请在test/reducers/index.js中进行操作。
import { expect } from 'chai'
import deepFreeze from'deep-freeze'
const id=1
const destinationIndex=1
it('move position of layer', () => {
const action = actions.moveLayerIndex(id, destinationIndex)
const initialState = [
{
id: 1
},
{
id: 2
},
{
id: 3
}
]
const expectedState = [
{
id: 2
},
{
id: 1
},
{
id: 3
}
]
deepFreeze(initialState)
expect(layers(initialState, action)).to.eql(expectedState)
})
items
数组吗? - Dezitems
,而是应该返回tempArray
,但肯定要修改的是tempArray
,我会修复它。 - markerikson