Reputation: 4502
I have a component I am trying to test. It looks like so -
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;
This is tested as such using the follow test case -
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 };
};
However I am getting the following error from my test runner -
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">
I am unsure how I can introduce the component logic around CATEGORY_TYPES into my component.
Upvotes: 1
Views: 2688
Reputation: 4502
Silly mistake, I was passing in 'foo' and expecting to find it on my CATEGORY_TYPES array, which wouldn't work as it doesn't exist
Upvotes: 1