问题描述
我在React中有一个要通过Jest测试的组件,不幸的是测试没有通过。
I have component in React which I'm trying to test with Jest, unfortunately test do not pass.
组件代码:
import React, {Component} from 'react';
import ProductItem from '../ProductItem/ProductItem';
import AppBar from "@material-ui/core/es/AppBar/AppBar";
import Tabs from "@material-ui/core/es/Tabs/Tabs";
import Tab from "@material-ui/core/es/Tab/Tab";
import {connect} from 'react-redux';
class ProductsTabsWidget extends Component {
state = {
value: 0
}
renderTabs = () => {
return this.props.tabs.map((item, index) => {
return item.products.length > 0 ? (<Tab key={index} label={item.title}/>) : false;
})
}
handleChange = (event, value) => {
this.setState({value});
};
renderConentActiveTab = () => {
if (this.props.tabs[this.state.value]) {
return this.props.tabs[this.state.value].products.map((productIndex) => {
return (<ProductItem key={productIndex} {...this.props.products[productIndex]} />);
});
}
}
render() {
let tabs = null;
let content = null;
if (this.props.tabs) {
tabs = this.renderTabs();
content = this.renderConentActiveTab();
}
return (
<div>
<AppBar position="static" color="default">
<Tabs
value={this.state.value}
onChange={this.handleChange}
indicatorColor="primary"
textColor="primary"
centered
scrollButtons="auto"
>
{tabs}
</Tabs>
</AppBar>
<div className="productWidget">
<div className="wrapper">
{content}
</div>
</div>
</div>
)
}
}
const mapStateToProps = state => {
return {
products: state.product.products,
}
}
export default connect(mapStateToProps)(ProductsTabsWidget);
我尝试为此组件编写适当的测试,代码如下:
I have tried to write proper test for this component, the code is below:
import React from 'react';
import {configure, shallow} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import ProductsTabsWidget from "./ProductsTabsWidget";
configure({adapter: new Adapter()});
describe('ProductsTabsWidget - component', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(<ProductsTabsWidget/>);
});
it('renders with minimum props without exploding', () => {
wrapper.setProps({
tabs: [],
products:[]
});
expect(wrapper).toHaveLength(1);
});
})
但是当我运行测试时出现错误:
But when I'm running test I am getting error:
Test suite failed to run
F:\PRACA\reactiveShop\node_modules\@material-ui\core\es\AppBar\AppBar.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import _extends from "@babel/runtime/helpers/builtin/extends";
^^^^^^
SyntaxError: Unexpected token import
at new Script (vm.js:51:7)
at Object.<anonymous> (src/components/product/ProductsTabsWidget/ProductsTabsWidget.js:3:15)
我尝试过测试浅
,装载
,渲染
,但是它没有帮助。我缺少什么?
I have tried testing with shallow
, mount
, render
but it did not help. What am I missing?
我的应用程序是在 create-react-app
上创建的。
My application is created on create-react-app
.
推荐答案
使用 @ material-ui
时会有所不同。
您必须使用 @ material-ui
的内置API。例如 createMount
, createShallow
, createRender
酶的浅
,安装量
& 渲染
。
You've to use @material-ui
's Built-in API(s). Such as createMount
, createShallow
, createRender
in order to use enzyme's shallow
, mount
& render
.
这些API建立在酶
,因此您不能直接使用酶
来测试 @ material-ui
。
These APIs are built on top of enzyme
, so you can't use enzyme
directly for testing @material-ui
.
import { createShallow } from '@material-ui/core/test-utils';
describe('<MyComponent />', () => {
let shallow;
before(() => {
shallow = createShallow();
});
it('should work', () => {
const wrapper = shallow(<MyComponent />);
});
});
参考:
这篇关于物料界面+酶测试组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!