我正在为我的减速器编写测试,并希望在发送之前将状态与状态进行比较 - 实际上是“状态之后”的状态
describe('UpdateAccountData', () => {
let store;
describe('Customer', () => {
beforeEach(() => {
store = createStore(reducers, customerData, applyMiddleware(sagaMiddleware));
});
it('debug store', () => {
console.log(store.getState());
});
it('dispatch change', () => {
//need to deep copy old state here
store.dispatch(updateStoredCustomerDetails({ email: '[email protected]' }));
console.log(store.getState());
//need to substract old state from new state to check that only email has changed and nothing else
});
});
您可以像这样使用equals方法:
oldStore.equals(newStore); //Returns boolean
我最终使用了深层对象差异库,这是我的例子 - 我正在测试除了两次出现的电子邮件之外没有其他变化:
import { updatedDiff, deletedDiff, addedDiff } from 'deep-object-diff';
.....
it('updates customer email', () => {
const before = store.getState();
store.dispatch(updateStoredCustomerDetails({ email: '[email protected]' }));
const after = store.getState();
expect(addedDiff(before, after)).toEqual({});
expect(deletedDiff(before, after)).toEqual({});
expect(updatedDiff(before, after)).toEqual({
login: { loginToken: { email: '[email protected]' } },
customer: { email: '[email protected]' },
});
});