对于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);
};
但是不会将任何内容记录到控制台。
答案 0 :(得分:1)
为了测试,我手动触发了onpopstate:
const testEvent = { target: "mock" };
global.window.onpopstate(testEvent);