[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

chore: split types and provider impl.

userquin (Jul 3, 2024, 8:59 PM +0200) e200033f 292d5036

+179 -174
+56
packages/browser/src/node/commands/pointer-helper.ts
··· 1 + import { parseKeyDef } from '@testing-library/user-event/dist/esm/pointer/parseKeyDef.js' 2 + import { defaultKeyMap } from '@testing-library/user-event/dist/esm/pointer/keyMap.js' 3 + 4 + // @ts-expect-error no types 5 + import type { PointerCoords } from '@testing-library/user-event/dist/types/event' 6 + // @ts-expect-error no types 7 + import type { pointerKey } from '@testing-library/user-event/dist/types/system/pointer' 8 + 9 + // todo: try to add proper types to avoid duplicating 10 + export type PointerActionInput = string | ({ 11 + keys: string 12 + } & PointerActionPosition) | PointerAction 13 + export type PointerInput = PointerActionInput[] 14 + export type PointerAction = PointerPressAction | PointerMoveAction 15 + export interface PointerActionPosition { 16 + target?: string 17 + coords?: PointerCoords 18 + node?: string 19 + /** 20 + * If `node` is set, this is the DOM offset. 21 + * Otherwise this is the `textContent`/`value` offset on the `target`. 22 + */ 23 + offset?: number 24 + } 25 + export interface PointerPressAction extends PointerActionPosition { 26 + keyDef: pointerKey 27 + releasePrevious: boolean 28 + releaseSelf: boolean 29 + } 30 + export interface PointerMoveAction extends PointerActionPosition { 31 + pointerName?: string 32 + } 33 + 34 + // https://github.com/testing-library/user-event/blob/main/src/pointer/index.ts 35 + export function* collectActions(input: PointerInput) { 36 + // collect actions 37 + for (let i = 0; i < input.length; i++) { 38 + const actionInput = input[i] 39 + if (typeof actionInput === 'string') { 40 + for (const i of parseKeyDef(defaultKeyMap, actionInput)) { 41 + yield i 42 + } 43 + } 44 + else if ('keys' in actionInput) { 45 + for (const i of parseKeyDef(defaultKeyMap, actionInput.keys)) { 46 + yield { 47 + ...actionInput, 48 + ...i, 49 + } 50 + } 51 + } 52 + else { 53 + yield actionInput 54 + } 55 + } 56 + }
+3 -174
packages/browser/src/node/commands/pointer.ts
··· 1 - import { parseKeyDef } from '@testing-library/user-event/dist/esm/pointer/parseKeyDef.js' 2 - import { defaultKeyMap } from '@testing-library/user-event/dist/esm/pointer/keyMap.js' 3 - // @ts-expect-error no types 4 - import type { PointerCoords } from '@testing-library/user-event/dist/types/event' 5 - // @ts-expect-error no types 6 - import type { pointerKey } from '@testing-library/user-event/dist/types/system/pointer' 7 1 import type { UserEvent } from '../../../context' 8 2 import { PlaywrightBrowserProvider } from '../providers/playwright' 9 3 import { WebdriverBrowserProvider } from '../providers/webdriver' 10 4 import type { UserEventCommand } from './utils' 11 - 12 - // todo: try to add proper types to avoid duplicating 13 - type PointerActionInput = string | ({ 14 - keys: string 15 - } & PointerActionPosition) | PointerAction 16 - type PointerInput = PointerActionInput[] 17 - type PointerAction = PointerPressAction | PointerMoveAction 18 - interface PointerActionPosition { 19 - target?: string 20 - coords?: PointerCoords 21 - node?: string 22 - /** 23 - * If `node` is set, this is the DOM offset. 24 - * Otherwise this is the `textContent`/`value` offset on the `target`. 25 - */ 26 - offset?: number 27 - } 28 - interface PointerPressAction extends PointerActionPosition { 29 - keyDef: pointerKey 30 - releasePrevious: boolean 31 - releaseSelf: boolean 32 - } 33 - interface PointerMoveAction extends PointerActionPosition { 34 - pointerName?: string 35 - } 5 + import type { PointerInput } from './pointer-helper' 36 6 37 7 export const pointer: UserEventCommand<UserEvent['pointer']> = async ( 38 8 context, ··· 49 19 throw new TypeError(`Provider "${provider.name}" does not support pointer events`) 50 20 } 51 21 else if (provider instanceof WebdriverBrowserProvider) { 52 - await webdriverioPointerImplementation(context.browser, input) 22 + const browser = context.browser 23 + await import('./webdriver-pointer').then(({ webdriverPointerImplementation }) => webdriverPointerImplementation(browser, input)) 53 24 } 54 25 else { 55 26 throw new TypeError(`Provider "${provider.name}" does not support pointer events`) ··· 62 33 ) { 63 34 const actions = provider.browser 64 35 } */ 65 - async function webdriverioPointerImplementation( 66 - browser: WebdriverIO.Browser, 67 - input: PointerInput, 68 - ) { 69 - const actions = collectActions(input) 70 - const targets = new Map<string, WebdriverIO.Element>() 71 - for await (const action of executeWDIOActions(browser, targets, actions)) { 72 - await action.perform() 73 - } 74 - } 75 - 76 - // https://github.com/testing-library/user-event/blob/main/src/pointer/index.ts 77 - function collectActions(input: PointerInput) { 78 - const actions: PointerAction[] = [] 79 - // collect actions 80 - for (let i = 0; i < input.length; i++) { 81 - const actionInput = input[i] 82 - if (typeof actionInput === 'string') { 83 - actions.push(...parseKeyDef(defaultKeyMap, actionInput)) 84 - } 85 - else if ('keys' in actionInput) { 86 - actions.push( 87 - ...parseKeyDef(defaultKeyMap, actionInput.keys).map(i => ({ 88 - ...actionInput, 89 - ...i, 90 - })), 91 - ) 92 - } 93 - else { 94 - actions.push(actionInput) 95 - } 96 - } 97 - 98 - return actions 99 - } 100 - 101 - function createWDIOPointerAction(touch: boolean, browser: WebdriverIO.Browser) { 102 - return browser.action('pointer', { parameters: { pointerType: touch ? 'touch' : 'mouse' } }) 103 - } 104 - 105 - async function* executeWDIOActions( 106 - browser: WebdriverIO.Browser, 107 - targets: Map<string, WebdriverIO.Element>, 108 - actions: PointerAction[], 109 - ) { 110 - let lastAction: { 111 - touch: boolean 112 - action: ReturnType<typeof createWDIOPointerAction> 113 - } = undefined! 114 - for (const action of actions) { 115 - if ('target' in action) { 116 - const target = action.target 117 - if (target && !targets.has(target)) { 118 - targets.set(target, await browser.$(`//${target}`)) 119 - } 120 - } 121 - if ('node' in action) { 122 - const node = action.node 123 - if (node && !targets.has(node)) { 124 - targets.set(node, await browser.$(`//${node}`)) 125 - } 126 - } 127 - if ('keyDef' in action) { 128 - if (lastAction) { 129 - if (lastAction.touch) { 130 - yield lastAction.action 131 - lastAction = { 132 - touch: false, 133 - action: createWDIOPointerAction(false, browser), 134 - } 135 - } 136 - } 137 - else { 138 - lastAction = { 139 - touch: false, 140 - action: createWDIOPointerAction(true, browser), 141 - } 142 - } 143 - if (action.releasePrevious) { 144 - lastAction.action = lastAction.action.up().pause(50) 145 - } 146 - lastAction.action = lastAction.action.down() 147 - if (action.releaseSelf) { 148 - lastAction.action = lastAction.action.up().pause(50) 149 - } 150 - } 151 - else { 152 - if (action.pointerName) { 153 - if (lastAction) { 154 - if (!lastAction.touch) { 155 - yield lastAction.action 156 - lastAction = { 157 - touch: true, 158 - action: createWDIOPointerAction(true, browser), 159 - } 160 - } 161 - } 162 - else { 163 - lastAction = { 164 - touch: true, 165 - action: createWDIOPointerAction(true, browser), 166 - } 167 - } 168 - const params: any = {} 169 - const { x, y } = action.coords ?? {} 170 - if (x !== undefined && y !== undefined) { 171 - params.x = x 172 - params.y = y 173 - } 174 - if (action.target) { 175 - const target = targets.get(action.target) 176 - if (target) { 177 - params.origin = target 178 - } 179 - } 180 - params.button = action.pointerName === 'left' ? 0 : action.pointerName === 'right' ? 2 : 1 181 - lastAction.action = lastAction.action.move(params) 182 - } 183 - else { 184 - if (lastAction) { 185 - if (lastAction.touch) { 186 - yield lastAction.action 187 - lastAction = { 188 - touch: false, 189 - action: createWDIOPointerAction(false, browser), 190 - } 191 - } 192 - } 193 - else { 194 - lastAction = { 195 - touch: false, 196 - action: createWDIOPointerAction(false, browser), 197 - } 198 - } 199 - // todo: check also for coordinates 200 - lastAction.action = lastAction.action.move({ origin: targets.get(action.target!)! }) 201 - } 202 - } 203 - } 204 - 205 - yield lastAction.action 206 - }
+120
packages/browser/src/node/commands/webdriver-pointer.ts
··· 1 + import type { PointerInput } from './pointer-helper' 2 + import { collectActions } from './pointer-helper' 3 + 4 + export async function webdriverPointerImplementation( 5 + browser: WebdriverIO.Browser, 6 + input: PointerInput, 7 + ) { 8 + const targets = new Map<string, WebdriverIO.Element>() 9 + for await (const action of collectWDIOActions(browser, targets, input)) { 10 + await action.perform() 11 + } 12 + } 13 + 14 + // hack to infer types 15 + function createWDIOPointerAction(touch: boolean, browser: WebdriverIO.Browser) { 16 + return browser.action('pointer', { parameters: { pointerType: touch ? 'touch' : 'mouse' } }) 17 + } 18 + 19 + async function* collectWDIOActions( 20 + browser: WebdriverIO.Browser, 21 + targets: Map<string, WebdriverIO.Element>, 22 + input: PointerInput, 23 + ) { 24 + let lastAction: { 25 + touch: boolean 26 + action: ReturnType<typeof createWDIOPointerAction> 27 + } = undefined! 28 + for (const action of collectActions(input)) { 29 + if ('target' in action) { 30 + const target = action.target 31 + if (target && !targets.has(target)) { 32 + targets.set(target, await browser.$(`//${target}`)) 33 + } 34 + } 35 + if ('node' in action) { 36 + const node = action.node 37 + if (node && !targets.has(node)) { 38 + targets.set(node, await browser.$(`//${node}`)) 39 + } 40 + } 41 + if ('keyDef' in action) { 42 + if (lastAction) { 43 + if (lastAction.touch) { 44 + yield lastAction.action 45 + lastAction = { 46 + touch: false, 47 + action: createWDIOPointerAction(false, browser), 48 + } 49 + } 50 + } 51 + else { 52 + lastAction = { 53 + touch: false, 54 + action: createWDIOPointerAction(true, browser), 55 + } 56 + } 57 + if (action.releasePrevious) { 58 + lastAction.action = lastAction.action.up().pause(50) 59 + } 60 + lastAction.action = lastAction.action.down() 61 + if (action.releaseSelf) { 62 + lastAction.action = lastAction.action.up().pause(50) 63 + } 64 + } 65 + else { 66 + if (action.pointerName) { 67 + if (lastAction) { 68 + if (!lastAction.touch) { 69 + yield lastAction.action 70 + lastAction = { 71 + touch: true, 72 + action: createWDIOPointerAction(true, browser), 73 + } 74 + } 75 + } 76 + else { 77 + lastAction = { 78 + touch: true, 79 + action: createWDIOPointerAction(true, browser), 80 + } 81 + } 82 + const params: any = {} 83 + const { x, y } = action.coords ?? {} 84 + if (x !== undefined && y !== undefined) { 85 + params.x = x 86 + params.y = y 87 + } 88 + if (action.target) { 89 + const target = targets.get(action.target) 90 + if (target) { 91 + params.origin = target 92 + } 93 + } 94 + params.button = action.pointerName === 'left' ? 0 : action.pointerName === 'right' ? 2 : 1 95 + lastAction.action = lastAction.action.move(params) 96 + } 97 + else { 98 + if (lastAction) { 99 + if (lastAction.touch) { 100 + yield lastAction.action 101 + lastAction = { 102 + touch: false, 103 + action: createWDIOPointerAction(false, browser), 104 + } 105 + } 106 + } 107 + else { 108 + lastAction = { 109 + touch: false, 110 + action: createWDIOPointerAction(false, browser), 111 + } 112 + } 113 + // todo: check also for coordinates? 114 + lastAction.action = lastAction.action.move({ origin: targets.get(action.target!)! }) 115 + } 116 + } 117 + } 118 + 119 + yield lastAction.action 120 + }