Javascript 模拟酶中的history.back()
对于react组件测试,我需要模拟浏览器。对于安装的部件,这是否可行 我所尝试的:Javascript 模拟酶中的history.back(),javascript,jestjs,enzyme,Javascript,Jestjs,Enzyme,对于react组件测试,我需要模拟浏览器。对于安装的部件,这是否可行 我所尝试的: it('triggers onpopstate events',()=>{ requiredProps.filterBlocks=mockData.filterBlocks; const wrapper=mount(); global.window.history.pushState({mock:'mock'},“mock”,“mock.html”); global.window.history.pushStat
it('triggers onpopstate events',()=>{
requiredProps.filterBlocks=mockData.filterBlocks;
const wrapper=mount();
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);
};
但不会将任何内容记录到控制台。对于我的测试,我在PopState上手动触发了:
const testEvent = { target: "mock" };
global.window.onpopstate(testEvent);