简体   繁体   English

测试库反应模拟 function 与回调将设置 state 返回

[英]testing-library react mock function with callback that will set state on return

I'm new to the testing world, and I'm trying to test a component with useEffect that will execute a function that gets 3 parameters and the last one is a callback with setMyState inside:我是测试世界的新手,我正在尝试使用useEffect测试一个组件,该组件将执行一个获取 3 个参数的 function,最后一个是内部带有setMyState的回调:

my component:我的组件:

const Component = () => {
  const [myState, setMystate] = useState({})
  useEffect(() => {
    myFunctions('string param>','<string param>', (foo) => {
    setMystat(foo)
  })
})
}

return...

I'm using '@testing-library/react' for testing.我正在使用'@testing-library/react'进行测试。 Not sure how can I render the component in the test then make sure the state got the new state set to it and the component retendered.不知道如何在测试中渲染组件,然后确保 state 设置了新的 state 并重新渲染了组件。

I managed to solved the issue by mocking the model with the function and a callback, and calling the callback function: on the top of the test: I managed to solved the issue by mocking the model with the function and a callback, and calling the callback function: on the top of the test:

jest.mock('../../../path/to/module', () => ({
//function in the module I need to call
 myFunction: {
//this will execute the function when called from the "useEffect"
//method inside the function
method : (param1, param2, callback ) => callback({item: 'item'})
 },

}) })

component :

const Component = () => {
const [myState, setMystate] = useState({})
useEffect(() => {
myFunctions.post('string param>','<string param>', (foo) => {
setMystat(foo)
 })

}) } }) }

return...返回...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React testing-library - 测试在第一个 useEffect 挂钩中设置状态的 promise - React testing-library - Testing a promise that set states in the first useEffect hook 反应测试 | 从“@testing-library/user-event”导入 userEvent 会生成错误“toBeInTheDocument 不是函数” - React Testing | importing userEvent from '@testing-library/user-event' generates error "toBeInTheDocument is not a function" 反应测试库中的模拟导出函数和变量 - mock export function and variable in react testing library 无法读取测试库中的 null 的属性“innerHTML”(反应) - Cannot read property 'innerHTML' of null in expect function in testing-library (React) @testing-library/react 中的 toBeInTheDocument 和 getBy* 有什么区别 - Whats the difference between toBeInTheDocument and getBy* in @testing-library/react 在反应应用程序上使用 jest 和测试库未调用模拟函数 - Mock function not being called using jest and testing library on react app 使用 Jest 和 @testing-library/react-hooks 在 TypeScript 中单元测试自定义 React Hook - Unit Testing Custom React Hooks in TypeScript using Jest and @testing-library/react-hooks 在 React / testing-library/react 中呈现所有组件后,如何从表中获取元素? - How to get the elements from a table after all components are rendered in React / testing-library/react? 使用@testing-library/react 对 react-router 的链接进行最简单的测试 - Simplest test for react-router's Link with @testing-library/react @testing-library/react (rtl) 'waitFor' 只有在没有 await 关键字的情况下才会成功 - @testing-library/react (rtl) 'waitFor' makes only success without await keyword
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM