gpt4 book ai didi

reactjs - react 模拟测试 useNavigate

转载 作者:行者123 更新时间:2023-12-02 18:20:52 25 4
gpt4 key购买 nike

我已经查看了以下帖子,但仍然遇到在 React 中使用 useNavigate 进行重定向的问题。

react jest mock useNavigate()

https://github.com/remix-run/react-router/issues/7811

创建帖子片段

const result = await dispatch(postCreateView(postObj))
if(result){
nav('/posts')
}

测试设置

import {screen, render, fireEvent} from '@testing-library/react'
import { MockComponent } from '../../testMockComponentWrapper'
import { PostCreate } from '../PostCreate'

const mockedUsedNavigate = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedUsedNavigate,
}));

describe('Post Create', () => {

it('successful post with redirect', async () => {
const {container} = render(<MockComponent><PostCreate/></MockComponent>)
const titleElement = screen.getByPlaceholderText(/enter title/i)
const descriptionElement = screen.getByPlaceholderText(/enter description/i)
const buttonElement = screen.getByRole('button')

fireEvent.change(titleElement, {target: {value: 'A New title from Testing'}})
fireEvent.change(descriptionElement, {target: {value: 'A New description from Testing'}})
fireEvent.click(buttonElement)

expect(mockedUsedNavigate).toHaveBeenCalledTimes(1);

})

})

模拟组件:

export const MockComponent = (props) => {
return(
<BrowserRouter>
<Provider store={store}>
{props.children}
</Provider>
</BrowserRouter>
)
}

我也试过

export const MockComponent = (props) => {
return(
<Router>
<Provider store={store}>
{props.children}
</Provider>
</Router>
)
}

BrowserRouter 报错:

TypeError: Cannot read property 'pathname' of undefined

使用 Router I 我得到的错误:

Expected number of calls: 1
Received number of calls: 0

我原以为调用次数会是 1,因为 useNavigate 会由 jest 处理,但事实似乎并非如此。

我使用的版本:

"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"react": "^17.0.2",
"react-router-dom": "^6.2.1",

仅供引用,以下确实有效并测试了 react-redux 的调度方法:

const mockDispatch = jest.fn();

jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useDispatch: () => mockDispatch
}))

// In the test
await expect(mockDispatch).toHaveBeenCalledTimes(1);

如果有人对此提供帮助,我将不胜感激。如果有更好的地方发布这个问题或我错过的 SO 指南,我提前道歉,我们将不胜感激。提前谢谢你。

最佳答案

阿格! super 简单的答案......有时它有助于在 SO 上发布问题所以它迫使我更深入地挖掘......:)

这里是任何感兴趣的人的解决方案和解释......感谢阅读

import {screen, render, fireEvent} from '@testing-library/react'
import { MockComponent } from '../../testMockComponentWrapper'
import { PostCreate } from '../PostCreate'

// mock useDispatch
const mockDispatch = jest.fn();

jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useDispatch: () => mockDispatch.mockReturnValueOnce(true) // Return a value since I'm expecting a value to be returned before I redirect
}))

// mock useNavigate
const mockedUsedNavigate = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedUsedNavigate, // Return an empty jest function to test whether it was called or not...I'm not depending on the results so no need to put in a return value
}));



describe('Post Create', () => {

it('successful post with redirect', async () => {
const {container} = render(<MockComponent><PostCreate/></MockComponent>)
const titleElement = screen.getByPlaceholderText(/enter title/i)
const descriptionElement = screen.getByPlaceholderText(/enter description/i)
const buttonElement = screen.getByRole('button')

fireEvent.change(titleElement, {target: {value: 'A New title from Testing'}})
fireEvent.change(descriptionElement, {target: {value: 'A New description from Testing'}})
fireEvent.click(buttonElement)

// Await the dispatch results
await expect(mockDispatch).toHaveBeenCalledTimes(1);
await expect(mockedUsedNavigate).toHaveBeenCalledTimes(1);
})

})

如果我在解释或理解中遗漏了什么,我将不胜感激更新或见解。谢谢,

关于reactjs - react 模拟测试 useNavigate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70896585/

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