到达/路由器导航不使用 react-testing-library 更新历史对象

问题描述

以下测试继续失败。单击按钮时,它会使用查询字符串 /?searchString=abcde 调用 @reach/router 导航,但是测试中未更新历史记录对象。您如何使用 react-testing-library 和reach-router在没有模拟的情况下测试程序化导航更新。

import { createHistory,createMemorySource,LocationProvider,navigate } from "@reach/router"
import { render,waitFor } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import React from 'react'

test('Can us search path',async () => {
  const history = createHistory(createMemorySource('/'))
  const page = render(
      <LocationProvider history={history}>
        <div>
          <button onClick={() => navigate('/?searchString=abcde')}>navigateButton</button>
        </div>)
      </LocationProvider>
  )
  expect(history.location.search).toEqual('')
  userEvent.click(page.getByText('navigateButton')) //click button to call navigate
  await waitFor(() => expect(history.location.search).toEqual('searchString=abcde')) //FAILS but Should PASS
})

依赖

  "@testing-library/react": "^11.2.3","@testing-library/user-event": "^12.6.0","@testing-library/jest-dom": "^5.11.9","react": "^17.0.1","react-dom": "^17.0.1","@reach/router": "^1.3.4",

解决方法

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

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

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