-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: dispatch all UI events per one internal API (#838)
* refactor: centralize calls to createEvent * refactor: centralize dispatching ui events * refactor: apply event props in dispatcher * refactor: inline paste implementation
- Loading branch information
1 parent
a5ca2e4
commit 4720ac2
Showing
37 changed files
with
480 additions
and
364 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
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,98 @@ | ||
import {createEvent as createEventBase} from '@testing-library/dom' | ||
import {eventMap} from '@testing-library/dom/dist/event-map.js' | ||
import {isMouseEvent} from './eventTypes' | ||
import {EventType, PointerCoords} from './types' | ||
|
||
export type EventTypeInit<K extends EventType> = SpecificEventInit< | ||
FixedDocumentEventMap[K] | ||
> | ||
|
||
interface FixedDocumentEventMap extends DocumentEventMap { | ||
input: InputEvent | ||
} | ||
|
||
type SpecificEventInit<E extends Event> = E extends InputEvent | ||
? InputEventInit | ||
: E extends ClipboardEvent | ||
? ClipboardEventInit | ||
: E extends KeyboardEvent | ||
? KeyboardEventInit | ||
: E extends PointerEvent | ||
? PointerEventInit | ||
: E extends MouseEvent | ||
? MouseEventInit | ||
: E extends UIEvent | ||
? UIEventInit | ||
: EventInit | ||
|
||
export function createEvent<K extends EventType>( | ||
type: K, | ||
target: Element, | ||
init?: EventTypeInit<K>, | ||
) { | ||
const eventKey = Object.keys(eventMap).find( | ||
k => k.toLowerCase() === type, | ||
) as keyof typeof createEventBase | ||
|
||
const event = createEventBase[eventKey](target, init) as DocumentEventMap[K] | ||
|
||
// Can not use instanceof, as MouseEvent might be polyfilled. | ||
if (isMouseEvent(type) && init) { | ||
// see https://github.com/testing-library/react-testing-library/issues/268 | ||
assignPositionInit(event as MouseEvent, init) | ||
assignPointerInit(event as PointerEvent, init) | ||
} | ||
|
||
return event | ||
} | ||
|
||
function assignProps( | ||
obj: MouseEvent | PointerEvent, | ||
props: MouseEventInit & PointerEventInit & PointerCoords, | ||
) { | ||
for (const [key, value] of Object.entries(props)) { | ||
Object.defineProperty(obj, key, {get: () => value}) | ||
} | ||
} | ||
|
||
function assignPositionInit( | ||
obj: MouseEvent | PointerEvent, | ||
{ | ||
x, | ||
y, | ||
clientX, | ||
clientY, | ||
offsetX, | ||
offsetY, | ||
pageX, | ||
pageY, | ||
screenX, | ||
screenY, | ||
}: PointerCoords & MouseEventInit, | ||
) { | ||
assignProps(obj, { | ||
/* istanbul ignore start */ | ||
x: x ?? clientX ?? 0, | ||
y: y ?? clientY ?? 0, | ||
clientX: x ?? clientX ?? 0, | ||
clientY: y ?? clientY ?? 0, | ||
offsetX: offsetX ?? 0, | ||
offsetY: offsetY ?? 0, | ||
pageX: pageX ?? 0, | ||
pageY: pageY ?? 0, | ||
screenX: screenX ?? 0, | ||
screenY: screenY ?? 0, | ||
/* istanbul ignore end */ | ||
}) | ||
} | ||
|
||
function assignPointerInit( | ||
obj: MouseEvent | PointerEvent, | ||
{isPrimary, pointerId, pointerType}: PointerEventInit, | ||
) { | ||
assignProps(obj, { | ||
isPrimary, | ||
pointerId, | ||
pointerType, | ||
}) | ||
} |
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,9 @@ | ||
declare module '@testing-library/dom/dist/event-map.js' { | ||
import {EventType} from '@testing-library/dom' | ||
export const eventMap: { | ||
[k in EventType]: { | ||
EventType: string | ||
defaultInit: EventInit | ||
} | ||
} | ||
} |
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,20 @@ | ||
import {eventMap} from '@testing-library/dom/dist/event-map.js' | ||
|
||
const eventKeys = Object.fromEntries( | ||
Object.keys(eventMap).map(k => [k.toLowerCase(), k]), | ||
) as { | ||
[k in keyof DocumentEventMap]: keyof typeof eventMap | ||
} | ||
|
||
function getEventClass(type: keyof DocumentEventMap) { | ||
return eventMap[eventKeys[type]].EventType | ||
} | ||
|
||
const mouseEvents = ['MouseEvent', 'PointerEvent'] | ||
export function isMouseEvent(type: keyof DocumentEventMap) { | ||
return mouseEvents.includes(getEventClass(type)) | ||
} | ||
|
||
export function isKeyboardEvent(type: keyof DocumentEventMap) { | ||
return getEventClass(type) === 'KeyboardEvent' | ||
} |
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,30 @@ | ||
import {Config} from '../setup' | ||
import {getUIEventModifiers} from '../utils' | ||
import {createEvent, EventTypeInit} from './createEvent' | ||
import {isKeyboardEvent, isMouseEvent} from './eventTypes' | ||
import {EventType, PointerCoords} from './types' | ||
import {wrapEvent} from './wrapEvent' | ||
|
||
export type {EventType, PointerCoords} | ||
|
||
export function dispatchUIEvent<K extends EventType>( | ||
config: Config, | ||
target: Element, | ||
type: K, | ||
init?: EventTypeInit<K>, | ||
) { | ||
if (isMouseEvent(type) || isKeyboardEvent(type)) { | ||
init = { | ||
...init, | ||
...getUIEventModifiers(config.keyboardState), | ||
} as EventTypeInit<K> | ||
} | ||
|
||
const event = createEvent(type, target, init) | ||
|
||
return wrapEvent(() => target.dispatchEvent(event), target) | ||
} | ||
|
||
export function bindDispatchUIEvent(config: Config) { | ||
return dispatchUIEvent.bind(undefined, config) | ||
} |
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,14 @@ | ||
export type EventType = keyof DocumentEventMap | ||
|
||
export interface PointerCoords { | ||
x?: number | ||
y?: number | ||
clientX?: number | ||
clientY?: number | ||
offsetX?: number | ||
offsetY?: number | ||
pageX?: number | ||
pageY?: number | ||
screenX?: number | ||
screenY?: number | ||
} |
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,5 @@ | ||
import {getConfig} from '@testing-library/dom' | ||
|
||
export function wrapEvent<R>(cb: () => R, _element: Element) { | ||
return getConfig().eventWrapper(cb) as unknown as R | ||
} |
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.