|
| 1 | +import type { SerializedLocator } from '@vitest/browser' |
| 2 | +import type { Locator, UserEventPointerInputNormalized } from 'vitest/browser' |
| 3 | +import type { BrowserCommandContext } from 'vitest/node' |
| 4 | +import type { UserEventCommand } from './utils' |
| 5 | +import { parseKeyDef } from '@vitest/browser' |
| 6 | +import { click } from './click' |
| 7 | +import { hover } from './hover' |
| 8 | + |
| 9 | +type SerializedPointerInput = ElementToSerializedLocator<UserEventPointerInputNormalized[number]> |
| 10 | +interface PointerReturnData extends Pick<SerializedPointerInput, 'coords' | 'target'> { |
| 11 | + unreleased: string[] |
| 12 | +} |
| 13 | +type PointerEvent = ( |
| 14 | + input: readonly SerializedPointerInput[], |
| 15 | + state: Partial<PointerReturnData>, |
| 16 | +) => Promise<PointerReturnData> |
| 17 | + |
| 18 | +type ElementToSerializedLocator<T> = T extends Element | Locator |
| 19 | + ? SerializedLocator |
| 20 | + : { |
| 21 | + [K in keyof T]: ElementToSerializedLocator<T[K]> |
| 22 | + } |
| 23 | + |
| 24 | +export const pointer: UserEventCommand<PointerEvent> = async (context, input, state = {}) => { |
| 25 | + const pressedKeys = new Set<string>(state.unreleased) |
| 26 | + let lastTarget: SerializedPointerInput['target'] = state.target |
| 27 | + let lastCoords: SerializedPointerInput['coords'] = state.coords |
| 28 | + |
| 29 | + for (const option of input) { |
| 30 | + let target = option.target |
| 31 | + let coords = option.coords |
| 32 | + |
| 33 | + if (target || coords) { |
| 34 | + lastTarget = target |
| 35 | + lastCoords = coords |
| 36 | + } else { |
| 37 | + target = lastTarget |
| 38 | + coords = lastCoords |
| 39 | + } |
| 40 | + |
| 41 | + const pointerAction = { ...option, target, coords } |
| 42 | + const keys = 'keys' in option ? option.keys : null |
| 43 | + const parsedKeys = keys === null ? null : groupKeyDefs(parseKeyDef(keys)) |
| 44 | + const hasOnlyClickActions = parsedKeys?.every( |
| 45 | + ({ |
| 46 | + keyDef: { |
| 47 | + keyDef: { code }, |
| 48 | + releasePrevious, |
| 49 | + releaseSelf, |
| 50 | + }, |
| 51 | + }) => code === 'MouseLeft' && !releasePrevious && releaseSelf, |
| 52 | + ) |
| 53 | + |
| 54 | + // click has its own moving logic, no need to move twice |
| 55 | + if (!hasOnlyClickActions) { |
| 56 | + if (target) { |
| 57 | + await hover(context, target, { |
| 58 | + position: coords, |
| 59 | + }) |
| 60 | + } else if (coords) { |
| 61 | + await context.page.mouse.move(coords.x, coords.y) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (parsedKeys) { |
| 66 | + for (const key of parsedKeys) { |
| 67 | + await keyDefHandler(key, pointerAction, pressedKeys, context) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return { |
| 73 | + unreleased: Array.from(pressedKeys), |
| 74 | + target: lastTarget, |
| 75 | + coords: lastCoords, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +type KeyDefOutput = ReturnType<typeof parseKeyDef>[number] |
| 80 | +interface GroupedKeyDef { |
| 81 | + times: number |
| 82 | + keyDef: KeyDefOutput |
| 83 | +} |
| 84 | + |
| 85 | +function groupKeyDefs(keyDefs: readonly KeyDefOutput[]): GroupedKeyDef[] { |
| 86 | + const output: GroupedKeyDef[] = [] |
| 87 | + let last: GroupedKeyDef | undefined |
| 88 | + |
| 89 | + for (const keyDef of keyDefs) { |
| 90 | + if ( |
| 91 | + last !== undefined && |
| 92 | + // for our purpose we can ignore the `repeat` property |
| 93 | + last.keyDef.releasePrevious === keyDef.releasePrevious && |
| 94 | + last.keyDef.releaseSelf === keyDef.releaseSelf && |
| 95 | + last.keyDef.keyDef.key === keyDef.keyDef.key && |
| 96 | + last.keyDef.keyDef.code === keyDef.keyDef.code && |
| 97 | + last.keyDef.keyDef.location === keyDef.keyDef.location |
| 98 | + ) { |
| 99 | + last.times += keyDef.repeat |
| 100 | + } else { |
| 101 | + last = { times: keyDef.repeat, keyDef } |
| 102 | + output.push(last) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return output |
| 107 | +} |
| 108 | + |
| 109 | +const MOUSE_KEYS = ['MouseLeft', 'MouseRight', 'MouseMiddle'] |
| 110 | + |
| 111 | +async function keyDefHandler( |
| 112 | + { keyDef: { keyDef, releasePrevious, releaseSelf }, times }: GroupedKeyDef, |
| 113 | + pointerAction: Omit<SerializedPointerInput, 'keys'>, |
| 114 | + pressedKeys: Set<string>, |
| 115 | + context: BrowserCommandContext, |
| 116 | +) { |
| 117 | + const key = keyDef.key! |
| 118 | + const code = keyDef.code! |
| 119 | + |
| 120 | + if (MOUSE_KEYS.includes(code)) { |
| 121 | + const button = code.replace('Mouse', '').toLowerCase() as 'left' | 'right' | 'middle' |
| 122 | + const mouseOptions = { |
| 123 | + button, |
| 124 | + clickCount: times, |
| 125 | + } satisfies Parameters<(typeof context)['page']['mouse']['up']>[0] |
| 126 | + |
| 127 | + if (releasePrevious) { |
| 128 | + await context.page.mouse.up(mouseOptions) |
| 129 | + } else if (releaseSelf) { |
| 130 | + if (pointerAction.target) { |
| 131 | + await click(context, pointerAction.target, { |
| 132 | + ...mouseOptions, |
| 133 | + position: pointerAction.coords, |
| 134 | + }) |
| 135 | + } else { |
| 136 | + const x = pointerAction.coords?.x ?? 0 |
| 137 | + const y = pointerAction.coords?.y ?? 0 |
| 138 | + |
| 139 | + await context.page.mouse.click(x, y, mouseOptions) |
| 140 | + } |
| 141 | + } else { |
| 142 | + await context.page.mouse.down(mouseOptions) |
| 143 | + } |
| 144 | + |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + if (key === 'Unknown') { |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + if (!releasePrevious) { |
| 153 | + if (releaseSelf) { |
| 154 | + for (let count = 0; count < times; count += 1) { |
| 155 | + await context.page.keyboard.press(key) |
| 156 | + } |
| 157 | + } else { |
| 158 | + await context.page.keyboard.down(key) |
| 159 | + pressedKeys.add(key) |
| 160 | + } |
| 161 | + } else if (pressedKeys.has(key)) { |
| 162 | + await context.page.keyboard.up(key) |
| 163 | + pressedKeys.delete(key) |
| 164 | + } |
| 165 | +} |
0 commit comments