[英]mocking store.getState()
I want to assert that when a function gets my redux state value using store.getState()
, it does various things based on the conditions of that state. 我想断言,当一个函数使用
store.getState()
获取我的redux状态值时,它会根据该状态的条件执行各种操作。 How am I able to assert / mock what I want the state value to be for certain tests using the store.getState()
method? 如何使用
store.getState()
方法来断言/模拟某些测试的状态值? Thanks. 谢谢。
sampleFunction.js: 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: sampleFunction.test.js:
import sampleFunction from './sampleFunction.js';
test('sampleFunction returns true', () => {
// assert that state.foo.isGood = true
expect(sampleFunction()).toBeTruthy();
});
What you can do to mock your store is 您可以模拟商店的方法是
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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.