最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jest test fails with refs and Form - Stack Overflow

programmeradmin2浏览0评论

I have a search bar ponent which looks like:

  render () {
    const { onChangeTextInput } = this.props
    return (
      <Form
        name="searchForm"
        ref={(ref) => {
          this.searchForm = ref
        }}
      >
        <TextInput
          noSpacing
          placeholder={t('search')}
          name="search"
          validate="text"
          icon={this.icon}
          onChangeTextCallback={() => {
            onChangeTextInput(this.searchForm.values)
          }}
        />
        )
      </Form>
    )
  }
}

I am using a shallow render and jest to run a unit test to test the following scenario"

  1. User types a character in text input
  2. a callback method gets fired which provides the user with the text as an argument.

The test I am running is stated as:

 test('SearchBar returns value on text change', () => {
        const onChangeFunction = jest.fn()
        const obj = shallow(
          <SearchBar onChangeTextInput={onChangeFunction} />
        )
        obj.find('TextInput').props().onChangeTextCallback('h')
        expect(onChangeFunction).toHaveBeenCalledWith('h')
      })

I getting a weird error stating :

TypeError: Cannot read property 'values' of undefined

  51 |           icon={this.icon}
  52 |           onChangeTextCallback={() => {
> 53 |             onChangeTextInput(this.searchForm.values)
     |                                                 ^
  54 |           }}
  55 |         />
  56 |         )

My obj.html() dump for the test looks like:

<Form name="searchForm" if={true}>
  <TextInput
    noSpacing={true}
    placeholder="search"
    name="search"
    validate="text"
    icon={{
      $$typeof: Symbol(react.element),
      type: [(Function: Icon)],
      key: null,
      ref: null,
      props: {
        name: "search",
        size: 25,
        color: "#00a8ca",
        style: { position: "absolute", right: 0, bottom: 7 },
        allowFontScaling: false,
        type: "MaterialIcons",
        if: true,
      },
      _owner: null,
      _store: {},
    }}
    onChangeTextCallback={[(Function: onChangeTextCallback)]}
    value={null}
    style={{}}
    hasFloatingLabel={true}
    numberOfLines={1}
    isPassword={false}
    if={true}
  />
  )
</Form>

what is happening here? I have custom form that gives me values through refs. Do I need to do something and maybe initialize the Form ponent? please help, I am relatively new to this stuff.

I have a search bar ponent which looks like:

  render () {
    const { onChangeTextInput } = this.props
    return (
      <Form
        name="searchForm"
        ref={(ref) => {
          this.searchForm = ref
        }}
      >
        <TextInput
          noSpacing
          placeholder={t('search')}
          name="search"
          validate="text"
          icon={this.icon}
          onChangeTextCallback={() => {
            onChangeTextInput(this.searchForm.values)
          }}
        />
        )
      </Form>
    )
  }
}

I am using a shallow render and jest to run a unit test to test the following scenario"

  1. User types a character in text input
  2. a callback method gets fired which provides the user with the text as an argument.

The test I am running is stated as:

 test('SearchBar returns value on text change', () => {
        const onChangeFunction = jest.fn()
        const obj = shallow(
          <SearchBar onChangeTextInput={onChangeFunction} />
        )
        obj.find('TextInput').props().onChangeTextCallback('h')
        expect(onChangeFunction).toHaveBeenCalledWith('h')
      })

I getting a weird error stating :

TypeError: Cannot read property 'values' of undefined

  51 |           icon={this.icon}
  52 |           onChangeTextCallback={() => {
> 53 |             onChangeTextInput(this.searchForm.values)
     |                                                 ^
  54 |           }}
  55 |         />
  56 |         )

My obj.html() dump for the test looks like:

<Form name="searchForm" if={true}>
  <TextInput
    noSpacing={true}
    placeholder="search"
    name="search"
    validate="text"
    icon={{
      $$typeof: Symbol(react.element),
      type: [(Function: Icon)],
      key: null,
      ref: null,
      props: {
        name: "search",
        size: 25,
        color: "#00a8ca",
        style: { position: "absolute", right: 0, bottom: 7 },
        allowFontScaling: false,
        type: "MaterialIcons",
        if: true,
      },
      _owner: null,
      _store: {},
    }}
    onChangeTextCallback={[(Function: onChangeTextCallback)]}
    value={null}
    style={{}}
    hasFloatingLabel={true}
    numberOfLines={1}
    isPassword={false}
    if={true}
  />
  )
</Form>

what is happening here? I have custom form that gives me values through refs. Do I need to do something and maybe initialize the Form ponent? please help, I am relatively new to this stuff.

Share edited Sep 12, 2018 at 1:10 Thiago Murakami 9957 silver badges11 bronze badges asked Sep 10, 2018 at 20:54 AckmanAckman 1,5926 gold badges32 silver badges57 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Issue

The ref callback isn't getting called so this.searchForm is undefined when onChangeTextCallback() is invoked.

Details

From the Callback Refs docs: "React will call the ref callback with the DOM element when the ponent mounts".

In the test you are using shallow(). Shallow rendering does not mount the ponent so the ref callback is never called.

Solution

Mount the ponent. Since you are already using Enzyme you can use mount(). Note that "full DOM rendering requires that a full DOM API be available at the global scope", for Jest you can configure the test environment to use jsdom.

发布评论

评论列表(0)

  1. 暂无评论