-
Notifications
You must be signed in to change notification settings - Fork 812
Expand file tree
/
Copy pathhelper.js
More file actions
265 lines (232 loc) · 6.47 KB
/
helper.js
File metadata and controls
265 lines (232 loc) · 6.47 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import React from "react";
import ReactDOM from "react-dom";
import Modal, { bodyOpenClassName } from "../src/components/Modal";
import TestUtils from "react-dom/test-utils";
import { log as classListLog } from "../src/helpers/classList";
import { log as focusManagerLog } from "../src/helpers/focusManager";
import { log as ariaAppLog } from "../src/helpers/ariaAppHider";
import { log as bodyTrapLog } from "../src/helpers/bodyTrap";
import { log as portalInstancesLog } from "../src/helpers/portalOpenInstances";
const debug = false;
let i = 0;
/**
* This log is used to see if there are leaks in between tests.
*/
export function log(label, spaces) {
if (!debug) return;
console.log(`${label} -----------------`);
console.log(document.body.children.length);
const logChildren = c => console.log(c.nodeName, c.className, c.id);
document.body.children.forEach(logChildren);
ariaAppLog();
bodyTrapLog();
classListLog();
focusManagerLog();
portalInstancesLog();
console.log(`end ${label} -----------------` + (!spaces ? '' : `
`));
}
let elementPool = [];
/**
* Every HTMLElement must be requested using this function...
* and inside `withElementCollector`.
*/
export function createHTMLElement(name) {
const e = document.createElement(name);
elementPool[elementPool.length - 1].push(e);
e.className = `element_pool_${name}-${++i}`;
return e;
}
/**
* Remove every element from its parent and release the pool.
*/
export function drainPool(pool) {
pool.forEach(e => e.parentNode && e.parentNode.removeChild(e));
}
/**
* Every HTMLElement must be requested inside this function...
* The reason is that it provides a mechanism that disposes
* all the elements (built with `createHTMLElement`) after a test.
*/
export function withElementCollector(work) {
let r;
let poolIndex = elementPool.length;
elementPool[poolIndex] = [];
try {
r = work();
} finally {
drainPool(elementPool[poolIndex]);
elementPool = elementPool.slice(
0, poolIndex
);
}
return r;
}
/**
* Polyfill for String.includes on some node versions.
*/
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) {
if (typeof start !== "number") {
start = 0;
}
if (start + search.length > this.length) {
return false;
}
return this.indexOf(search, start) !== -1;
};
}
/**
* hide the native close watcher; we don't have a way to test it yet,
* the implementation will fall back to using normal keydown events
* as usual.
*/
if (window.CloseWatcher) {
delete window.CloseWatcher;
}
/**
* Return the class list object from `document.body`.
* @return {Array}
*/
export const documentClassList = () => document.body.classList;
/**
* Check if the document.body contains the react modal
* open class.
* @return {Boolean}
*/
export const isDocumentWithReactModalOpenClass = (
bodyClass = bodyOpenClassName
) => document.body.className.includes(bodyClass);
/**
* Return the class list object from <html />.
* @return {Array}
*/
export const htmlClassList = () =>
document.getElementsByTagName("html")[0].classList;
/**
* Check if the html contains the react modal
* open class.
* @return {Boolean}
*/
export const isHtmlWithReactModalOpenClass = htmlClass =>
htmlClassList().contains(htmlClass);
/**
* Returns a rendered dom element by class.
* @param {React} element A react instance.
* @param {String} className A class to find.
* @return {DOMElement}
*/
export const findDOMWithClass = TestUtils.findRenderedDOMComponentWithClass;
/**
* Returns an attribut of a rendered react tree.
* @param {React} component A react instance.
* @return {String}
*/
const getModalAttribute = component => (instance, attr) =>
modalComponent(component)(instance).getAttribute(attr);
/**
* Return an element from a react component.
* @param {React} A react instance.
* @return {DOMElement}
*/
const modalComponent = component => instance => instance.portal[component];
/**
* Returns the modal content.
* @param {Modal} modal Modal instance.
* @return {DOMElement}
*/
export const mcontent = modalComponent("content");
/**
* Returns the modal overlay.
* @param {Modal} modal Modal instance.
* @return {DOMElement}
*/
export const moverlay = modalComponent("overlay");
/**
* Return an attribute of modal content.
* @param {Modal} modal Modal instance.
* @return {String}
*/
export const contentAttribute = getModalAttribute("content");
/**
* Return an attribute of modal overlay.
* @param {Modal} modal Modal instance.
* @return {String}
*/
export const overlayAttribute = getModalAttribute("overlay");
const Simulate = TestUtils.Simulate;
const dispatchMockEvent = eventCtor => (key, code) => (element, opts) =>
eventCtor(
element,
Object.assign(
{},
{
key: key,
which: code
},
code,
opts
)
);
const dispatchMockKeyDownEvent = dispatchMockEvent(Simulate.keyDown);
/**
* @deprecated will be replaced by `escKeyDownWithCode` when `react-modal`
* drops support for React <18.
*
* Dispatch an 'esc' key down event using the legacy KeyboardEvent.keyCode.
*/
export const escKeyDown = dispatchMockKeyDownEvent("ESC", { keyCode: 27 });
/**
* Dispatch an 'esc' key down event.
*/
export const escKeyDownWithCode = dispatchMockKeyDownEvent("ESC", {
code: "Escape"
});
/**
* @deprecated will be replaced by `escKeyDownWithCode` when `react-modal`
* drops support for React <18.
*
* Dispatch a 'tab' key down event using the legacy KeyboardEvent.keyCode.
*/
export const tabKeyDown = dispatchMockKeyDownEvent("TAB", { keyCode: 9 });
/**
* Dispatch a 'tab' key down event.
*/
export const tabKeyDownWithCode = dispatchMockKeyDownEvent("TAB", {
code: "Tab"
});
/**
* Dispatch a 'click' event at a node.
*/
export const clickAt = Simulate.click;
/**
* Dispatch a 'mouse up' event at a node.
*/
export const mouseUpAt = Simulate.mouseUp;
/**
* Dispatch a 'mouse down' event at a node.
*/
export const mouseDownAt = Simulate.mouseDown;
export const noop = () => {};
/**
* Request a managed modal to run the tests on.
*
*/
export const withModal = function(props, children, test = noop) {
return withElementCollector(() => {
const node = createHTMLElement();
const modalProps = { ariaHideApp: false, ...props };
let modal;
try {
ReactDOM.render(
<Modal ref={m => (modal = m)} {...modalProps}>
{children}
</Modal>,
node
);
test(modal);
} finally {
ReactDOM.unmountComponentAtNode(node);
}
});
};