【问题标题】:How can I testi React Router with Jest我如何用 Jest 测试 React 路由器
【发布时间】:2021-11-08 02:38:58
【问题描述】:

我是 jest 测试的新手,我想测试以下代码。

import React from "react";
import "./ButtonLogin.css";
import { Link } from 'react-router-dom';

function ButtonLogin() {
    return (
        <Link to="/login"> <button className="button-login">Iniciar sesión</button></Link>
    )
}

export default ButtonLogin;
import { MemoryRouter } from 'react-router-dom';
import { render, fireEvent, Link } from '@testing-library/react';
import { ButtonLogin } from './ButtonLogin';

it('routes to a new route', async () => {

  ButtonLogin = jest.fn();

  const { getByText } = render(
    <MemoryRouter ButtonLogin={ButtonLogin}>
      <Link to="/login">Iniciar sesión</Link>
    </MemoryRouter>
  );

  fireEvent.click(getByText('Iniciar sesión'));

  expect(ButtonLogin).toHaveBeenCalledWith('/login');
});

我已经执行了以下测试,但它失败了,我在第 9 行收到以下错误。 通往新路线的路线

"ButtonLogin" is read-only.

【问题讨论】:

    标签: javascript reactjs jestjs react-router-dom react-testing-library


    【解决方案1】:

    您可以使用createMemoryHistory 函数和Router 组件对其进行测试。使用初始条目创建内存历史记录来模拟当前位置,这样我们就不会依赖真实的浏览器环境。触发点击事件后,断言pathname是否正确更改。

    ButtonLogin.tsx:

    import React from 'react';
    import { Link } from 'react-router-dom';
    
    function ButtonLogin() {
      return (
        <Link to="/login">
          <button className="button-login">Iniciar sesión</button>
        </Link>
      );
    }
    
    export default ButtonLogin;
    

    ButtonLogin.test.tsx:

    import { fireEvent, render } from '@testing-library/react';
    import React from 'react';
    import { Router } from 'react-router-dom';
    import ButtonLogin from './ButtonLogin';
    import { createMemoryHistory } from 'history';
    describe('ButtonLogin', () => {
      test('should pass', () => {
        const history = createMemoryHistory({ initialEntries: ['/home'] });
        const { getByText } = render(
          <Router history={history}>
            <ButtonLogin />
          </Router>
        );
        expect(history.location.pathname).toBe('/home');
        fireEvent.click(getByText('Iniciar sesión'));
        expect(history.location.pathname).toBe('/login');
      });
    });
    

    测试结果:

     PASS  examples/69878146/ButtonLogin.test.tsx (10.675 s)
      ButtonLogin
        ✓ should pass (41 ms)
    
    -----------------|---------|----------|---------|---------|-------------------
    File             | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    -----------------|---------|----------|---------|---------|-------------------
    All files        |     100 |      100 |     100 |     100 |                   
     ButtonLogin.tsx |     100 |      100 |     100 |     100 |                   
    -----------------|---------|----------|---------|---------|-------------------
    Test Suites: 1 passed, 1 total
    Tests:       1 passed, 1 total
    Snapshots:   0 total
    Time:        11.722 s, estimated 12 s
    

    包版本:"react-router-dom": "^5.2.0"

    【讨论】:

    • 请注意,&lt;Router history={history} /&gt; 在 v6 中不起作用,这是您当前在 npm i react-router-dom 时得到的:stackoverflow.com/q/69859509/3001761
    • @jonrsharpe 谢谢。很高兴知道。我还在用react-routerv5
    猜你喜欢
    • 2021-06-14
    • 2021-04-01
    • 2021-01-09
    • 2022-07-14
    • 1970-01-01
    • 2021-04-18
    • 2021-07-04
    • 1970-01-01
    • 2021-07-14
    相关资源
    最近更新 更多