question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to Mock WrappedFormUtils

See original GitHub issue

What problem does this feature solve?

Hi, we have a form that is created with Form.create. We then have a component called ContractConditions that is used within that form that contains further FormItems.

The component gets passed the form like this:

<ContractConditions form={this.props.form} />

This works, but I am unsure of how to mock the WrappedFormUtils when trying to test ContractConditions? ContractConditions expects the ‘form’ prop to be set up in the unit test. Do you have examples of how to mock a WrappedFormUtils using Jest and Enzyme?

thanks

What does the proposed API look like?

No api required

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
zongyuan-valecommented, Mar 12, 2018

@benjycui 打扰一下,我在测试由Form.create()封装过的myComponent遇到这样的困惑: 因为myComponent组件内需要用到form的方法(如getFieldDecorator,validateFieldsAndScroll),我直接对myComponent进行测试的话,是需要对props.form内需要用到的进行模拟mock吗?可否举个例子,非常感谢

4reactions
infengcommented, Apr 6, 2017

I test WrappedFormUtils like this:

class TestForm extends React.Component {
  render() {
    const { getFieldDecorator } = this.props.form;
    return (
      <Form>
        <FormItem
          label="name"
        >
          {getFieldDecorator('name', {
            rules: [{
              type: 'string',
              requied: true,
            }],
            initialValue: 'mary',              
          })(
            <input />
          )}
        </FormItem>
      </Form>
    );
  }
}
const WrappedForm = Form.create()(TestForm);
// Test
const wrapper = mount(<WrappedForm />);
wrapper.node.setFieldsValue({
  name: 'tom'
});
expect(wrapper.node.getFieldsValue()).toEqual({name: 'tom'});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking refs in React function component - Stack Overflow
Here is my unit test strategy, use jest.spyOn method spy on the useRef hook. index.tsx : import React from 'react'; export const Comp ......
Read more >
React 单元测试实践- 掘金
一般来说如果报错部分其实和测试无关,可以直接mock 掉,如果比较重要就建议 ... function getForm() { let form: WrappedFormUtils<any>; const Fake ...
Read more >
Antd form typescript - Weebly
TypeScript is a javascript superset that adds many useful features to javascript: Type annotations and compile the type of inference type Erase Interfaces ......
Read more >
编写单元测试,规范很重要
... 写在render方法里面, 需要在table里mock进去不同的数据, 也增加了编码工作。 ... export const filter = (form: WrappedFormUtils) => (e: React.
Read more >
React单元测试实践 - CSDN
一般来说如果报错部分其实和测试无关,可以直接mock 掉,如果比较重要就建议补全对应 ... let form: WrappedFormUtils<any>; const Fake = Form.create()((props: any) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found