gpt4 book ai didi

javascript - 在 enzyme 中模拟 history.back()

转载 作者:行者123 更新时间:2023-11-30 20:20:07 25 4
gpt4 key购买 nike

对于 react 组件测试,我需要模拟浏览器返回。对于已安装的组件,这可能吗?

我尝试过的:

it('triggers onpopstate events', () => {
requiredProps.filterBlocks = mockData.filterBlocks;

const wrapper = mount(<FilterBlocks {...requiredProps} />);

global.window.history.pushState({ mock: 'MOCK'}, "mock", "mock.html");
global.window.history.pushState({ mock: 'MOCK2'}, "mock", "mock2.html");

global.window.history.back();


expect(mockedFunction).toHaveBeenCalled();
})

在组件 (componentDidMount) 中我有一个 window.onpopstate 监听器:

window.onpopstate = event => {
console.log('event', event);
};

但控制台不会记录任何内容。

最佳答案

对于我的测试,我手动触发了 onpopstate:

const testEvent = { target: "mock" };

global.window.onpopstate(testEvent);

关于javascript - 在 enzyme 中模拟 history.back(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51536037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com