forked from moroshko/react-autosuggest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
356 lines (291 loc) · 9.69 KB
/
helpers.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import chai, { expect } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import ReactDom from 'react-dom';
import TestUtils, { Simulate } from 'react-dom/test-utils';
chai.use(sinonChai);
const clock = sinon.useFakeTimers();
let app, container, input, suggestionsContainer, clearButton;
let eventsArray = [];
export const { tick } = clock;
export const clearEvents = () => {
eventsArray = [];
};
export const addEvent = (event) => {
eventsArray.push(event);
};
export const getEvents = () => {
return eventsArray;
};
export const init = (application) => {
app = application;
container = TestUtils.findRenderedDOMComponentWithClass(
app,
'react-autosuggest__container'
);
input = TestUtils.findRenderedDOMComponentWithClass(
app,
'react-autosuggest__input'
);
suggestionsContainer = TestUtils.findRenderedDOMComponentWithClass(
app,
'react-autosuggest__suggestions-container'
);
clearButton = TestUtils.scryRenderedDOMComponentsWithTag(app, 'button')[0];
};
// Since react-dom doesn't export SyntheticEvent anymore
export const syntheticEventMatcher = sinon.match((value) => {
if (typeof value !== 'object' || value === null) {
return false;
}
const proto = Object.getPrototypeOf(value);
if ('_dispatchListeners' in value && proto && proto.constructor.Interface) {
return true;
}
return false;
}, 'of SyntheticEvent type');
export const childrenMatcher = sinon.match.any;
export const containerPropsMatcher = sinon.match({
id: sinon.match.string,
key: sinon.match.string,
className: sinon.match.string,
ref: sinon.match.func,
});
const reactAttributesRegex = / data-react[-\w]+="[^"]+"/g;
// See: http://stackoverflow.com/q/28979533/247243
const stripReactAttributes = (html) => html.replace(reactAttributesRegex, '');
export const getInnerHTML = (element) =>
stripReactAttributes(element.innerHTML);
export const getElementWithClass = (className) =>
TestUtils.findRenderedDOMComponentWithClass(app, className);
export const expectContainerAttribute = (attributeName, expectedValue) => {
expect(container.getAttribute(attributeName)).to.equal(expectedValue);
};
export const expectInputAttribute = (attributeName, expectedValue) => {
expect(input.getAttribute(attributeName)).to.equal(expectedValue);
};
export const getSuggestionsContainerAttribute = (attributeName) =>
suggestionsContainer.getAttribute(attributeName);
export const expectInputValue = (expectedValue) => {
expect(input.value).to.equal(expectedValue);
};
export const getSuggestionsList = () =>
TestUtils.findRenderedDOMComponentWithClass(
app,
'react-autosuggest__suggestions-list'
);
export const getSuggestions = () =>
TestUtils.scryRenderedDOMComponentsWithClass(
app,
'react-autosuggest__suggestion'
);
export const getSuggestion = (suggestionIndex) => {
const suggestions = getSuggestions();
if (suggestionIndex >= suggestions.length) {
throw Error(
`
Cannot find suggestion #${suggestionIndex}.
${
suggestions.length === 0
? 'No suggestions found.'
: `Only ${suggestions.length} suggestion${
suggestions.length === 1 ? '' : 's'
} found.`
}
`
);
}
return suggestions[suggestionIndex];
};
export const expectSuggestionAttribute = (
suggestionIndex,
attributeName,
expectedValue
) => {
expect(getSuggestion(suggestionIndex).getAttribute(attributeName)).to.equal(
expectedValue
);
};
export const getTitles = () =>
TestUtils.scryRenderedDOMComponentsWithClass(
app,
'react-autosuggest__section-title'
);
export const getTitle = (titleIndex) => {
const titles = getTitles();
if (titleIndex >= titles.length) {
throw Error(`Cannot find title #${titleIndex}`);
}
return titles[titleIndex];
};
export const expectInputReferenceToBeSet = () => {
expect(app.input).to.equal(input);
};
export const expectSuggestions = (expectedSuggestions) => {
const suggestions = getSuggestions().map(
(suggestion) => suggestion.textContent
);
expect(suggestions).to.deep.equal(expectedSuggestions);
};
export const expectHighlightedSuggestion = (suggestion) => {
const highlightedSuggestions = TestUtils.scryRenderedDOMComponentsWithClass(
app,
'react-autosuggest__suggestion--highlighted'
);
if (suggestion === null) {
expect(highlightedSuggestions).to.have.length(0);
} else {
expect(highlightedSuggestions).to.have.length(1);
expect(highlightedSuggestions[0].textContent).to.equal(suggestion);
}
};
export const saveKeyDown = (event) => {
let runBrowserDefault = event.defaultPrevented
? 'defaultPrevented'
: 'runBrowserDefault';
addEvent('onKeyDown-' + runBrowserDefault);
};
export const expectLetBrowserHandleKeyDown = () => {
expect(getEvents()).to.not.deep.include('onKeyDown-defaultPrevented');
expect(getEvents()).to.deep.include('onKeyDown-runBrowserDefault');
};
export const expectDontLetBrowserHandleKeyDown = () => {
expect(getEvents()).to.not.deep.include('onKeyDown-runBrowserDefault');
expect(getEvents()).to.deep.include('onKeyDown-defaultPrevented');
};
export const mouseEnterSuggestion = (suggestionIndex) => {
Simulate.mouseEnter(getSuggestion(suggestionIndex));
};
export const mouseLeaveSuggestion = (suggestionIndex) => {
Simulate.mouseLeave(getSuggestion(suggestionIndex));
};
export const mouseDownSuggestion = (suggestionIndex) => {
Simulate.mouseDown(getSuggestion(suggestionIndex));
};
const mouseDownDocument = (target) => {
document.dispatchEvent(
new window.CustomEvent('mousedown', {
detail: {
// must be 'detail' accoring to docs: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events#Adding_custom_data_–_CustomEvent()
target,
},
})
);
};
export const mouseUpDocument = (target) => {
document.dispatchEvent(
new window.CustomEvent(
'mouseup',
target
? {
detail: {
// must be 'detail' accoring to docs: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events#Adding_custom_data_–_CustomEvent()
target,
},
}
: null
)
);
};
const touchStartSuggestion = (suggestionIndex) => {
Simulate.touchStart(getSuggestion(suggestionIndex));
};
const touchMoveSuggestion = (suggestionIndex) => {
Simulate.touchMove(getSuggestion(suggestionIndex));
};
// It doesn't feel right to emulate all the DOM events by copying the implementation.
// Please show me a better way to emulate this.
export const clickSuggestion = (suggestionIndex) => {
const suggestion = getSuggestion(suggestionIndex);
mouseEnterSuggestion(suggestionIndex);
mouseDownDocument(suggestion);
mouseDownSuggestion(suggestionIndex);
mouseUpDocument(suggestion);
blurInput();
focusInput();
Simulate.click(suggestion);
clock.tick(1);
};
// Simulates only mouse events since on touch devices dragging considered as a scroll and is a different case.
export const dragSuggestionOut = (suggestionIndex) => {
const suggestion = getSuggestion(suggestionIndex);
mouseEnterSuggestion(suggestionIndex);
mouseDownDocument(suggestion);
mouseDownSuggestion(suggestionIndex);
mouseLeaveSuggestion(suggestionIndex);
mouseUpDocument();
};
export const dragSuggestionOutAndIn = (suggestionIndex) => {
const suggestion = getSuggestion(suggestionIndex);
mouseEnterSuggestion(suggestionIndex);
mouseDownDocument(suggestion);
mouseDownSuggestion(suggestionIndex);
mouseLeaveSuggestion(suggestionIndex);
mouseEnterSuggestion(suggestionIndex);
mouseUpDocument();
blurInput();
focusInput();
Simulate.click(suggestion);
clock.tick(1);
};
// Simulates mouse events as well as touch events since some browsers (chrome) mirror them and we should handle this.
// Order of events is implemented according to docs: https://developer.mozilla.org/en-US/docs/Web/API/Touch_events/Supporting_both_TouchEvent_and_MouseEvent
export const dragSuggestionOutTouch = (suggestionIndex) => {
touchStartSuggestion(suggestionIndex);
touchMoveSuggestion(suggestionIndex);
mouseDownSuggestion(suggestionIndex);
mouseUpDocument();
};
export const clickSuggestionsContainer = () => {
mouseDownDocument(suggestionsContainer);
blurInput();
focusInput();
};
export const focusInput = () => {
Simulate.focus(input);
};
export const blurInput = () => {
Simulate.blur(input);
};
export const clickEscape = () => {
Simulate.keyDown(input, { key: 'Escape', keyCode: 27 }); // throws if key is missing
};
export const clickEnter = () => {
Simulate.keyDown(input, { key: 'Enter', keyCode: 13 }); // throws if key is missing
clock.tick(1);
};
// See #388
export const clickCombinedCharacterEnter = () => {
Simulate.keyDown(input, { key: 'Enter', keyCode: 229 }); // throws if key is missing
clock.tick(1);
};
export const clickDown = (count = 1) => {
for (let i = 0; i < count; i++) {
Simulate.keyDown(input, { key: 'ArrowDown', keyCode: 40 }); // throws if key is missing
}
};
export const clickUp = (count = 1) => {
for (let i = 0; i < count; i++) {
Simulate.keyDown(input, { key: 'ArrowUp', keyCode: 38 }); // throws if key is missing
}
};
export const setInputValue = (value) => {
input.value = value;
Simulate.change(input);
};
export const focusAndSetInputValue = (value) => {
focusInput();
setInputValue(value);
};
export const isInputFocused = () => document.activeElement === input;
export const clickClearButton = () => {
if (clearButton) {
Simulate.mouseDown(clearButton);
} else {
throw new Error("Clear button doesn't exist");
}
};
export const unmountApp = () => {
// eslint-disable-next-line react/no-find-dom-node
ReactDom.unmountComponentAtNode(ReactDom.findDOMNode(app).parentNode);
};