【发布时间】:2018-04-05 15:51:45
【问题描述】:
我是 react 新手,正在尝试对我的 react 组件中的以下代码进行单元测试
import React,{Component} from 'react';
const styleSheet = document.styleSheets[0];
const keyframes =
`@-webkit-keyframes pulse {
0% {
transform: scale(0.9);
opacity: 0.6;
}
50% {
transform: scale(1);
opacity: 1;
}
100% {
transform: scale(0.9);
opacity: 0.6;
}
}`;
styleSheet.insertRule(keyframes, styleSheet.cssRules.length);
const style = {
animation: 'pulse 5.5s infinite ease-in-out'
};
class someclass extends Component {
render() {
return (
<svg style={style}>
</svg>
);
}
}
export default someclass;
这是我的测试文件:
import React from 'react'
import {shallow, mount, render} from 'enzyme'
import {expect} from 'chai'
import someclassfrom '.../src/components/someclass';
describe('someclass', () => {
it('should have the main svg', () => {
const wrapper = shallow(<someclass/>);
expect(wrapper.find('svg')).to.have.length(1);
})
})
但我不断收到以下错误:
Cannot read property '0' of undefined
我如何模拟相同的?提前致谢
【问题讨论】:
-
你有没有试过在定义组件之前简化一些东西并注释掉样式处理代码?
标签: javascript reactjs unit-testing enzyme jsdom