reactjs - 用玩笑和酶测试反应成分,覆盖率问题
问题描述
我有一个简单的组件(选择),它需要一个选项列表。在其中我使用地图呈现列表。我使用 jest 和酵素测试组件并制作快照。不幸的是,覆盖范围抱怨其中的地图和功能,这会产生选项元素。
如何以正确的方式对其进行测试以获得 100% 的覆盖率?
覆盖范围:
-------------------|----------|----------|----------|----------|-------------------|
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();
});
});
解决方案
酶浅不会呈现renderBookItems
。因此,您的测试仅涵盖BookList
,您需要为renderBookItems
.
推荐阅读
- kotlin - 找不到片段构造函数kotlin
- python - 我无法使用 Selenium Python 在亚马逊上单击“立即购买”按钮
- pine-script - 修复松树筛选器转换
- android - Firebase 实时数据库数据自动被删除
- kubernetes - Argo 提前停止工作流程,标记完成
- encoding - 编码错误:用越南语从头开始训练 BERT
- .net-core - 信号器:身份验证后,我在客户端收到错误 400(错误请求)
- flutter - 没有为“Registrar”类型定义方法“registerMessageHandler”
- laravel - 有没有办法将最后一次操作的结果放入变量中
- visual-studio-code - 使用 VS Code 打开新终端时设置默认工作目录