fdd2629795
* use esbuild for React instead of tsdx * remove tsdx from Vue * use consistent names * add jest and prettier * update scripts * ignore some folders for prettier * run lint script instead of tsdx lint * run prettier en-masse This has a few changes because of the new prettier version. * bump typescript to latest version * make typescript happy * cleanup playground package.json * make esbuild a dev dependency * make scripts consistent * fix husky hooks * add dedicated watch script * add `yarn playground-react` and `yarn react-playground` (alias) This will make sure to run a watcher for the actual @headlessui/react package, and start a development server in the playground-react package. * ignore formatting in the .next folder * run prettier on playground-react package * setup playground-vue Still not 100% working, but getting there! * add playground aliases in @headlessui/vue and @headlessui/react This allows you to run `yarn react playground` or `yarn vue playground` from the root. * add `clean` script * move examples folder in playground-vue to root * ensure new lines for consistency in scripts * fix typescript issue * fix typescript issues in playgrounds * make sure to run prettier on everything it can * run prettier on all files * improve error output If you minify the code, then it could happen that the errors are a bit obscure. This will hardcode the component name to improve errors. * add the `prettier-plugin-tailwindcss` plugin, party! * update changelog
356 lines
9.5 KiB
TypeScript
356 lines
9.5 KiB
TypeScript
import { fireEvent } from '@testing-library/react'
|
|
import { disposables } from '../utils/disposables'
|
|
|
|
let d = disposables()
|
|
|
|
function nextFrame(cb: Function): void {
|
|
setImmediate(() =>
|
|
setImmediate(() => {
|
|
cb()
|
|
})
|
|
)
|
|
}
|
|
|
|
export let Keys: Record<string, Partial<KeyboardEvent>> = {
|
|
Space: { key: ' ', keyCode: 32, charCode: 32 },
|
|
Enter: { key: 'Enter', keyCode: 13, charCode: 13 },
|
|
Escape: { key: 'Escape', keyCode: 27, charCode: 27 },
|
|
Backspace: { key: 'Backspace', keyCode: 8 },
|
|
|
|
ArrowLeft: { key: 'ArrowLeft', keyCode: 37 },
|
|
ArrowUp: { key: 'ArrowUp', keyCode: 38 },
|
|
ArrowRight: { key: 'ArrowRight', keyCode: 39 },
|
|
ArrowDown: { key: 'ArrowDown', keyCode: 40 },
|
|
|
|
Home: { key: 'Home', keyCode: 36 },
|
|
End: { key: 'End', keyCode: 35 },
|
|
|
|
PageUp: { key: 'PageUp', keyCode: 33 },
|
|
PageDown: { key: 'PageDown', keyCode: 34 },
|
|
|
|
Tab: { key: 'Tab', keyCode: 9, charCode: 9 },
|
|
}
|
|
|
|
export function shift(event: Partial<KeyboardEvent>) {
|
|
return { ...event, shiftKey: true }
|
|
}
|
|
|
|
export function word(input: string): Partial<KeyboardEvent>[] {
|
|
let result = input.split('').map((key) => ({ key }))
|
|
|
|
d.enqueue(() => {
|
|
let element = document.activeElement
|
|
|
|
if (element instanceof HTMLInputElement) {
|
|
fireEvent.change(element, {
|
|
target: Object.assign({}, element, { value: input }),
|
|
})
|
|
}
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
let Default = Symbol()
|
|
let Ignore = Symbol()
|
|
|
|
let cancellations: Record<string | typeof Default, Record<string, Set<string>>> = {
|
|
[Default]: {
|
|
keydown: new Set(['keypress']),
|
|
keypress: new Set([]),
|
|
keyup: new Set([]),
|
|
},
|
|
[Keys.Enter.key!]: {
|
|
keydown: new Set(['keypress', 'click']),
|
|
keypress: new Set(['click']),
|
|
keyup: new Set([]),
|
|
},
|
|
[Keys.Space.key!]: {
|
|
keydown: new Set(['keypress', 'click']),
|
|
keypress: new Set([]),
|
|
keyup: new Set(['click']),
|
|
},
|
|
[Keys.Tab.key!]: {
|
|
keydown: new Set(['keypress', 'blur', 'focus']),
|
|
keypress: new Set([]),
|
|
keyup: new Set([]),
|
|
},
|
|
}
|
|
|
|
let order: Record<
|
|
string | typeof Default,
|
|
((
|
|
element: Element,
|
|
event: Partial<KeyboardEvent | MouseEvent>
|
|
) => boolean | typeof Ignore | Element)[]
|
|
> = {
|
|
[Default]: [
|
|
function keydown(element, event) {
|
|
return fireEvent.keyDown(element, event)
|
|
},
|
|
function keypress(element, event) {
|
|
return fireEvent.keyPress(element, event)
|
|
},
|
|
function input(element, event) {
|
|
return fireEvent.input(element, event)
|
|
},
|
|
function keyup(element, event) {
|
|
return fireEvent.keyUp(element, event)
|
|
},
|
|
],
|
|
[Keys.Enter.key!]: [
|
|
function keydown(element, event) {
|
|
return fireEvent.keyDown(element, event)
|
|
},
|
|
function keypress(element, event) {
|
|
return fireEvent.keyPress(element, event)
|
|
},
|
|
function click(element, event) {
|
|
if (element instanceof HTMLButtonElement) return fireEvent.click(element, event)
|
|
return Ignore
|
|
},
|
|
function keyup(element, event) {
|
|
return fireEvent.keyUp(element, event)
|
|
},
|
|
],
|
|
[Keys.Space.key!]: [
|
|
function keydown(element, event) {
|
|
return fireEvent.keyDown(element, event)
|
|
},
|
|
function keypress(element, event) {
|
|
return fireEvent.keyPress(element, event)
|
|
},
|
|
function keyup(element, event) {
|
|
return fireEvent.keyUp(element, event)
|
|
},
|
|
function click(element, event) {
|
|
if (element instanceof HTMLButtonElement) return fireEvent.click(element, event)
|
|
return Ignore
|
|
},
|
|
],
|
|
[Keys.Tab.key!]: [
|
|
function keydown(element, event) {
|
|
return fireEvent.keyDown(element, event)
|
|
},
|
|
function blurAndfocus(_element, event) {
|
|
return focusNext(event)
|
|
},
|
|
function keyup(element, event) {
|
|
return fireEvent.keyUp(element, event)
|
|
},
|
|
],
|
|
}
|
|
|
|
export async function type(events: Partial<KeyboardEvent>[], element = document.activeElement) {
|
|
jest.useFakeTimers()
|
|
|
|
try {
|
|
if (element === null) return expect(element).not.toBe(null)
|
|
|
|
for (let event of events) {
|
|
let skip = new Set()
|
|
let actions = order[event.key!] ?? order[Default as any]
|
|
for (let action of actions) {
|
|
let checks = action.name.split('And')
|
|
if (checks.some((check) => skip.has(check))) continue
|
|
|
|
let result = action(element, {
|
|
type: action.name,
|
|
charCode: event.key?.length === 1 ? event.key?.charCodeAt(0) : undefined,
|
|
...event,
|
|
})
|
|
if (result === Ignore) continue
|
|
if (result instanceof Element) {
|
|
element = result
|
|
}
|
|
|
|
let cancelled = !result
|
|
if (cancelled) {
|
|
let skippablesForKey = cancellations[event.key!] ?? cancellations[Default as any]
|
|
let skippables = skippablesForKey?.[action.name] ?? new Set()
|
|
|
|
for (let skippable of skippables) skip.add(skippable)
|
|
}
|
|
}
|
|
}
|
|
|
|
// We don't want to actually wait in our tests, so let's advance
|
|
jest.runAllTimers()
|
|
|
|
await d.workQueue()
|
|
|
|
await new Promise(nextFrame)
|
|
} catch (err) {
|
|
if (err instanceof Error) Error.captureStackTrace(err, type)
|
|
throw err
|
|
} finally {
|
|
jest.useRealTimers()
|
|
}
|
|
}
|
|
|
|
export async function press(event: Partial<KeyboardEvent>, element = document.activeElement) {
|
|
return type([event], element)
|
|
}
|
|
|
|
export enum MouseButton {
|
|
Left = 0,
|
|
Right = 2,
|
|
}
|
|
|
|
export async function click(
|
|
element: Document | Element | Window | Node | null,
|
|
button = MouseButton.Left
|
|
) {
|
|
try {
|
|
if (element === null) return expect(element).not.toBe(null)
|
|
|
|
let options = { button }
|
|
|
|
if (button === MouseButton.Left) {
|
|
// Cancel in pointerDown cancels mouseDown, mouseUp
|
|
let cancelled = !fireEvent.pointerDown(element, options)
|
|
if (!cancelled) {
|
|
fireEvent.mouseDown(element, options)
|
|
}
|
|
|
|
// Ensure to trigger a `focus` event if the element is focusable, or within a focusable element
|
|
let next: HTMLElement | null = element as HTMLElement | null
|
|
while (next !== null) {
|
|
if (next.matches(focusableSelector)) {
|
|
next.focus()
|
|
break
|
|
}
|
|
next = next.parentElement
|
|
}
|
|
|
|
fireEvent.pointerUp(element, options)
|
|
if (!cancelled) {
|
|
fireEvent.mouseUp(element, options)
|
|
}
|
|
fireEvent.click(element, options)
|
|
} else if (button === MouseButton.Right) {
|
|
// Cancel in pointerDown cancels mouseDown, mouseUp
|
|
let cancelled = !fireEvent.pointerDown(element, options)
|
|
if (!cancelled) {
|
|
fireEvent.mouseDown(element, options)
|
|
}
|
|
|
|
// Only in Firefox:
|
|
fireEvent.pointerUp(element, options)
|
|
if (!cancelled) {
|
|
fireEvent.mouseUp(element, options)
|
|
}
|
|
}
|
|
|
|
await new Promise(nextFrame)
|
|
} catch (err) {
|
|
if (err instanceof Error) Error.captureStackTrace(err, click)
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export async function focus(element: Document | Element | Window | Node | null) {
|
|
try {
|
|
if (element === null) return expect(element).not.toBe(null)
|
|
|
|
fireEvent.focus(element)
|
|
|
|
await new Promise(nextFrame)
|
|
} catch (err) {
|
|
if (err instanceof Error) Error.captureStackTrace(err, focus)
|
|
throw err
|
|
}
|
|
}
|
|
export async function mouseEnter(element: Document | Element | Window | null) {
|
|
try {
|
|
if (element === null) return expect(element).not.toBe(null)
|
|
|
|
fireEvent.pointerOver(element)
|
|
fireEvent.pointerEnter(element)
|
|
fireEvent.mouseOver(element)
|
|
|
|
await new Promise(nextFrame)
|
|
} catch (err) {
|
|
if (err instanceof Error) Error.captureStackTrace(err, mouseEnter)
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export async function mouseMove(element: Document | Element | Window | null) {
|
|
try {
|
|
if (element === null) return expect(element).not.toBe(null)
|
|
|
|
fireEvent.pointerMove(element)
|
|
fireEvent.mouseMove(element)
|
|
|
|
await new Promise(nextFrame)
|
|
} catch (err) {
|
|
if (err instanceof Error) Error.captureStackTrace(err, mouseMove)
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export async function mouseLeave(element: Document | Element | Window | null) {
|
|
try {
|
|
if (element === null) return expect(element).not.toBe(null)
|
|
|
|
fireEvent.pointerOut(element)
|
|
fireEvent.pointerLeave(element)
|
|
fireEvent.mouseOut(element)
|
|
fireEvent.mouseLeave(element)
|
|
|
|
await new Promise(nextFrame)
|
|
} catch (err) {
|
|
if (err instanceof Error) Error.captureStackTrace(err, mouseLeave)
|
|
throw err
|
|
}
|
|
}
|
|
|
|
// ---
|
|
|
|
function focusNext(event: Partial<KeyboardEvent>) {
|
|
let direction = event.shiftKey ? -1 : +1
|
|
let focusableElements = getFocusableElements()
|
|
let total = focusableElements.length
|
|
|
|
function innerFocusNext(offset = 0): Element {
|
|
let currentIdx = focusableElements.indexOf(document.activeElement as HTMLElement)
|
|
let next = focusableElements[(currentIdx + total + direction + offset) % total] as HTMLElement
|
|
|
|
if (next) next?.focus({ preventScroll: true })
|
|
|
|
if (next !== document.activeElement) return innerFocusNext(offset + direction)
|
|
return next
|
|
}
|
|
|
|
return innerFocusNext()
|
|
}
|
|
|
|
// Credit:
|
|
// - https://stackoverflow.com/a/30753870
|
|
let focusableSelector = [
|
|
'[contentEditable=true]',
|
|
'[tabindex]',
|
|
'a[href]',
|
|
'area[href]',
|
|
'button:not([disabled])',
|
|
'iframe',
|
|
'input:not([disabled])',
|
|
'select:not([disabled])',
|
|
'textarea:not([disabled])',
|
|
]
|
|
.map(
|
|
process.env.NODE_ENV === 'test'
|
|
? // TODO: Remove this once JSDOM fixes the issue where an element that is
|
|
// "hidden" can be the document.activeElement, because this is not possible
|
|
// in real browsers.
|
|
(selector) => `${selector}:not([tabindex='-1']):not([style*='display: none'])`
|
|
: (selector) => `${selector}:not([tabindex='-1'])`
|
|
)
|
|
.join(',')
|
|
|
|
function getFocusableElements(container = document.body) {
|
|
if (!container) return []
|
|
return Array.from(container.querySelectorAll(focusableSelector))
|
|
}
|