我在我的代码中有一个检查,看看页面是否被加载到iframe中。
App.js
if (window.location !== window.parent.location) {
console.log('App is running inside iframe!');
}Jest测试...
App.spec.js
delete window.location
window.location = new URL(`https://some-fake-url`)
delete window.parent.location
window.parent.location = new URL(`https://parent-fake-url`);在Jest测试中,window.location.href和window.parent.location.href都被设置为相同的值。
如何才能将每一个都设置为唯一的?
发布于 2020-11-12 00:58:20
我相信我能够通过以下几点来解决这个问题。
global.window = Object.create(window);
delete global.window.parent;
global.window.parent = Object.create(window);
Object.defineProperty(global.window, 'location', {
value: {
href: 'https://my-site'
}
});
Object.defineProperty(global.window.parent, 'location', {
value: {
href: 'https://my-parent-site'
}
});https://stackoverflow.com/questions/64790296
复制相似问题