I have a simple component (select), which takes a list of options. Inside it I render the list using map. I test the component using jest and enzyme and I make a snapshot. Unfortunately, the coverage complains about the map and function inside it, which produces option elements.
How to test it in the proper way to have 100% coverage?
coverage:
-------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-------------------|----------|----------|----------|----------|-------------------|
BookList.js | 83.33 | 100 | 50 | 100 | |
BookListItem.js | 100 | 100 | 100 | 100 | |
BookList.js
import React from 'react';
import { shape, string, arrayOf } from 'prop-types';
import BookListItem from './BookListItem';
const renderBookItems = book => <BookListItem
key={book.id}
title={book.volumeInfo.title}
/>;
const BookList = ({ books }) => <div>{books.map(renderBookItems)}</div>;
BookList.displayName = 'BookList';
BookList.propTypes = {
books: arrayOf(shape({
volumeInfo: shape({
title: string,
}),
id: string,
})),
};
export default BookList;
BookList.test.js
import React from 'react';
import { shallow } from 'enzyme';
import BookList from './BookList';
describe('<BookList />', () => {
it('should match snapshot', () => {
const wrapper = shallow(<BookList books={[]} />);
expect(wrapper).toMatchSnapshot();
});
});
renderBookItemswill never been called. so it's excluded from test.