我是Jest的新手,redux。我的redux表单(在Typescript中)如下:
let SelectorForms = reduxForm({
form: 'selector-form',
validate,
})(SelectorForms);
SelectorForms = connect(state => (
{initialValues: {code: state.code,
region:state.region}}))(SelectorForms);
export default SelectorForms
我正在尝试编写一个测试来验证表单名称:
import * as React from "react";
import {mount} from "enzyme";
import thunk from "redux-thunk";
import {Provider} from "react-redux";
import SelectorForms from "./SelectorForm";
import {mockClient} from "./mockClientData";
import configureStore from "redux-mock-store";
const mockStore = configureStore([thunk]);
const store = mockStore({
selectedClient: {
clientInfo: mockClient
},
cimplicityQueryStringData:{Code:'1234',region:'REG'}
});
let component;
describe("Header", () => {
beforeEach(() => {
component = mount(
<Provider store={store}>
<SelectorForms />
</Provider>
);
});
it("should have selector-form", () => {
console.log(component);
expect(component.find("selector-form").length).toBe(1);
});
});
&#13;
然而,测试失败了。我需要知道如何获取表单名称。
我应该在连接状态之前将其导出吗?如果是这样的话?
答案 0 :(得分:1)
您应该导出reduxForm选项对象。
export const selectorFormOptions = {
form: 'selector-form',
validate,
};
let SelectorForms = reduxForm(selectorFormOptions)(SelectorForms);
然后你可以import { selectorFormOptions }
和expect(selectorFormOptions).toEqual(...)
。
答案 1 :(得分:0)
这也有效:
export const formName = 'selector-form';
let SelectorForms = reduxForm({
form: formName,
validate,
})(SelectorForms);
SelectorForms = connect(state => (
{initialValues: {code: state.code,
region:state.region}}))(SelectorForms);
export default SelectorForms
测试如下:
import * as React from "react";
import {formName} from "../SelectorForm";
describe("Client Selector Form", () => {
it("form name should be selector-form", () => {
expect(formName).toBe('selector-form');
});
});