Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export callbacks for hooks #1270

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/document/patchFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const patched = Symbol('patched focus/blur methods')

declare global {
interface HTMLElement {
readonly [patched]?: Pick<HTMLElement, 'focus' | 'blur'>
[patched]?: Pick<HTMLElement, 'focus' | 'blur'>
}
}

Expand Down Expand Up @@ -93,11 +93,17 @@ export function restoreFocus(HTMLElement: typeof globalThis['HTMLElement']) {
Object.defineProperties(HTMLElement.prototype, {
focus: {
configurable: true,
get: () => focus,
value: focus,
writable: true,
},
blur: {
configurable: true,
get: () => blur,
value: blur,
writable: true,
},
[patched]: {
configurable: true,
get: () => undefined,
},
})
}
Expand Down
26 changes: 25 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
// the default export is kept for backward compatibility only...
export {userEvent as default} from './setup'

export {userEvent} from './setup'
export {userEvent, prepare, reset, detach} from './setup'
export type {UserEvent} from './setup/setup'
export type {keyboardKey} from './system/keyboard'
export type {pointerKey} from './system/pointer'
export {PointerEventsCheckLevel, type Options} from './options'

import {reset, detach} from './setup'

const g = globalThis as {
afterEach?: (cb?: () => void) => void
afterAll?: (cb?: () => void) => void
window?: Window & typeof globalThis
}

if (typeof g.afterEach === 'function') {
g.afterEach(() => {
if (g.window) {
reset(g.window)
}
})
}

if (typeof g.afterAll === 'function') {
g.afterAll(() => {
if (g.window) {
detach(g.window)
}
})
}
6 changes: 6 additions & 0 deletions src/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ export const userEvent = {
...directApi,
setup: setupMain,
} as const

export {
prepare,
reset,
detach,
} from './setup'
57 changes: 49 additions & 8 deletions src/setup/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {patchFocus} from '../document/patchFocus'
import {patchFocus, restoreFocus} from '../document/patchFocus'
import {prepareDocument} from '../document/prepareDocument'
import {dispatchEvent, dispatchUIEvent} from '../event'
import {defaultKeyMap as defaultKeyboardMap} from '../keyboard/keyMap'
Expand All @@ -7,8 +7,10 @@ import {Options, PointerEventsCheckLevel} from '../options'
import {
ApiLevel,
attachClipboardStubToView,
detachClipboardStubFromView,
getDocumentFromNode,
getWindow,
resetClipboardStubOnView,
setLevelRef,
wait,
} from '../utils'
Expand Down Expand Up @@ -83,12 +85,9 @@ export function createConfig(
*/
export function setupMain(options: Options = {}) {
const config = createConfig(options)
prepareDocument(config.document)
patchFocus(getWindow(config.document).HTMLElement)

const view =
config.document.defaultView ?? /* istanbul ignore next */ globalThis.window
attachClipboardStubToView(view)
const view = getWindow(config.document)
_prepare(view)

return createInstance(config).api
}
Expand All @@ -105,8 +104,9 @@ export function setupDirect(
node?: Node,
) {
const config = createConfig(options, defaultOptionsDirect, node)
prepareDocument(config.document)
patchFocus(getWindow(config.document).HTMLElement)

const view = getWindow(config.document)
_prepare(view, false)

const system = pointerState ?? keyboardState ?? new System()

Expand Down Expand Up @@ -182,3 +182,44 @@ function getDocument(
options.document ?? (node && getDocumentFromNode(node)) ?? defaults.document
)
}

function _prepare(
view: Window & typeof globalThis,
attachClipboardStub = true,
) {
prepareDocument(view.document)
patchFocus(view.HTMLElement)

if (attachClipboardStub) {
attachClipboardStubToView(view)
}
}

/**
* @experimental
*/
export function prepare(
view: Window & typeof globalThis,
) {
_prepare(view)
}

/**
* @experimental
*/
export function reset(
view: Window & typeof globalThis,
) {
restoreFocus(view.HTMLElement)
resetClipboardStubOnView(view)
}

/**
* @experimental
*/
export function detach(
view: Window & typeof globalThis,
) {
restoreFocus(view.HTMLElement)
detachClipboardStubFromView(view)
}
14 changes: 0 additions & 14 deletions src/utils/dataTransfer/Clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,3 @@ export async function writeDataTransferToClipboard(
throw new Error('The Clipboard API is unavailable.')
}
}

const g = globalThis as {
afterEach?: (cb?: () => void) => void
afterAll?: (cb?: () => void) => void
}
/* istanbul ignore else */
if (typeof g.afterEach === 'function') {
g.afterEach(() => resetClipboardStubOnView(globalThis.window))
}

/* istanbul ignore else */
if (typeof g.afterAll === 'function') {
g.afterAll(() => detachClipboardStubFromView(globalThis.window))
}