我已经创建了一个传奇的中间件,其中一个功能需要从使用getFormValues的redux-form中获取值。功能就像:
export function* loadLogin(action) {
var login = yield select(getFormValues('login'));
const { response, error } = yield doAsync(login, loginRequest) || { }
if (response) {
yield put(onLoginSuccess(response))
} else {
yield put(onLoginFailure(error ? error : "TIMEOUT"));
}
}
function* doAsync(func, ...args) {
const { funcResponse, timeout } = race({
funcResponse: call(func, ...args),
timeout: call(delay, TIMEOUT)
});
return funcResponse || { }
}
在构建开发环境上效果很好。但是它在测试中返回未定义。我正在使用Jest和如下测试代码:
describe('When execute process', () => {
it('Should export ON_LOGIN_SUCCESS response', ()=>{
const spy = jest.fn();
const store = createStore(() => ({}));
const tempform = reduxForm({form: "login"})(Login)
const props = {
state:{
form: {
login: {
values: {
username: "foo",
password: "bar"
}
}
}
}
};
const mockedComponent = (
<Provider store={store}>
<tempform {...props}>
<span />
</tempform>
</Provider>
);
const tree = renderer.create(mockedComponent).toJSON();
const gen = loadLogin(tree);
expect(gen.next().value).toEqual(result);
expect(gen.next().value).toEqual(put(onLoginSuccess(response));
});
});
我已推荐Redux-Form : How to mock formValueSelector in Jest尝试将值模拟为全局状态以传递给saga,但仍然无法正常工作。我该如何模拟呢?测试代码有什么问题吗?