【问题标题】:Testing ReduxForm with Enzyme用 Enzyme 测试 ReduxForm
【发布时间】:2020-08-10 17:55:03
【问题描述】:

如何测试我的 Redux-Form 组件?
我有这个测试:

describe('<SignIn />', () => {
    let wrapper, store;
    const props = {isWidthSmallThan750: true, classes: {}};

    beforeEach(() => {
        store = mockStore({});
        wrapper = mountComponent(SignUp, props); //equal to mount(Component)
    });

    afterEach(() => {
        wrapper = null;
        store = null;
    });

    it('snapshot testing', () => {
        expect(componentToJSON(SignIn, props)).toMatchSnapshot(); //equal to render.create(Component).toJSON
    })

    it('when form submitted, these actions may happen', () => {
        const form = wrapper.find('form');
        wrapper.find('input').at(0).simulate('change', {target: {value: 'Tigran'}});
        wrapper.find('input').at(1).simulate('change', {target: {value: 'Tigran'}});
        expect(wrapper.find('input').at(0).props().value).toBe('Tigran');
        expect(wrapper.find('input').at(1).props().value).toBe('Tigran');
        form.simulate('submit');
        expect(store.getActions()).toEqual(null);
    })
});

但是当 form.simulate 被调用时,我没有得到我的 onSubmit 函数调用。
我该如何解决这个问题?

【问题讨论】:

    标签: reactjs redux jestjs enzyme redux-form


    【解决方案1】:

    尝试wrapper.find('form').simulate('submit'); 而不是form.simulate('submit')

    酶缓存结果的最新版本,因此它的状态可能是陈旧的。

    也可以试试wrapper.find('form').props().submit()。我强烈建议你不要使用模拟并直接调用它,模拟是片状的。

    【讨论】:

    • 如果 wrapper.find('form').props().submit() 抛出错误。
    • onSubmit 不是 submit()。不知道为什么它不起作用。您的代码中的其他地方一定是错误的
    • props() 不是函数
    猜你喜欢
    • 2017-11-01
    • 2019-07-01
    • 2018-09-14
    • 2016-10-08
    • 2018-12-16
    • 2018-09-08
    • 2017-02-12
    • 2020-08-07
    • 2017-11-26
    相关资源
    最近更新 更多