我有以下两项测试
import {put, select, takeEvery} from 'redux-saga/effects';
import {combineReducers} from 'redux';
export default class SessionReducer {
public static readonly _initialState: any = {
disconnectCounts: {},
};
public static reducer(state: any = SessionReducer._initialState, action: any): any {
// console.log('reducer', action);
let newState: any;
switch (action.type) {
case 'DEVICE_DISCONNECTED':
newState = {
...state,
};
if (!newState.disconnectCounts[action.value]) newState.disconnectCounts[action.value] = 0;
newState.disconnectCounts[action.value]++;
newState.error = {
type: 'DEVICE_DISCONNECTED',
utc: 1,
};
return newState;
default:
return state;
}
}
}
export function* errorHandler() {
yield takeEvery(['DEVICE_DISCONNECTED'], function* (action: any) {
let state = yield select();
console.log('*********', state);
if (state.session.disconnectCounts[action.value] > 1) {
yield put({
type: 'WATCH_REBOOT_REQUEST',
});
// state.session.disconnectCounts[action.value] = 0
}
});
}
let action = {type: 'DEVICE_DISCONNECTED', value: '111'};
describe('Handles Error States and Transitions', () => {
test('Sends watch reboot request when disconnection count threshold met', () => {
return expectSaga(errorHandler)
.withReducer(
combineReducers({
session: SessionReducer.reducer,
}),
{session: SessionReducer._initialState},
)
.dispatch(action)
.dispatch(action)
.put({type: 'WATCH_REBOOT_REQUEST'})
.run()
.then((result: {storeState: any}) => {
debugger;
let session = result.storeState.session;
expect(session.disconnectCounts[action.value]).toBe(2); // values for error are tested in reducer test
expect(session.error).toBeTruthy(); // values for error are tested in reducer test
});
});
test('Does not send WATCH_REBOOT_REQUEST when threshold not met', () => {
return expectSaga(errorHandler)
.withReducer(
combineReducers({
session: SessionReducer.reducer,
}),
{session: SessionReducer._initialState},
)
.dispatch(action)
.run()
.then((result: {storeState: any}) => {
let session = result.storeState.session;
expect(session.disconnectCounts[action.value]).toBe(1); // values for error are tested in reducer test
// expect(session.currentScreen).toEqual('actionRequiredIdleScreen');
});
});
});如果您独立运行每个测试,我使用了.only,它们通过了,但是没有.only就运行它们,而第二个测试在disconnectCounts中总是失败w/太多值。
Handles Error States and Transitions
✓ Sends watch reboot request when disconnection count threshold met (263 ms)
✕ Does not send WATCH_REBOOT_REQUEST when threshold not met (258 ms)
● Handles Error States and Transitions › Does not send WATCH_REBOOT_REQUEST when threshold not met
expect(received).toBe(expected) // Object.is equality
Expected: 1
Received: 3
76 | .then((result: {storeState: any}) => {
77 | let session = result.storeState.session;
> 78 | expect(session.disconnectCounts[action.value]).toBe(1); // values for error are tested in reducer test
| ^
79 | // expect(session.currentScreen).toEqual('actionRequiredIdleScreen');
80 | });
81 | });
at __tests__/sagas/sagaStateIssue.ts:78:64
at tryCallOne (node_modules/promise/lib/core.js:37:12)
at node_modules/promise/lib/core.js:123:15
at flush (node_modules/asap/raw.js:50:29)我错过了什么?
发布于 2021-10-28 22:36:33
把还原剂和状态放在一个类中是一种反模式的还原.
const initialState = () => ({ disconnectCounts: {} });
const reducer = (state: any = initialState(), action: any): any => {保持initialState的单个引用,最好有一个返回新实例的函数
https://codesandbox.io/s/proud-morning-0w4wu?file=/src/testy.test.ts:175-182
下面是一个运行测试的沙箱
发布于 2021-10-28 21:44:06
我认为问题在于,两个测试都使用相同的SessionReducer._initialState引用。当您将它传递到withReducer中的状态时,它不会以任何方式被克隆,因此您最终会在内存中使用相同的对象。
有很多方法可以修复它,例如,您可以使用一个方法而不是一个属性来创建初始对象:
_initialState = () => ({disconnectCounts: {}})
// ...
.withReducer(
combineReducers({
session: SessionReducer.reducer,
}),
{session: SessionReducer._initialState()},
)或者,您可以在测试中自己深入克隆对象。
const deepClone = obj => JSON.parse(JSON.stringify(obj))
// ...
.withReducer(
combineReducers({
session: SessionReducer.reducer,
}),
{session: deepClone(SessionReducer._initialState)},
)https://stackoverflow.com/questions/69550512
复制相似问题