UNPKG

1.2 kBJavaScriptView Raw
1import React from 'react';
2import TestUtils from 'react-addons-test-utils';
3
4export 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
10export const renderComponent = component => {
11 const _component = React.createFactory(component);
12 return props => TestUtils.renderIntoDocument(_component(props, <div></div>));
13};
14
15export const ExampleComponent = () => <div></div>;
16
17export const nativeTouch = (x, y) => ({touches: [{ clientX: x, clientY: y }]});
18
19export const createFakeRaf = () => {
20 const FRAME_LENGTH = 1000 / 60; // assume 60fps for now
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 // eslint-disable-next-line no-loop-func
40 callbacks.forEach(({ callback }) => callback(time));
41 callbacks = [];
42 }
43 };
44
45 return raf;
46};