Skip to content

Commit 0bef94b

Browse files
author
Ondřej Baše
committed
fix: 🐛 Fix failing unit tests
1 parent 6a862de commit 0bef94b

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

Diff for: packages/integration-react/src/__mocks__/widgetMock.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,23 @@ function widgetMockCleanup() {
7474
removeMerkur();
7575
}
7676

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+
};

Diff for: packages/integration-react/src/__tests__/AbstractMerkurWidgetSpec.jsx

+16-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { shallow } from 'enzyme';
33

44
import {
55
mockedWidgetProperties,
6+
mockGlobalProperty,
67
widgetMockCleanup,
78
widgetMockInit,
89
} from '../__mocks__/widgetMock';
@@ -27,6 +28,8 @@ describe('AbstractMerkurWidget', () => {
2728
let widgetProperties = null;
2829
let instance = null;
2930
let wrapper = null;
31+
let restoreWindow;
32+
let restoreDocument;
3033

3134
beforeEach(() => {
3235
// Cache mocked widget data
@@ -47,6 +50,16 @@ describe('AbstractMerkurWidget', () => {
4750
afterEach(() => {
4851
widgetMockCleanup();
4952
jest.clearAllMocks();
53+
54+
if (restoreWindow) {
55+
restoreWindow();
56+
restoreWindow = null;
57+
}
58+
59+
if (restoreDocument) {
60+
restoreDocument();
61+
restoreDocument = null;
62+
}
5063
});
5164

5265
describe('html getter', () => {
@@ -192,14 +205,14 @@ describe('AbstractMerkurWidget', () => {
192205

193206
describe('_isClient() method', () => {
194207
it('should return false for non-browser environments', () => {
195-
delete global.window;
208+
restoreWindow = mockGlobalProperty('window', undefined);
196209

197210
expect(instance._isClient()).toBe(false);
198211
});
199212

200213
it('should return true for browser environments', () => {
201-
global.window = {};
202-
global.document = {};
214+
restoreWindow = mockGlobalProperty('window', {});
215+
restoreDocument = mockGlobalProperty('document', {});
203216

204217
expect(instance._isClient()).toBe(true);
205218
});

0 commit comments

Comments
 (0)