🐛 add getPrototypeOf trap in sandbox to keep the window instanceof Window returns true in microapp (#1465)

This commit is contained in:
Kuitos 2021-05-25 11:50:04 +08:00 committed by GitHub
parent 4637fae87e
commit 08f810a28b
2 changed files with 20 additions and 0 deletions

View File

@ -332,3 +332,18 @@ test('falsy values should return as expected', () => {
expect(proxy.nullvar).toBeNull();
expect(proxy.zero).toBe(0);
});
it('should return true while [[GetPrototypeOf]] invoked by proxy object', () => {
// window.__proto__ not equals window prototype in jest environment
// eslint-disable-next-line no-proto
expect(window.__proto__ === Object.getPrototypeOf(window)).toBeFalsy();
// we must to set the prototype of window as jest modified window `__proto__` property but not changed it internal [[Prototype]] property
// eslint-disable-next-line no-proto
Object.setPrototypeOf(window, window.__proto__);
const { proxy } = new ProxySandbox('window-prototype');
expect(proxy instanceof Window).toBeTruthy();
expect(Object.getPrototypeOf(proxy)).toBe(Object.getPrototypeOf(window));
expect(Reflect.getPrototypeOf(proxy)).toBe(Reflect.getPrototypeOf(window));
expect(Reflect.getPrototypeOf(proxy)).toBe(Object.getPrototypeOf(window));
});

View File

@ -332,6 +332,11 @@ export default class ProxySandbox implements SandBox {
return true;
},
// makes sure `window instanceof Window` returns truthy in micro app
getPrototypeOf() {
return Reflect.getPrototypeOf(rawWindow);
},
});
this.proxy = proxy;