如何比较两个 redux store 状态?
How to compare two redux store states?
我正在为我的 reducer 编写测试,我想比较发送前的状态和发送后的状态 - 实际上是 'substract' 之前的状态和之后的状态
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: 'blabla@blabla.com' }));
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: 'blabla@blabla.com' }));
const after = store.getState();
expect(addedDiff(before, after)).toEqual({});
expect(deletedDiff(before, after)).toEqual({});
expect(updatedDiff(before, after)).toEqual({
login: { loginToken: { email: 'blabla@blabla.com' } },
customer: { email: 'blabla@blabla.com' },
});
});
我正在为我的 reducer 编写测试,我想比较发送前的状态和发送后的状态 - 实际上是 'substract' 之前的状态和之后的状态
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: 'blabla@blabla.com' }));
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: 'blabla@blabla.com' }));
const after = store.getState();
expect(addedDiff(before, after)).toEqual({});
expect(deletedDiff(before, after)).toEqual({});
expect(updatedDiff(before, after)).toEqual({
login: { loginToken: { email: 'blabla@blabla.com' } },
customer: { email: 'blabla@blabla.com' },
});
});