(opts: {
element: HTMLElement;
advanceTimer: (time: number) => unknown | Promise<unknown>;
pointerOpts?: Record<string, any>;
})
| 65 | * https://testing-library.com/docs/dom-testing-library/api-events/#fireevent for more info. |
| 66 | */ |
| 67 | export async function triggerLongPress(opts: { |
| 68 | element: HTMLElement; |
| 69 | advanceTimer: (time: number) => unknown | Promise<unknown>; |
| 70 | pointerOpts?: Record<string, any>; |
| 71 | }): Promise<void> { |
| 72 | let {element, advanceTimer, pointerOpts = {}} = opts; |
| 73 | let pointerType = pointerOpts.pointerType ?? 'mouse'; |
| 74 | let shouldFireCompatibilityEvents = false; |
| 75 | act(() => { |
| 76 | shouldFireCompatibilityEvents = fireEvent.pointerDown(element, {pointerType, ...pointerOpts}); |
| 77 | }); |
| 78 | let shouldFocus = true; |
| 79 | if (shouldFireCompatibilityEvents) { |
| 80 | if (pointerType === 'touch') { |
| 81 | act(() => { |
| 82 | shouldFocus = shouldFireCompatibilityEvents = fireEvent.touchStart(element, { |
| 83 | targetTouches: [ |
| 84 | { |
| 85 | identifier: pointerOpts.pointerId, |
| 86 | clientX: pointerOpts.clientX, |
| 87 | clientY: pointerOpts.clientY |
| 88 | } |
| 89 | ] |
| 90 | }); |
| 91 | }); |
| 92 | } else if (pointerType === 'mouse') { |
| 93 | act(() => { |
| 94 | shouldFocus = fireEvent.mouseDown(element, pointerOpts); |
| 95 | }); |
| 96 | if (shouldFocus) { |
| 97 | act(() => element.focus()); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | await act(async () => await advanceTimer(DEFAULT_LONG_PRESS_TIME)); |
| 102 | act(() => { |
| 103 | fireEvent.pointerUp(element, {pointerType, ...pointerOpts}); |
| 104 | }); |
| 105 | if (shouldFireCompatibilityEvents) { |
| 106 | if (pointerType === 'touch') { |
| 107 | act(() => { |
| 108 | shouldFocus = fireEvent.touchEnd(element, { |
| 109 | targetTouches: [ |
| 110 | { |
| 111 | identifier: pointerOpts.pointerId, |
| 112 | clientX: pointerOpts.clientX, |
| 113 | clientY: pointerOpts.clientY |
| 114 | } |
| 115 | ] |
| 116 | }); |
| 117 | shouldFocus = fireEvent.mouseDown(element, pointerOpts); |
| 118 | }); |
| 119 | if (shouldFocus) { |
| 120 | act(() => element.focus()); |
| 121 | } |
| 122 | act(() => { |
| 123 | fireEvent.mouseUp(element, pointerOpts); |
| 124 | }); |
no test coverage detected