【问题标题】:Fake click using @testing-library/user-event使用@testing-library/user-event 进行虚假点击
【发布时间】:2020-12-04 09:48:13
【问题描述】:

我想测试点击响应中的链接时是否发生跟踪事件。但是,我确实得到了Error: Not implemented: navigation,这是单击链接时发生的情况。我正在用 jest 和 @testing-library/user-event 进行测试。

我现在想模拟或存根,以便在触发 userEvent 时不会发生导航尝试,因为我只想看到正在发生的跟踪事件。我该怎么做?

userEvent.click(item);

expect(Ga.trackEvent).toHaveBeenCalledTimes(1);
expect(Ga.trackEvent).toHaveBeenCalledWith('myModal', 'open'); 

【问题讨论】:

    标签: javascript reactjs jestjs react-testing-library user-event


    【解决方案1】:

    我认为您的测试实际上是在尝试使用 window.location 进行导航

    在尝试用户事件之前,您需要先模拟一下

    let assignMock = jest.fn();
    
    delete window.location;
    window.location = { assign: assignMock };
    
    afterEach(() => {
      assignMock.mockClear();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 2019-07-07
      • 2019-11-11
      • 1970-01-01
      • 2020-10-27
      • 2023-04-04
      • 2019-05-21
      相关资源
      最近更新 更多