测试重新选择选择器
Testing Reselect Selectors
我正在尝试测试一个 react/redux 重新选择选择器,它将过滤一个数组并且 return 只过滤活动客户端。我的问题是 immutableJS。
选择器:
const selectClients = state => state.get('clients', initialState)
const makeSelectInactiveClients = () => createSelector(
makeSelectClients(),
clients => clients.filter(c => c.inactive)
)
测试:
describe('Active and not active client selectors', () => {
const activeClients = [
{ id: 1, name: 'Client 1' },
{ id: 2, name: 'Client 2' },
]
const inactiveClients = [
{ id: 1, name: 'Client 3', inactive: true },
{ id: 2, name: 'Client 4', inactive: true },
]
const clients = [
{ id: 1, name: 'Client 1' },
{ id: 2, name: 'Client 2' },
{ id: 1, name: 'Client 3', inactive: true },
{ id: 2, name: 'Client 4', inactive: true },
]
describe('#makeSelectActiveClients', () => {
const activeClientsSelector = selectors.makeSelectActiveClients()
it('only returns active clients', () => {
const mockedState = fromJS({
clients: {
loading: false,
clients: clients,
},
})
expect(activeClientsSelector(mockedState)).toEqual(activeClients)
})
})
})
减速器:
export default function ClientsReducer(state = initialState, action) {
switch (action.type) {
case FETCH_CLIENTS:
return state.set('loading', true)
case SET_CLIENTS:
return state
.set('loading', false)
.set('clients', action.clients)
default:
return state
}
}
问题是当reducer设置client数组时,设置的是plan JS数组。但是,当在我的测试中使用 ImmutableJS 设置该数组时,它被设置为不可变对象,并且在不使用 get 关键字的情况下没有非活动状态。
我应该像这样在我的 reducer 中调用 toJS 吗?
return state
.set('loading', false)
.set('clients', fromJS(action.clients))
然后在选择器中使用get关键字来访问客户端成员对象的属性?
或者我应该在测试中做一些不同的事情吗?
尝试在测试断言中对 activeClients
调用 fromJS
:
expect(activeClientsSelector(mockedState)).toEqual(fromJS(activeClients))
我正在尝试测试一个 react/redux 重新选择选择器,它将过滤一个数组并且 return 只过滤活动客户端。我的问题是 immutableJS。
选择器:
const selectClients = state => state.get('clients', initialState)
const makeSelectInactiveClients = () => createSelector(
makeSelectClients(),
clients => clients.filter(c => c.inactive)
)
测试:
describe('Active and not active client selectors', () => {
const activeClients = [
{ id: 1, name: 'Client 1' },
{ id: 2, name: 'Client 2' },
]
const inactiveClients = [
{ id: 1, name: 'Client 3', inactive: true },
{ id: 2, name: 'Client 4', inactive: true },
]
const clients = [
{ id: 1, name: 'Client 1' },
{ id: 2, name: 'Client 2' },
{ id: 1, name: 'Client 3', inactive: true },
{ id: 2, name: 'Client 4', inactive: true },
]
describe('#makeSelectActiveClients', () => {
const activeClientsSelector = selectors.makeSelectActiveClients()
it('only returns active clients', () => {
const mockedState = fromJS({
clients: {
loading: false,
clients: clients,
},
})
expect(activeClientsSelector(mockedState)).toEqual(activeClients)
})
})
})
减速器:
export default function ClientsReducer(state = initialState, action) {
switch (action.type) {
case FETCH_CLIENTS:
return state.set('loading', true)
case SET_CLIENTS:
return state
.set('loading', false)
.set('clients', action.clients)
default:
return state
}
}
问题是当reducer设置client数组时,设置的是plan JS数组。但是,当在我的测试中使用 ImmutableJS 设置该数组时,它被设置为不可变对象,并且在不使用 get 关键字的情况下没有非活动状态。
我应该像这样在我的 reducer 中调用 toJS 吗?
return state
.set('loading', false)
.set('clients', fromJS(action.clients))
然后在选择器中使用get关键字来访问客户端成员对象的属性?
或者我应该在测试中做一些不同的事情吗?
尝试在测试断言中对 activeClients
调用 fromJS
:
expect(activeClientsSelector(mockedState)).toEqual(fromJS(activeClients))