generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f4845d
commit a79a5f9
Showing
9 changed files
with
276 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import React, { useRef } from 'react' | ||
import { fireEvent, render, screen } from '@testing-library/react' | ||
|
||
import { SwipeCallback, useSwipe } from './useSwipe' | ||
|
||
describe('Test suite for useSwipe', () => { | ||
function renderComponent(callback: SwipeCallback) { | ||
const TestComponent = () => { | ||
const ref = useRef<HTMLDivElement>(null) | ||
useSwipe(ref, callback) | ||
return ( | ||
<div ref={ref} style={{ height: '100%' }}> | ||
Swipe | ||
</div> | ||
) | ||
} | ||
|
||
return render(<TestComponent />) | ||
} | ||
|
||
it('should test useSwipe with touch events', () => { | ||
const callback = vi.fn() | ||
renderComponent(callback) | ||
|
||
const div = screen.getByText('Swipe') | ||
expect(callback).toHaveBeenCalledTimes(0) | ||
|
||
// Swipe left | ||
fireEvent.touchStart(div, { | ||
changedTouches: [{ clientX: 100, clientY: 0 }], | ||
}) | ||
fireEvent.touchEnd(div, { | ||
changedTouches: [{ clientX: 10, clientY: 0 }], | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(callback).toHaveBeenCalledWith('left') | ||
|
||
// Swipe right | ||
fireEvent.touchStart(div, { | ||
changedTouches: [{ clientX: 10, clientY: 0 }], | ||
}) | ||
fireEvent.touchEnd(div, { | ||
changedTouches: [{ clientX: 100, clientY: 0 }], | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(2) | ||
expect(callback).toHaveBeenCalledWith('right') | ||
|
||
// Swipe down | ||
fireEvent.touchStart(div, { | ||
changedTouches: [{ clientX: 0, clientY: 10 }], | ||
}) | ||
fireEvent.touchEnd(div, { | ||
changedTouches: [{ clientX: 0, clientY: 100 }], | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(3) | ||
expect(callback).toHaveBeenCalledWith('down') | ||
|
||
// Swipe up | ||
fireEvent.touchStart(div, { | ||
changedTouches: [{ clientX: 0, clientY: 100 }], | ||
}) | ||
fireEvent.touchEnd(div, { | ||
changedTouches: [{ clientX: 0, clientY: 10 }], | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(4) | ||
expect(callback).toHaveBeenCalledWith('up') | ||
}) | ||
|
||
it('should test useSwipe with mouse events', () => { | ||
const original = window.ontouchstart | ||
delete window.ontouchstart | ||
|
||
const callback = vi.fn() | ||
renderComponent(callback) | ||
|
||
const div = screen.getByText('Swipe') | ||
expect(callback).toHaveBeenCalledTimes(0) | ||
|
||
// Swipe left | ||
fireEvent.mouseDown(div, { clientX: 100, clientY: 0 }) | ||
fireEvent.mouseUp(div, { clientX: 10, clientY: 0 }) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(callback).toHaveBeenCalledWith('left') | ||
|
||
// Swipe right | ||
fireEvent.mouseDown(div, { clientX: 10, clientY: 0 }) | ||
fireEvent.mouseUp(div, { clientX: 100, clientY: 0 }) | ||
expect(callback).toHaveBeenCalledTimes(2) | ||
expect(callback).toHaveBeenCalledWith('right') | ||
|
||
// Swipe down | ||
fireEvent.mouseDown(div, { clientX: 0, clientY: 10 }) | ||
fireEvent.mouseUp(div, { clientX: 0, clientY: 100 }) | ||
expect(callback).toHaveBeenCalledTimes(3) | ||
expect(callback).toHaveBeenCalledWith('down') | ||
|
||
// Swipe up | ||
fireEvent.mouseDown(div, { clientX: 0, clientY: 100 }) | ||
fireEvent.mouseUp(div, { clientX: 0, clientY: 10 }) | ||
expect(callback).toHaveBeenCalledTimes(4) | ||
expect(callback).toHaveBeenCalledWith('up') | ||
|
||
// Too small distance | ||
fireEvent.mouseDown(div, { clientX: 0, clientY: 0 }) | ||
fireEvent.mouseUp(div, { clientX: 10, clientY: 10 }) | ||
expect(callback).toHaveBeenCalledTimes(4) | ||
|
||
window.ontouchstart = original | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { RefObject, useEffect, useRef } from 'react' | ||
|
||
export type SwipeDirection = 'left' | 'right' | 'up' | 'down' | ||
export type SwipeCallback = (swipeDirection: SwipeDirection) => void | ||
|
||
export function useSwipe( | ||
ref: RefObject<HTMLElement | null | undefined>, | ||
callback: SwipeCallback, | ||
minDistance = 30, | ||
) { | ||
const callbackRef = useRef<SwipeCallback>(callback) | ||
callbackRef.current = callback | ||
const minDistanceRef = useRef<number>(minDistance) | ||
minDistanceRef.current = minDistance | ||
const startRef = useRef<[number, number]>([0, 0]) | ||
|
||
function eventEnd(endX: number, endY: number) { | ||
const [startX, startY] = startRef.current | ||
const diffX = endX - startX | ||
const diffY = endY - startY | ||
if (Math.max(Math.abs(diffX), Math.abs(diffY)) >= minDistanceRef.current) { | ||
let direction: SwipeDirection | ||
if (Math.abs(diffX) > Math.abs(diffY)) { | ||
direction = diffX < 0 ? 'left' : 'right' | ||
} else { | ||
direction = diffY < 0 ? 'up' : 'down' | ||
} | ||
callbackRef.current(direction) | ||
} | ||
} | ||
|
||
function onTouchStart(e: TouchEvent) { | ||
const [touchSrc] = e.changedTouches | ||
if (touchSrc) { | ||
startRef.current = [touchSrc.clientX, touchSrc.clientY] | ||
} | ||
} | ||
|
||
function onTouchEnd(e: TouchEvent) { | ||
const [touchSrc] = e.changedTouches | ||
if (touchSrc) { | ||
eventEnd(touchSrc.clientX, touchSrc.clientY) | ||
} | ||
} | ||
|
||
function onMouseDown(e: MouseEvent) { | ||
startRef.current = [e.clientX, e.clientY] | ||
} | ||
|
||
function onMouseUp(e: MouseEvent) { | ||
eventEnd(e.clientX, e.clientY) | ||
} | ||
|
||
useEffect(() => { | ||
const el = ref.current | ||
const isTouch = 'ontouchstart' in window | ||
if (el) { | ||
if (isTouch) { | ||
el.addEventListener('touchstart', onTouchStart) | ||
el.addEventListener('touchend', onTouchEnd) | ||
} else { | ||
el.addEventListener('mousedown', onMouseDown) | ||
el.addEventListener('mouseup', onMouseUp) | ||
} | ||
} | ||
return () => { | ||
if (el) { | ||
if (isTouch) { | ||
el.removeEventListener('touchstart', onTouchStart) | ||
el.removeEventListener('touchend', onTouchEnd) | ||
} else { | ||
el.removeEventListener('mousedown', onMouseDown) | ||
el.removeEventListener('mouseup', onMouseUp) | ||
} | ||
} | ||
} | ||
}, [ref]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.