-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pull request #103
Pull request #103
Changes from all commits
da7de2b
aa5db64
c3d0772
1cf4e46
ff969cc
f571f2f
170ec0c
171440f
0392e90
5c945d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* global window */ | ||
const react = require('react'); | ||
// Resolution for requestAnimationFrame not supported in jest error : | ||
// https://github.com/facebook/react/issues/9102#issuecomment-283873039 | ||
global.window = global; | ||
|
||
window.addEventListener = () => {}; | ||
window.requestAnimationFrame = () => { | ||
throw new Error('requestAnimationFrame is not supported in Node'); | ||
}; | ||
|
||
module.exports = react; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -340,6 +340,7 @@ describe('Swipeable', () => { | |
wrapper.unmount(); | ||
}); | ||
|
||
|
||
describe('preventDefaultTouchmoveEvent and passive support eventListener option', () => { | ||
beforeAll(() => { | ||
DetectPassiveEvents.hasSupport = true; | ||
|
@@ -379,4 +380,209 @@ describe('Swipeable', () => { | |
expect(swipeableDiv.prop('onTouchMove')).toBe(instance.eventMove); | ||
}); | ||
}); | ||
|
||
describe('Handle Rotation by 90 degree: ', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for adding tests!!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'course - won't work without. ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, a question: the tests are mainly data-driven. Is it okay to refactor them into data-driven tests with less duplication? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm all for cleaning up the tests and if you'd like to do that, could it be another PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, external PR would be best |
||
let wrapper = {}; | ||
let touchHere = {}; | ||
let swipeFuncs = {}; | ||
let onTap = () => {}; | ||
|
||
beforeEach(() => { | ||
swipeFuncs = getMockedSwipeFunctions(); | ||
onTap = jest.fn(); | ||
|
||
wrapper = mount(( | ||
<Swipeable | ||
{...swipeFuncs} | ||
onTap={onTap} | ||
rotationAngle={90} | ||
> | ||
<span>Touch Here</span> | ||
</Swipeable> | ||
)); | ||
|
||
touchHere = wrapper.find('span'); | ||
}); | ||
|
||
afterEach(() => { | ||
wrapper.unmount(); | ||
touchHere = {}; | ||
}); | ||
|
||
it('handles swipe direction to the right', () => { | ||
touchHere.simulate('touchStart', createStartTouchEventObject({ x: 100, y: 100 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 100, y: 125 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 100, y: 150 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 100, y: 175 })); | ||
touchHere.simulate('touchEnd', createMoveTouchEventObject({ x: 100, y: 200 })); | ||
|
||
expect(swipeFuncs.onSwipedDown).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingDown).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipedUp).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingUp).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipedLeft).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingLeft).not.toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwipedRight).toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingRight).toHaveBeenCalledTimes(3); | ||
|
||
expect(onTap).not.toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwiped).toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwiping).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('handles swipe direction to the left', () => { | ||
touchHere.simulate('touchStart', createStartTouchEventObject({ x: 100, y: 100 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 100, y: 75 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 100, y: 50 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 100, y: 25 })); | ||
touchHere.simulate('touchEnd', createMoveTouchEventObject({ x: 100, y: 0 })); | ||
|
||
expect(swipeFuncs.onSwipedDown).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingDown).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipedUp).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingUp).not.toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwipedLeft).toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingLeft).toHaveBeenCalledTimes(3); | ||
|
||
expect(swipeFuncs.onSwipedRight).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingRight).not.toHaveBeenCalled(); | ||
expect(onTap).not.toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwiped).toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwiping).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('handles swipe direction upwards', () => { | ||
touchHere.simulate('touchStart', createStartTouchEventObject({ x: 100, y: 100 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 125, y: 100 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 150, y: 100 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 175, y: 100 })); | ||
touchHere.simulate('touchEnd', createMoveTouchEventObject({ x: 200, y: 100 })); | ||
|
||
expect(swipeFuncs.onSwipedDown).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingDown).not.toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwipedUp).toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingUp).toHaveBeenCalledTimes(3); | ||
|
||
expect(swipeFuncs.onSwipedLeft).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingLeft).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipedRight).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingRight).not.toHaveBeenCalled(); | ||
expect(onTap).not.toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwiped).toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwiping).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('handles swipe direction downwards', () => { | ||
touchHere.simulate('touchStart', createStartTouchEventObject({ x: 100, y: 100 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 75, y: 100 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 50, y: 100 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 25, y: 100 })); | ||
touchHere.simulate('touchEnd', createMoveTouchEventObject({ x: 0, y: 100 })); | ||
|
||
expect(swipeFuncs.onSwipedDown).toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingDown).toHaveBeenCalledTimes(3); | ||
|
||
expect(swipeFuncs.onSwipedUp).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingUp).not.toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwipedLeft).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingLeft).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipedRight).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingRight).not.toHaveBeenCalled(); | ||
expect(onTap).not.toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwiped).toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwiping).toHaveBeenCalledTimes(3); | ||
}); | ||
}); | ||
|
||
|
||
it('Handle Rotation by negative 90 degree', () => { | ||
const swipeFuncs = getMockedSwipeFunctions(); | ||
const onTap = jest.fn(); | ||
|
||
const wrapper = mount(( | ||
<Swipeable | ||
{...swipeFuncs} | ||
onTap={onTap} | ||
rotationAngle={-90} | ||
> | ||
<span>Touch Here</span> | ||
</Swipeable> | ||
)); | ||
|
||
const touchHere = wrapper.find('span'); | ||
|
||
touchHere.simulate('touchStart', createStartTouchEventObject({ x: 100, y: 100 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 100, y: 125 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 100, y: 150 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 100, y: 175 })); | ||
touchHere.simulate('touchEnd', createMoveTouchEventObject({ x: 100, y: 200 })); | ||
|
||
expect(swipeFuncs.onSwipedDown).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingDown).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipedUp).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingUp).not.toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwipedLeft).toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingLeft).toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwipedRight).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingRight).not.toHaveBeenCalledTimes(3); | ||
|
||
expect(onTap).not.toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwiped).toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwiping).toHaveBeenCalledTimes(3); | ||
|
||
wrapper.unmount(); | ||
}); | ||
|
||
|
||
it('Handle Rotation by more than 360 degree', () => { | ||
const swipeFuncs = getMockedSwipeFunctions(); | ||
const onTap = jest.fn(); | ||
|
||
const wrapper = mount(( | ||
<Swipeable | ||
{...swipeFuncs} | ||
onTap={onTap} | ||
rotationAngle={360 + 270} | ||
> | ||
<span>Touch Here</span> | ||
</Swipeable> | ||
)); | ||
|
||
const touchHere = wrapper.find('span'); | ||
|
||
touchHere.simulate('touchStart', createStartTouchEventObject({ x: 100, y: 100 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 100, y: 125 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 100, y: 150 })); | ||
touchHere.simulate('touchMove', createMoveTouchEventObject({ x: 100, y: 175 })); | ||
touchHere.simulate('touchEnd', createMoveTouchEventObject({ x: 100, y: 200 })); | ||
|
||
expect(swipeFuncs.onSwipedDown).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingDown).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipedUp).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingUp).not.toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwipedLeft).toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingLeft).toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwipedRight).not.toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwipingRight).not.toHaveBeenCalledTimes(3); | ||
|
||
expect(onTap).not.toHaveBeenCalled(); | ||
|
||
expect(swipeFuncs.onSwiped).toHaveBeenCalled(); | ||
expect(swipeFuncs.onSwiping).toHaveBeenCalledTimes(3); | ||
|
||
wrapper.unmount(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we "lock" this to a range of
0 - 360
? via a new proptype?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We thought about the same, but in the end it does not matter for the calculation - so why lock the range.