-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
React.test.tsx
226 lines (186 loc) · 5.17 KB
/
React.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import GlobalRegistrator from '../../cjs/GlobalRegistrator.cjs';
import React from 'react';
import ReactDOM from 'react-dom/client';
import { act } from 'react-dom/test-utils';
import ReactComponent from './ReactComponent.js';
const GETTERS = [
'location',
'history',
'navigator',
'screen',
'sessionStorage',
'localStorage',
'opener',
'scrollX',
'pageXOffset',
'scrollY',
'pageYOffset',
'CSS',
'innerWidth',
'innerHeight',
'outerWidth',
'outerHeight',
'devicePixelRatio'
];
async function main(): Promise<void> {
const selfReferringProperties = ['self', 'top', 'parent', 'window'];
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const originalSetTimeout = global.setTimeout;
/**
* Registers Happy DOM globally.
*/
GlobalRegistrator.register();
/**
* Test if all properties defined as getter are included in the global object.
*/
function testGetters(): void {
const included: string[] = [];
const propertyNames = Object.getOwnPropertyNames(global);
for (const name of GETTERS) {
if (propertyNames.includes(name)) {
included.push(name);
}
}
if (included.length !== GETTERS.length) {
throw Error(
`Object.getOwnPropertyNames() did not return all properties defined as getter. Expected: "${GETTERS.join(', ')}", Got: "${included.join(', ')}".`
);
}
}
testGetters();
/**
* Test if it is possible to create a React component and mount it.
*/
async function testReactComponent(): Promise<void> {
const appElement = document.createElement('app');
let root;
document.body.appendChild(appElement);
async function mountReactComponent(): Promise<void> {
act(() => {
root = ReactDOM.createRoot(appElement);
root.render(<ReactComponent />);
});
await new Promise((resolve) => setTimeout(resolve, 2));
if (appElement.innerHTML !== '<div>Test</div>') {
throw Error('React not rendered correctly.');
}
}
function unmountReactComponent(): void {
act(() => {
root.unmount();
});
if (appElement.innerHTML !== '') {
throw Error('React not unmounted correctly.');
}
}
if (global.setTimeout === originalSetTimeout) {
throw Error('Happy DOM function not registered.');
}
for (const property of selfReferringProperties) {
if (global[property] !== global) {
throw Error('Self referring property property was not registered.');
}
}
await mountReactComponent();
unmountReactComponent();
}
await testReactComponent();
/**
* Test if it is possible to set the location.href property and that location isn't replaced to a new object.
*
* @see https://github.com/capricorn86/happy-dom/issues/1230
* */
function testLocationHref(): void {
globalThis.location.href = 'https://example.com/';
if (globalThis.location.href !== 'https://example.com/') {
throw Error('The property "location.href" could not be set.');
}
}
testLocationHref();
/**
* Test CSS.
*/
function testCSS(): void {
const style = document.createElement('style');
document.head.appendChild(style);
style.innerHTML = `
body {
background-color: red;
}
@media (min-width: 1000px) {
body {
background-color: green;
}
}
`;
if (globalThis.getComputedStyle(document.body).backgroundColor !== 'green') {
throw Error('The CSS was not applied correctly.');
}
}
testCSS();
/**
* Unregisters Happy DOM globally.
*/
GlobalRegistrator.unregister();
/**
* Test if all properties defined as getter are removed from the global object.
*/
function testGettersAfterUnregister(): void {
const included: string[] = [];
const propertyNames = Object.getOwnPropertyNames(global);
for (const name of GETTERS) {
if (propertyNames.includes(name)) {
included.push(name);
}
}
// In Node.js v21 and later, the navigator property is available.
if (!included.includes('navigator')) {
included.push('navigator');
}
if (included.length !== 1 || included[0] !== 'navigator') {
throw Error(
`GlobalObserver.unregister() did not remove all properties defined as getter. Expected: "navigator", Got: "${included.join(', ')}".`
);
}
}
testGettersAfterUnregister();
/**
* Test if setTimeout is restored.
*/
function testSetTimeout(): void {
if (global.setTimeout !== originalSetTimeout) {
throw Error('Global property was not restored.');
}
}
testSetTimeout();
/**
* Test registering with options.
*/
function testWindowOptions(): void {
GlobalRegistrator.register({
url: 'https://example.com/',
width: 1920,
height: 1080,
settings: {
navigator: {
userAgent: 'Custom User Agent'
}
}
});
if (globalThis.location.href !== 'https://example.com/') {
throw Error('The option "url" has no affect.');
}
if (globalThis.innerWidth !== 1920) {
throw Error('The option "width" has no affect.');
}
if (globalThis.innerHeight !== 1080) {
throw Error('The option "height" has no affect.');
}
if (globalThis.navigator.userAgent !== 'Custom User Agent') {
throw Error('The option "settings.userAgent" has no affect.');
}
GlobalRegistrator.unregister();
}
testWindowOptions();
}
main();