JedWatson / react-tappable

Tappable component for React
http://jedwatson.github.io/react-tappable/
MIT License
862 stars 90 forks source link

Any way to simulate tap? #15

Closed sairion closed 9 years ago

sairion commented 9 years ago

I've been trying my custom component based on react-tappable, but simulating tap is quite a PITA. What would you do for simulating tap in unit test?

(Actually, I've read react-tappable and tappable both source and finding it hard to test, by given nature of tappable lib)

EDIT: Sorry, I've just find out both libs have nothing to do with each other ;) But still wondering about testing method tho.

PS: Would you accept unit test PR?

sairion commented 9 years ago

Well, I ended up doing something like this;

// borrowed from React.addons.TestUtils.nativeTouchData
function tapDataInjector (x, y) {
    return {
        touches: [
            {pageX: x, pageY: y, clientX: x, clientY: y}
        ]
    };
}

function tap (tappableReactElement) {
    React.addons.TestUtils.SimulateNative.touchStart(
        tappableReactElement.getDOMNode(),
        tapDataInjector(0, 0)
    );
    React.addons.TestUtils.SimulateNative.touchEnd(
        tappableReactElement.getDOMNode(),
        tapDataInjector(0, 0)
    );
}

// Now I am able to simulate tap event
tap(someTappableReactElement);

Also, I didn't realize React repo has its own test for tap event: https://github.com/facebook/react/blob/01ef83feef099f6090391b1b5161ad2e4fb3b681/src/browser/__tests__/ReactBrowserEventEmitter-test.js#L323-L339

JedWatson commented 9 years ago

@sairion there is a fair bit of specific logic in this project on top of React's tap event, if you are interested in writing unit tests for it I would be very happy to accept them.