如何测试在反应测试库测试中调用了中继钩子突变?

问题描述

我基本上有以下几点。

  • 一个组件
  • 订阅了一些数据事件。
  • 并且在处理订阅事件后运行突变。

运行mutation时UI没有变化,它只是向后端发送一个ping,说我们收到了一条消息。

我们在重构为 GraphQL 之前拥有的旧系统有一个名为 Api 的简单模块,我们可以在其中简单地监视 Api 模块方法以查看它们是否在特定时间被调用,如下所示:

it('test api is called',() => {
  render(<MyComponent />)

  triggerEvent()

  expect(Api.sendReceipt).toHaveBeenCalledWith(123,'delivered',null)

  fireEvent.click(getByText(/search/i))

  expect(Api.sendReceipt).toHaveBeenLastCalledWith(123,'ackNowledged',null)
})

现在如何将其移植到测试调用的钩子突变?

it('test mutation is called',() => {
  render(<MyComponent />)

  triggerEvent()

  expect(somehowHookIntoMutation).toHaveBeenCalledWith(123,null)

  fireEvent.click(getByText(/search/i))

  expect(somehowHookIntoMutation).toHaveBeenLastCalledWith(123,null)
})

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)