【发布时间】:2020-05-11 15:00:27
【问题描述】:
App.js:
function App() {
return (
<div className="App">
<MyComponent />
</div>
);
}
export default App;
MyComponent.js:
import React, { Component } from 'react'
class MyComponent extends Component {
constructor(props) {
super(props)
this.state = {
isShown : false
}
}
clickHandler = () => {
this.setState(({isShown}) => ({ isShown : !isShown }));
}
render() {
const {isShown} = this.state
return (
<div>
<button onClick= {this.clickHandler}>Click Me</button>
{isShown && <div>Text goes here</div> }
</div>
)
}
}
export default MyComponent
MyComponent.test.js:
import React from 'react'
import Enzyme, {shallow} from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import MyComponent from './MyComponent';
Enzyme.configure({adapter : new Adapter() });
describe('MyComponent', () => {
it('should show text', () => {
const wrapper = shallow(<MyComponent />);
const text = wrapper.find('div div');
expect(text.text()).toBe('Text goes here');
});
});
我已经安装了以下命令
npm install --save-dev enzyme enzyme-adapter-react-16
我不知道为什么会出现以下错误:
MyComponent › 应该显示文本
Method “text” is meant to be run on 1 node. 0 found instead.
11 | const wrapper = shallow(<MyComponent />);
12 | const text = wrapper.find('div div');
> 13 | expect(text.text()).toBe('Text goes here');
| ^
14 | });
15 | });
at ShallowWrapper.single (node_modules/enzyme/src/ShallowWrapper.js:1652:13)
at ShallowWrapper.text (node_modules/enzyme/src/ShallowWrapper.js:1093:17)
at Object.<anonymous> (src/MyComponent.test.js:13:21)
曾经使用相同的配置执行相同的代码..但现在它没有执行? 有人可以帮我解决这个问题吗?
【问题讨论】:
标签: c# reactjs asp.net-mvc .net-core react-redux