Jim*_*mmy 8 javascript jestjs redux
我想断言,当一个函数使用 获取我的 redux 状态值时store.getState(),它会根据该状态的条件执行各种操作。我如何能够断言/模拟我希望使用该store.getState()方法进行某些测试的状态值?谢谢。
示例函数.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;
Run Code Online (Sandbox Code Playgroud)
sampleFunction.test.js:
import sampleFunction from './sampleFunction.js';
test('sampleFunction returns true', () => {
// assert that state.foo.isGood = true
expect(sampleFunction()).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)
Arn*_*las 12
你可以做些什么来模拟你的商店
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();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4330 次 |
| 最近记录: |