Skip to content

Instantly share code, notes, and snippets.

@hartzis
Last active November 5, 2018 19:22
Show Gist options
  • Save hartzis/25229842d3802a0c9fe62c3241a71567 to your computer and use it in GitHub Desktop.
Save hartzis/25229842d3802a0c9fe62c3241a71567 to your computer and use it in GitHub Desktop.
Touch Event Testing React Jest Enzyme
import React from 'react';
import EventComponent from './EventComponent';
import { mount } from 'enzyme';
import {
createStartTouchEventObject,
createMoveTouchEventObject
} from './EventHelpers.js';
describe('EventComponent', () => {
it('should render the component', ()=>{
const component = mount((<EventComponent />));
});
it('should calculate when swiped', ()=>{
const component = mount((<EventComponent />));
expect(component.text()).toEqual('Component-');
component.simulate('touchStart',
createStartTouchEventObject({ x: 100, y: 0 }));
component.simulate('touchMove',
createMoveTouchEventObject({ x: 150, y: 0 }));
component.simulate('touchEnd',
createMoveTouchEventObject({ x: 200, y: 0 }));
expect(component.text()).toEqual('Component-swiped');
});
it('should call onSwiped when swiped', ()=>{
const onSwiped = jest.fn();
const component = mount((<EventComponent onSwiped={onSwiped} />));
component.simulate('touchStart',
createStartTouchEventObject({ x: 100, y: 0 }));
component.simulate('touchMove',
createMoveTouchEventObject({ x: 150, y: 0 }));
component.simulate('touchEnd',
createMoveTouchEventObject({ x: 200, y: 0 }));
expect(onSwiped).toHaveBeenCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment