1 | import React from 'react';
|
2 | import TestUtils from 'react-addons-test-utils';
|
3 |
|
4 | export const documentEvent = (eventName, props={}) => {
|
5 | const evt = Object.assign(document.createEvent("HTMLEvents"), props);
|
6 | evt.initEvent(eventName, true, true);
|
7 | document.dispatchEvent(evt);
|
8 | };
|
9 |
|
10 | export const renderComponent = component => {
|
11 | const _component = React.createFactory(component);
|
12 | return props => TestUtils.renderIntoDocument(_component(props, <div></div>));
|
13 | };
|
14 |
|
15 | export const ExampleComponent = () => <div></div>;
|
16 |
|
17 | export const nativeTouch = (x, y) => ({touches: [{ clientX: x, clientY: y }]});
|
18 |
|
19 | export const createFakeRaf = () => {
|
20 | const FRAME_LENGTH = 1000 / 60;
|
21 |
|
22 | let callbacks = [];
|
23 | let time = 0;
|
24 | let id = 0;
|
25 |
|
26 | const raf = callback => {
|
27 | id += 1;
|
28 | callbacks.push({ callback, id });
|
29 | return id;
|
30 | };
|
31 |
|
32 | raf.cancel = cancelId => {
|
33 | callbacks = callbacks.filter(item => item.id !== cancelId);
|
34 | };
|
35 |
|
36 | raf.step = (steps=1) => {
|
37 | for (let i = 0; i < steps; i++) {
|
38 | time += FRAME_LENGTH;
|
39 |
|
40 | callbacks.forEach(({ callback }) => callback(time));
|
41 | callbacks = [];
|
42 | }
|
43 | };
|
44 |
|
45 | return raf;
|
46 | };
|