对于 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/