【问题标题】:Jest / Enzyme - Cannot read property 'value' of undefined开玩笑/酶 - 无法读取未定义的属性“值”
【发布时间】:2018-09-05 21:12:58
【问题描述】:

我有一个要测试的组件。好像是这样 -

import * as React from 'react';
import { CATEGORY_TYPES } from '../../constants';

import './ResultCard.sass';

const ResultCard = ({ result, onSelectResult = null }) => {
  const { guid, category, postcode, condition } = result;
  const { value, image } = CATEGORY_TYPES.find(asset => asset.type === result.category);

  return (
    <article className="media result-card is-marginless" onClick={() => onSelectResult()}>
   <!-- omitted for brevity -->
    </article>
  );
};

export default ResultCard;

这是使用以下测试用例进行测试的 -

import * as React from 'react';
import { shallow } from 'enzyme';

import { ResultCard } from '../../../src/components';
import { CATEGORY_TYPES } from '../../../src/constants';

describe('.ResultCard', () => {
  it('should render', () => {
    const { wrapper } = setup({});

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

const setup = propOverrides => {
  const props = Object.assign(
    { result: { guid: '61934c1f-ce55-5cc7-e1d7-18b391fe7944', postcode: 'ab12 3cd', category: 'foo', condition: 'bar' } },
    propOverrides
  );
  const wrapper = shallow(<ResultCard {...props} />);
  return { wrapper };
};

但是,我的测试运行程序出现以下错误 -

  TypeError: Cannot read property 'value' of undefined

       7 |   const { guid, category, postcode, condition } = result;
       8 |   const { value, image } = CATEGORY_TYPES.find(asset => asset.type === result.category);
    >  9 |
      10 |   return (
      11 |     <article className="media result-card is-marginless" onClick={() => onSelectResult()}>
      12 |       <figure className="media-left">

我不确定如何将 CATEGORY_TYPES 周围的组件逻辑引入到我的组件中。

【问题讨论】:

    标签: reactjs unit-testing jestjs enzyme


    【解决方案1】:

    愚蠢的错误,我传入了 'foo' 并希望在我的 CATEGORY_TYPES 数组中找到它,因为它不存在,所以它不起作用

    【讨论】:

      猜你喜欢
      • 2020-06-26
      • 1970-01-01
      • 2022-01-18
      • 2023-03-03
      • 2020-11-19
      • 2018-06-17
      • 1970-01-01
      • 2020-07-10
      • 2017-12-15
      相关资源
      最近更新 更多