File tree 2 files changed +36
-4
lines changed
packages/integration-react/src
2 files changed +36
-4
lines changed Original file line number Diff line number Diff line change @@ -74,4 +74,23 @@ function widgetMockCleanup() {
74
74
removeMerkur ( ) ;
75
75
}
76
76
77
- export { mockedWidgetProperties , widgetMockCleanup , widgetMockInit } ;
77
+ function mockGlobalProperty ( propName , value ) {
78
+ const originalDescriptor = Object . getOwnPropertyDescriptor ( global , propName ) ;
79
+ Object . defineProperty ( global , propName , { writable : true } ) ;
80
+ global [ propName ] = value ;
81
+
82
+ return ( ) => {
83
+ if ( originalDescriptor ) {
84
+ Object . defineProperty ( global , propName , originalDescriptor ) ;
85
+ } else {
86
+ delete global [ propName ] ;
87
+ }
88
+ } ;
89
+ }
90
+
91
+ export {
92
+ mockedWidgetProperties ,
93
+ mockGlobalProperty ,
94
+ widgetMockCleanup ,
95
+ widgetMockInit ,
96
+ } ;
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { shallow } from 'enzyme';
3
3
4
4
import {
5
5
mockedWidgetProperties ,
6
+ mockGlobalProperty ,
6
7
widgetMockCleanup ,
7
8
widgetMockInit ,
8
9
} from '../__mocks__/widgetMock' ;
@@ -27,6 +28,8 @@ describe('AbstractMerkurWidget', () => {
27
28
let widgetProperties = null ;
28
29
let instance = null ;
29
30
let wrapper = null ;
31
+ let restoreWindow ;
32
+ let restoreDocument ;
30
33
31
34
beforeEach ( ( ) => {
32
35
// Cache mocked widget data
@@ -47,6 +50,16 @@ describe('AbstractMerkurWidget', () => {
47
50
afterEach ( ( ) => {
48
51
widgetMockCleanup ( ) ;
49
52
jest . clearAllMocks ( ) ;
53
+
54
+ if ( restoreWindow ) {
55
+ restoreWindow ( ) ;
56
+ restoreWindow = null ;
57
+ }
58
+
59
+ if ( restoreDocument ) {
60
+ restoreDocument ( ) ;
61
+ restoreDocument = null ;
62
+ }
50
63
} ) ;
51
64
52
65
describe ( 'html getter' , ( ) => {
@@ -192,14 +205,14 @@ describe('AbstractMerkurWidget', () => {
192
205
193
206
describe ( '_isClient() method' , ( ) => {
194
207
it ( 'should return false for non-browser environments' , ( ) => {
195
- delete global . window ;
208
+ restoreWindow = mockGlobalProperty ( ' window' , undefined ) ;
196
209
197
210
expect ( instance . _isClient ( ) ) . toBe ( false ) ;
198
211
} ) ;
199
212
200
213
it ( 'should return true for browser environments' , ( ) => {
201
- global . window = { } ;
202
- global . document = { } ;
214
+ restoreWindow = mockGlobalProperty ( 'window' , { } ) ;
215
+ restoreDocument = mockGlobalProperty ( 'document' , { } ) ;
203
216
204
217
expect ( instance . _isClient ( ) ) . toBe ( true ) ;
205
218
} ) ;
You can’t perform that action at this time.
0 commit comments