我想断言,当一个函数使用store.getState()
获取我的redux状态值时,它会根据该状态的条件执行各种操作。如何使用store.getState()
方法断言/模拟某些测试的状态值?谢谢。
sampleFunction.js:
import { store } from './reduxStore';
const sampleFunction = () => {
const state = store.getState();
let result = false;
if (state.foo.isGood) {
result = true;
}
return result;
};
export default sampleFunction;
sampleFunction.test.js:
import sampleFunction from './sampleFunction.js';
test('sampleFunction returns true', () => {
// assert that state.foo.isGood = true
expect(sampleFunction()).toBeTruthy();
});
答案 0 :(得分:0)
您可以做的模拟商店
import { store } from './reduxStore';
import sampleFunction from './sampleFunction.js';
jest.mock('./reduxStore')
const mockState = {
foo: { isGood: true }
}
// in this point store.getState is going to be mocked
store.getState = () => mockState
test('sampleFunction returns true', () => {
// assert that state.foo.isGood = true
expect(sampleFunction()).toBeTruthy();
});