|
| 1 | +import createVirtualEnvironment from '@locker/near-membrane-dom'; |
| 2 | + |
| 3 | +const originalHostDesc = Object.getOwnPropertyDescriptor(ShadowRoot.prototype, 'host'); |
| 4 | +const { get: hostGetter } = originalHostDesc; |
| 5 | + |
| 6 | +const distortionMap = new Map([[hostGetter, () => null]]); |
| 7 | + |
| 8 | +function distortionCallback(v) { |
| 9 | + return distortionMap.get(v) ?? v; |
| 10 | +} |
| 11 | + |
| 12 | +describe('protectDistortions', () => { |
| 13 | + afterEach(() => { |
| 14 | + if (!Object.getOwnPropertyDescriptor(ShadowRoot.prototype, 'host')) { |
| 15 | + Object.defineProperty(ShadowRoot.prototype, 'host', originalHostDesc); |
| 16 | + } |
| 17 | + }); |
| 18 | + |
| 19 | + describe('when enabled', () => { |
| 20 | + it('should throw when deleting a property whose getter is distorted', () => { |
| 21 | + expect.assertions(1); |
| 22 | + |
| 23 | + const env = createVirtualEnvironment(window, { |
| 24 | + distortionCallback, |
| 25 | + liveTargetCallback() { |
| 26 | + return true; |
| 27 | + }, |
| 28 | + protectDistortions: true, |
| 29 | + globalObjectShape: window, |
| 30 | + }); |
| 31 | + |
| 32 | + env.evaluate(` |
| 33 | + expect(() => { |
| 34 | + delete ShadowRoot.prototype.host; |
| 35 | + }).toThrowError(TypeError); |
| 36 | + `); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should allow deleting properties that are not distorted', () => { |
| 40 | + expect.assertions(2); |
| 41 | + |
| 42 | + const env = createVirtualEnvironment(window, { |
| 43 | + distortionCallback, |
| 44 | + liveTargetCallback() { |
| 45 | + return true; |
| 46 | + }, |
| 47 | + protectDistortions: true, |
| 48 | + globalObjectShape: window, |
| 49 | + }); |
| 50 | + |
| 51 | + env.evaluate(` |
| 52 | + const obj = { x: 1 }; |
| 53 | + expect(delete obj.x).toBe(true); |
| 54 | + expect(obj.x).toBe(undefined); |
| 55 | + `); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should allow deleting an undistorted window API', () => { |
| 59 | + expect.assertions(3); |
| 60 | + |
| 61 | + const env = createVirtualEnvironment(window, { |
| 62 | + distortionCallback, |
| 63 | + liveTargetCallback() { |
| 64 | + return true; |
| 65 | + }, |
| 66 | + protectDistortions: true, |
| 67 | + globalObjectShape: window, |
| 68 | + }); |
| 69 | + |
| 70 | + env.evaluate(` |
| 71 | + expect(delete window.AbortController).toBe(true); |
| 72 | + expect(window.AbortController).toBe(undefined); |
| 73 | + `); |
| 74 | + |
| 75 | + expect(window.AbortController).not.toBe(undefined); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should preserve the distorted getter after a failed delete attempt', () => { |
| 79 | + expect.assertions(2); |
| 80 | + |
| 81 | + const env = createVirtualEnvironment(window, { |
| 82 | + distortionCallback, |
| 83 | + liveTargetCallback() { |
| 84 | + return true; |
| 85 | + }, |
| 86 | + protectDistortions: true, |
| 87 | + globalObjectShape: window, |
| 88 | + }); |
| 89 | + |
| 90 | + env.evaluate(` |
| 91 | + const elm = document.createElement('div'); |
| 92 | + elm.attachShadow({ mode: 'open' }); |
| 93 | + expect(() => { |
| 94 | + delete ShadowRoot.prototype.host; |
| 95 | + }).toThrowError(TypeError); |
| 96 | + expect(elm.shadowRoot.host).toBe(null); |
| 97 | + `); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('when disabled (default)', () => { |
| 102 | + it('should not throw when deleting a property whose getter is distorted', () => { |
| 103 | + expect.assertions(1); |
| 104 | + |
| 105 | + const env = createVirtualEnvironment(window, { |
| 106 | + distortionCallback, |
| 107 | + liveTargetCallback() { |
| 108 | + return true; |
| 109 | + }, |
| 110 | + globalObjectShape: window, |
| 111 | + }); |
| 112 | + |
| 113 | + env.evaluate(` |
| 114 | + expect(() => { |
| 115 | + delete ShadowRoot.prototype.host; |
| 116 | + }).not.toThrow(); |
| 117 | + `); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments