forked from reactjs/react-modal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
136 lines (116 loc) · 3.39 KB
/
helper.js
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
import React from 'react';
import ReactDOM from 'react-dom';
import Modal, { bodyOpenClassName } from '../src/components/Modal';
import TestUtils from 'react-dom/test-utils';
const divStack = [];
/**
* 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;
};
}
/**
* Check if the document.body contains the react modal
* open class.
* @return {Boolean}
*/
export const isBodyWithReactModalOpenClass = (bodyClass = bodyOpenClassName) =>
document.body.className.includes(bodyClass);
/**
* 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, keyCode: code, which: code
}, opts));
const dispatchMockKeyDownEvent = dispatchMockEvent(Simulate.keyDown);
/**
* Dispatch an 'esc' key down event from an element.
*/
export const escKeyDown = dispatchMockKeyDownEvent("ESC", 27);
/**
* Dispatch a 'tab' key down event from an element.
*/
export const tabKeyDown = dispatchMockKeyDownEvent("TAB", 9);
/**
* 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 renderModal = function(props, children, callback) {
props.ariaHideApp = false;
const currentDiv = document.createElement('div');
divStack.push(currentDiv);
document.body.appendChild(currentDiv);
return ReactDOM.render(
<Modal {...props}>{children}</Modal>
, currentDiv, callback);
};
export const unmountModal = function() {
const currentDiv = divStack.pop();
ReactDOM.unmountComponentAtNode(currentDiv);
document.body.removeChild(currentDiv);
};
export const emptyDOM = () => {
while (divStack.length) {
unmountModal();
}
};