我想测试点击链接更新我的应用程序中的组件。
这是我的应用程序,当您单击 about 时,它会呈现 About 组件
import React, { Component } from 'react';
import './App.css';
import {
MemoryRouter as Router,
Route,
Link
} from 'react-router-dom'
const Home = () => <h1>home</h1>
const About = () => <h1>about</h1>
class App extends Component {
render() {
return (
<Router>
<ul>
<li><Link to="/about">About</Link></li>
<li><Link to="/">Home</Link></li>
</ul>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
</Router>
);
}
}
export default App;
这是我的测试:
import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import Enzyme from 'enzyme';
import { mount } from "enzyme";
import { MemoryRouter} from 'react-router-dom'
import App from './App';
Enzyme.configure({ adapter: new Adapter() });
// this test passes as expected
it('renders intitial heading as home', () => {
const wrapper = mount(
<App />
);
const pageHeading = wrapper.find("h1").first().text()
expect(pageHeading).toEqual('home');
});
it('renders about heading when we navigate to about', () => {
const wrapper = mount(
<App />
);
const link = wrapper.find("Link").first();
link.simulate('click');
const pageHeading = wrapper.find("h1").first().text()
expect(pageHeading).toEqual('about');
});
第二次测试失败:
FAIL src/App.test.js
● renders about heading when we navigate to about
expect(received).toEqual(expected)
Expected value to equal:
"about"
Received:
"home"
我正在使用react-router v4,react 16 和酶 3.1
是否可以使用 React Router 和酶以这种方式进行测试?