简体   繁体   English

如何使用 jest 和 react 测试库在 react 中测试路由

[英]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.

相关问题 如何使用 react-testing-library 和 jest 测试链接 - How to test link using react-testing-library and jest 如何使用 jest 和 react 测试库测试 axios api? - How to test axios api using jest and react testing library? 如何使用 React 测试库和 jest 测试条件渲染的字符串 - How to test conditionally rendered string using React testing library and jest 我将如何使用 Jest 和 React 测试库对此进行测试? - How would I test this using Jest & React Testing library? 如何使用 Jest 和 React 测试库测试 className - How to test a className with the Jest and React testing library 如何使用 Jest 和 react-testing-library 测试 react-dropzone? - How to test react-dropzone with Jest and react-testing-library? 如何使用 onClick 函数 Jest/React 测试库测试 React 组件 - How to test react component with onClick function Jest/React testing library 如何用 jest 和 react-testing-library 测试 react-toastify - How to test react-toastify with jest and react-testing-library 如何使用 React、Jest 和 React-testing-library 为带有令牌的 api 调用编写单元测试? - How can I write unit test for api call with token using React, Jest and React-testing-library? 使用 JEST 和 React 测试库在 Onclick Function 中测试 API 调用 - Test API call in Onclick Function using JEST and React Testing Library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM