我正在使用 redux 存储库的 reduxsauce 库,并且我想对其中的单个 redux 存储进行单元测试。还原文件:
import { createReducer, createActions } from 'reduxsauce'
import Immutable from 'seamless-immutable'
/* ------------- Types and Action Creators ------------- */
const { Types, Creators } = createActions({
getLanguage: [],
setLanguage: ['language']
})
export const LanguageTypes = Types
export default Creators
/* ------------- Initial State ------------- */
export const INITIAL_STATE = Immutable({
language: "en"
})
/* ------------- Reducers ------------- */
export const getLanguage = (state: Object, {}: Object) => {
return state.merge({})
}
export const setLanguage = (state: Object, { language }: Object) => {
return state.merge({ language })
}
/* ------------- Hookup Reducers To Types ------------- */
export const reducer = createReducer(INITIAL_STATE, {
[Types.SET_LANGUAGE]: setLanguage,
[Types.GET_LANGUAGE]: getLanguage,
})
考试:
import * as actions from '../../../redux/LanguageRedux'
import * as types from '../../../redux/LanguageRedux'
describe('Language redux ', () => {
it('should have default language ', () => {
expect(actions.INITIAL_STATE.language).toEqual("en")
}),
it('should be able to set the language', () => {
// I know the calls below are not tests but still its relevant with the error
actions.getLanguage()
actions.setLanguage()
})
})
错误:
● Language redux › should be able to set the language
TypeError: Cannot destructure 'undefined' or 'null'.
21 |
22 |
> 23 | export const getLanguage = (state: Object, {}: Object) => {
| ^
24 | return state.merge({})
25 | }
26 |
at Object.getLanguage (src/redux/LanguageRedux.js:23:28)
at Object.getLanguage (src/__tests__/src/redux/LanguageRedux.js:9:13)
现在,我将存储配置在不同的文件中,但减速器组合在不同的文件中:
import { combineReducers } from 'redux'
import configureStore from './CreateStore'
import rootSaga from '../sagas'
export default () => {
/* ------------- Assemble The Reducers ------------- */
const rootReducer = combineReducers({
language: require('./LanguageRedux').reducer
})
return configureStore(rootReducer, rootSaga)
}
任何人都知道如何测试 redux 操作等。使用普通的 redux,我可以找到很多文章,但使用 reduxsauce 库,我似乎找不到任何东西。请问有什么线索吗?