gpt4 book ai didi

reactjs - 如何使用 Jest 使用新的 React Router Hooks 模拟历史记录。

转载 作者:行者123 更新时间:2023-12-03 13:32:39 24 4
gpt4 key购买 nike

我正在尝试在 react-router 上的新 useHistory Hook 内模拟 history.push 并使用 @testing-library/ react 。我只是像这里的第一个答案一样 mock 该模块:How to test components using new react router hooks?

所以我正在做:

//NotFound.js
import * as React from 'react';
import { useHistory } from 'react-router-dom';


const RouteNotFound = () => {
const history = useHistory();
return (
<div>
<button onClick={() => history.push('/help')} />
</div>
);
};

export default RouteNotFound;
//NotFound.test.js
describe('RouteNotFound', () => {
it('Redirects to correct URL on click', () => {
const mockHistoryPush = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useHistory: () => ({
push: mockHistoryPush,
}),
}));

const { getByRole } = render(
<MemoryRouter>
<RouteNotFound />
</MemoryRouter>
);

fireEvent.click(getByRole('button'));
expect(mockHistoryPush).toHaveBeenCalledWith('/help');
});
})

但是 mockHistoryPush 没有被调用...我做错了什么?

最佳答案

在模块范围内使用jest.mock将自动提升到代码块的顶部。这样您就可以在 NotFound.jsx 文件和测试文件中获取模拟版本 react-router-dom

此外,我们只想模拟 useHistory 钩子(Hook),因此我们应该使用 jest.requireActual() 来获取原始模块,并将其他方法保留为原始版本。

解决方案如下:

NotFound.jsx:

import React from 'react';
import { useHistory } from 'react-router-dom';

const RouteNotFound = () => {
const history = useHistory();
return (
<div>
<button onClick={() => history.push('/help')} />
</div>
);
};

export default RouteNotFound;

NotFound.test.jsx:

import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { render, fireEvent } from '@testing-library/react';
import RouteNotFound from './NotFound';

const mockHistoryPush = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useHistory: () => ({
push: mockHistoryPush,
}),
}));

describe('RouteNotFound', () => {
it('Redirects to correct URL on click', () => {
const { getByRole } = render(
<MemoryRouter>
<RouteNotFound />
</MemoryRouter>,
);

fireEvent.click(getByRole('button'));
expect(mockHistoryPush).toHaveBeenCalledWith('/help');
});
});

100%覆盖率的单元测试结果:

PASS  src/stackoverflow/58524183/NotFound.test.jsx
RouteNotFound
✓ Redirects to correct URL on click (66ms)

--------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
NotFound.jsx | 100 | 100 | 100 | 100 | |
--------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 5.133s, estimated 11s

源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/58524183

关于reactjs - 如何使用 Jest 使用新的 React Router Hooks 模拟历史记录。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58524183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com