-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Showing
11 changed files
with
388 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ packages/public/badge-* | |
packages/*/index.mjs | ||
playgrounds/*/pnpm-lock.yaml | ||
types | ||
coverage |
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 |
---|---|---|
@@ -1,101 +1,120 @@ | ||
import { promiseTimeout } from '@vueuse/shared' | ||
import type { Ref } from 'vue-demi' | ||
import { ref } from 'vue-demi' | ||
import { useEventListener } from '../useEventListener' | ||
import { onLongPress } from '.' | ||
|
||
describe('onLongPress', () => { | ||
let element: Ref<HTMLElement> | ||
let parentElement: Ref<HTMLElement> | ||
let childElement: Ref<HTMLElement> | ||
let pointerdownEvent: PointerEvent | ||
let pointerUpEvent: PointerEvent | ||
|
||
beforeEach(() => { | ||
element = ref(document.createElement('div')) | ||
async function triggerCallback(isRef: boolean) { | ||
const onLongPressCallback = vi.fn() | ||
onLongPress(isRef ? element : element.value, onLongPressCallback) | ||
element.value.dispatchEvent(pointerdownEvent) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(0) | ||
await promiseTimeout(500) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
} | ||
|
||
pointerdownEvent = new PointerEvent('pointerdown', { cancelable: true, bubbles: true }) | ||
}) | ||
async function triggerCallbackWithDelay(isRef: boolean) { | ||
const onLongPressCallback = vi.fn() | ||
onLongPress(isRef ? element : element.value, onLongPressCallback, { delay: 1000 }) | ||
// first pointer down | ||
element.value.dispatchEvent(pointerdownEvent) | ||
|
||
it('should be defined', () => { | ||
expect(onLongPress).toBeDefined() | ||
}) | ||
// wait for 500ms after pointer down | ||
await promiseTimeout(500) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(0) | ||
|
||
describe('given argument is ref', () => { | ||
describe('given no options', () => { | ||
it('should trigger longpress after 500ms', async () => { | ||
const onLongPressCallback = vi.fn() | ||
onLongPress(element, onLongPressCallback) | ||
element.value.dispatchEvent(pointerdownEvent) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(0) | ||
await promiseTimeout(500) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
// pointer up to cancel callback | ||
element.value.dispatchEvent(pointerUpEvent) | ||
|
||
describe('given options', () => { | ||
it('should trigger longpress after options.delay ms', async () => { | ||
const onLongPressCallback = vi.fn() | ||
onLongPress(element, onLongPressCallback, { delay: 1000 }) | ||
element.value.dispatchEvent(pointerdownEvent) | ||
await promiseTimeout(500) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(0) | ||
await promiseTimeout(500) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
}) | ||
// wait for 500ms after pointer up | ||
await promiseTimeout(500) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(0) | ||
|
||
it('should work with once and prevent modefiers', async () => { | ||
const onLongPressCallback = vi.fn() | ||
onLongPress(element, onLongPressCallback, { modifiers: { once: true, prevent: true } }) | ||
// another pointer down | ||
element.value.dispatchEvent(pointerdownEvent) | ||
|
||
element.value.dispatchEvent(pointerdownEvent) | ||
// wait for 1000ms after pointer down | ||
await promiseTimeout(1000) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
} | ||
|
||
await promiseTimeout(500) | ||
async function notTriggerCallbackOnChildLongPress(isRef: boolean) { | ||
const onLongPressCallback = vi.fn() | ||
onLongPress(isRef ? element : element.value, onLongPressCallback, { modifiers: { self: true } }) | ||
|
||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
expect(pointerdownEvent.defaultPrevented).toBe(true) | ||
childElement.value.dispatchEvent(pointerdownEvent) | ||
|
||
await promiseTimeout(500) | ||
await promiseTimeout(500) | ||
|
||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(0) | ||
} | ||
|
||
describe('given argument is no ref', () => { | ||
describe('given no options', () => { | ||
it('should trigger longpress after 500ms', async () => { | ||
const onLongPressCallback = vi.fn() | ||
onLongPress(element.value, onLongPressCallback) | ||
element.value.dispatchEvent(pointerdownEvent) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(0) | ||
await promiseTimeout(500) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
async function workOnceAndPreventModifiers(isRef: boolean) { | ||
const onLongPressCallback = vi.fn() | ||
onLongPress(isRef ? element : element.value, onLongPressCallback, { modifiers: { once: true, prevent: true } }) | ||
|
||
describe('given options', () => { | ||
it('should trigger longpress after options.delay ms', async () => { | ||
const onLongPressCallback = vi.fn() | ||
onLongPress(element.value, onLongPressCallback, { delay: 1000 }) | ||
element.value.dispatchEvent(pointerdownEvent) | ||
await promiseTimeout(500) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(0) | ||
await promiseTimeout(500) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
}) | ||
element.value.dispatchEvent(pointerdownEvent) | ||
|
||
await promiseTimeout(500) | ||
|
||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
expect(pointerdownEvent.defaultPrevented).toBe(true) | ||
|
||
it('should work with once and prevent modefiers', async () => { | ||
const onLongPressCallback = vi.fn() | ||
onLongPress(element.value, onLongPressCallback, { modifiers: { once: true, prevent: true } }) | ||
await promiseTimeout(500) | ||
|
||
element.value.dispatchEvent(pointerdownEvent) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
} | ||
|
||
await promiseTimeout(500) | ||
async function stopPropagation(isRef: boolean) { | ||
const onLongPressCallback = vi.fn() | ||
const onParentLongPressCallback = vi.fn() | ||
useEventListener(parentElement, 'pointerdown', onParentLongPressCallback) | ||
onLongPress(isRef ? element : element.value, onLongPressCallback, { modifiers: { stop: true } }) | ||
|
||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
expect(pointerdownEvent.defaultPrevented).toBe(true) | ||
element.value.dispatchEvent(pointerdownEvent) | ||
|
||
await promiseTimeout(500) | ||
await promiseTimeout(500) | ||
|
||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
}) | ||
expect(onLongPressCallback).toHaveBeenCalledTimes(1) | ||
expect(onParentLongPressCallback).toHaveBeenCalledTimes(0) | ||
} | ||
|
||
function suites(isRef: boolean) { | ||
describe('given no options', () => { | ||
it('should trigger longpress after 500ms', () => triggerCallback(isRef)) | ||
}) | ||
|
||
describe('given options', () => { | ||
it('should trigger longpress after options.delay ms', () => triggerCallbackWithDelay(isRef)) | ||
it('should not tirgger longpress when child element on longpress', () => notTriggerCallbackOnChildLongPress(isRef)) | ||
it('should work with once and prevent modifiers', () => workOnceAndPreventModifiers(isRef)) | ||
it('should stop propagation', () => stopPropagation(isRef)) | ||
}) | ||
} | ||
|
||
beforeEach(() => { | ||
element = ref(document.createElement('div')) | ||
parentElement = ref(document.createElement('div')) | ||
childElement = ref(document.createElement('div')) | ||
parentElement.value.appendChild(element.value) | ||
element.value.appendChild(childElement.value) | ||
|
||
pointerdownEvent = new PointerEvent('pointerdown', { cancelable: true, bubbles: true }) | ||
pointerUpEvent = new PointerEvent('pointerup', { cancelable: true, bubbles: true }) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(onLongPress).toBeDefined() | ||
}) | ||
|
||
describe('given argument is ref', () => suites(true)) | ||
|
||
describe('given argument is no ref', () => suites(false)) | ||
}) |
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
Oops, something went wrong.