【问题标题】:React Jest: TypeError: Cannot read property 'map' of undefinedReact Jest:TypeError:无法读取未定义的属性“地图”
【发布时间】:2020-02-10 21:44:03
【问题描述】:

我是新来的反应并且有一个失败的测试,我无法理解这是一个类导入问题还是我没有正确发送所需的参数。

这是我的代码

import * as React from 'react'
import cx from 'classnames'
import './dropdown.scss'

export interface DropdownProps {
    options: {
        key: string
        text: string
        value: string
    }[]
    value?: string
    placeholder?: string
    onSelect?: (value: string) => any
    children?: React.ReactNode
}

export const Dropdown = ({ options, value, placeholder, onSelect }: DropdownProps) => {
    const hasValue = typeof value === 'string';

    const [ top, setTop ] = React.useState(0);
    const handleTriggerRef = (el: HTMLButtonElement) => {
        if (el) {
            setTop(el.clientHeight)
        }
    };

    return (
        <div className={cx('dropdown-container')}>
            <button
                className={cx('title', { hasValue: hasValue })}
                ref={handleTriggerRef}
                onClick={() => {
                    if (value && typeof onSelect === 'function') {
                        onSelect(value)
                    }
                }}
            >{hasValue ?
                options.filter(item => item.value === value)[0].text :
                (placeholder || 'Select value')}</button>
            <div
                style={{ top }}
                className={cx('options')}
            >
                {options.map(option =>
                    <div
                        key={option.key}
                        className={cx('option')}
                        onClick={() => {
                            onSelect(option.value)
                        }}
                    ><div className={cx('text')}>{option.text}</div></div>)}
            </div>
        </div>
    )
};

这是测试

import { shallow } from "enzyme/build";
import React from "react";
import { Dropdown } from "../dropdown";

describe('Dropdown component', () => {
  test("Renders correctly", () => {
    const wrapper = shallow(<Dropdown />);

    expect(wrapper.exists()).toBe(true);
  });
});

【问题讨论】:

  • options: { key: string text: string value: string }[] 我在这里经过,代码工作正常,只是测试失败了。我想这是因为我没有通过测试下拉列表中的选项
  • 可能我需要做这样的事情const wrapper = shallow(&lt;Dropdown placeholder={placeholder} options={options} onSelect={setValue} value={value} /&gt;); 在测试中
  • 没错,你需要传递 props 才能访问它们

标签: javascript reactjs testing jestjs


【解决方案1】:

当您使用钩子时,建议我们应该使用 mount 而不是 shallow

你得到的错误是因为道具没有传递给浅层渲染。

import { shallow } from "enzyme/build";
import React from "react";
import { Dropdown } from "../dropdown";

describe('Dropdown component', () => {
  // no need to pass the optional props
  const props = {
    options: [{ key: 'key',text: 'text',value: 'value'}],
    value: 'value',
    placeholder: 'placeholder',
    onSelect: jest.fn(),
    children: <div>test</div>
  }

  test("Renders correctly", () => {
    const wrapper = shallow(<Dropdown {...props}/>);
    expect(wrapper.exists()).toBe(true);
  });
});

【讨论】:

    【解决方案2】:

    这是因为您没有将 options 传递给您的 Dropdown 组件。

    以下将防止错误。

    {options && options.map(option => .....
    

    但是在你的笑话测试中你真正想做的是

    <Dropdown options={options} />;
    

    【讨论】:

    • 是的,但我主要的问题是如何在测试中使用我的DropdownProps 接口?我不能只传递选项,因为它会抛出 ReferenceError: options is not defined
    • 我不太明白你的意思?。您的 DropdownProps 只是在描述您传递的道具。此外,options 未使用 ? 定义,因此您需要传递它们。你不需要在测试中使用这些道具,接口是在组件中定义的,这样就足够了。如果您传递所有必要的道具,如果它引发错误,我会感到惊讶。另外,如果你在options?: ... 中添加?,那么TS 会抱怨并强迫你这样做options &amp;&amp; options.map...
    • 是的,我尝试传递 options={options},但它抛出 ReferenceError: options is not defined 错误:P
    • 您是否在测试中定义了options?您将需要创建一个虚拟选项,例如 options={[{key: 1, text: 'One', value: '1'}]}
    猜你喜欢
    • 2019-03-14
    • 2021-04-16
    • 2017-03-26
    • 2021-09-09
    • 2017-02-21
    • 2020-09-16
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多