|
| 1 | +import { isJsdomEnv, WrongEnvironmentError } from '../../helper'; |
| 2 | + |
1 | 3 | const protectedProps = ['_x', '_y', '_width', '_height'];
|
2 | 4 |
|
3 | 5 | class MockedDOMRectReadOnly implements DOMRectReadOnly {
|
@@ -107,7 +109,7 @@ class MockedDOMRectReadOnly implements DOMRectReadOnly {
|
107 | 109 | }
|
108 | 110 | }
|
109 | 111 |
|
110 |
| -export class MockedDOMRect extends MockedDOMRectReadOnly implements DOMRect { |
| 112 | +class MockedDOMRect extends MockedDOMRectReadOnly implements DOMRect { |
111 | 113 | constructor(x = 0, y = 0, width = 0, height = 0) {
|
112 | 114 | super(x, y, width, height);
|
113 | 115 | }
|
@@ -149,18 +151,26 @@ export class MockedDOMRect extends MockedDOMRectReadOnly implements DOMRect {
|
149 | 151 | }
|
150 | 152 | }
|
151 | 153 |
|
152 |
| -if (typeof DOMRectReadOnly === 'undefined') { |
153 |
| - Object.defineProperty(window, 'DOMRectReadOnly', { |
154 |
| - writable: true, |
155 |
| - configurable: true, |
156 |
| - value: MockedDOMRectReadOnly, |
157 |
| - }); |
158 |
| -} |
| 154 | +function mockDOMRect() { |
| 155 | + if (!isJsdomEnv()) { |
| 156 | + throw new WrongEnvironmentError(); |
| 157 | + } |
159 | 158 |
|
160 |
| -if (typeof DOMRect === 'undefined') { |
161 |
| - Object.defineProperty(window, 'DOMRect', { |
162 |
| - writable: true, |
163 |
| - configurable: true, |
164 |
| - value: MockedDOMRect, |
165 |
| - }); |
| 159 | + if (typeof DOMRectReadOnly === 'undefined') { |
| 160 | + Object.defineProperty(window, 'DOMRectReadOnly', { |
| 161 | + writable: true, |
| 162 | + configurable: true, |
| 163 | + value: MockedDOMRectReadOnly, |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + if (typeof DOMRect === 'undefined') { |
| 168 | + Object.defineProperty(window, 'DOMRect', { |
| 169 | + writable: true, |
| 170 | + configurable: true, |
| 171 | + value: MockedDOMRect, |
| 172 | + }); |
| 173 | + } |
166 | 174 | }
|
| 175 | + |
| 176 | +export { MockedDOMRectReadOnly, MockedDOMRect, mockDOMRect }; |
0 commit comments