如何使用 Jest 测试 window.location 的路径名

时间:2021-06-30 13:51:42

标签: jestjs react-testing-library

我有一个按钮,当点击它时会触发 apollo 突变,如果可行的话会进行导航。我想测试导航是否到达我期望的位置

test('I click the Bekraeft button', async () => {
        global.window.location = {
    ...window.location, pathname: '/tilkoeb/1268' };
        const element = await waitFor(() => screen.getByTestId('CurrentSubscriptionOverview'));
        expect(element);
       // getbyText not working with danish characters evidently
      const newbutton = element.querySelector('button[type="submit"]');
      expect(newbutton);
      if (newbutton) {
         fireEvent.click(newbutton);
      }
      await waitFor(() => { expect(global.window.location.pathname).toEqual('/tilkoeb/1268/kvittering'); })
        
      });

if(newbutton) 行是为了安抚打字稿。

一切正常,直到我添加该行

expect(global.window.location.pathname).toEqual('/tilkoeb/1268/kvittering');

消息失败

expect(received).toEqual(expected) // deep equality
Expected: "/tilkoeb/1268/kvittering"
Received: "/"

我有一种感觉——从我正在阅读的所有答案和教程中,似乎经历了各种循环来模拟导航,我们只是无法获得实际导航或无法实现?

>

但是,如果我可以做到这一点,我如何查看单击按钮时导航到的位置?

0 个答案:

没有答案