[英]how to test Routes in react using jest and react testing library
so I'm trying to test each Route component and url are matching所以我正在尝试测试每个 Route 组件和 url 是否匹配
the function function
const App = () => (
<Provider store={store}>
<Router>
<Layout>
<Switch>
<Route exact path='/' component={HomePage} />
<Route exact path='/login' component={LoginPage} />
<Route exact path='/signup' component={SignupPage} />
<Route exact path='/recipes' component={RecipesPage} />
<Route exact path='/recipes/:id' component={RecipeDetailPage} />
<Route exact path='/reset_password' component={ResetPasswordPage} />
<Route exact path='/password/reset/confirm/:uid/:token' component={ResetPasswordConfirmPage} />
<Route component={NotFound} />
</Switch>
</Layout>
</Router>
</Provider>
);
anyone has an idea how can i test this?任何人都知道我该如何测试这个?
so after some trial and error I have managed to get the answer, this is the test that worked for me:所以经过一些试验和错误后,我设法得到了答案,这是对我有用的测试:
(i added a test-id to the homePage so i could know that this is the component that got rendered) (我在主页上添加了一个 test-id,所以我可以知道这是被渲染的组件)
describe('App', () => {
beforeEach(() => {
const renderWithRouter = (ui, { route = '/' } = {}) => {
window.history.pushState({}, 'Test page', route);
return render(ui, { wrapper: BrowserRouter });
};
renderWithRouter(<App />);
});
afterEach(() => {
cleanup();
});
test('should render without crashing', () => {});
test('should render home page', () => {
const homePage = screen.getByText('homePage');
expect(homePage).toBeInTheDocument();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.