From 9fdf9ec063abd83a2b5c8caf87b45e15554c592d Mon Sep 17 00:00:00 2001 From: Janpot <2109932+Janpot@users.noreply.github.com> Date: Fri, 8 Aug 2025 09:57:55 +0200 Subject: [PATCH] Make sure the default document is the correct instance --- src/setup/setup.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/setup/setup.ts b/src/setup/setup.ts index 9475a4c2..d1133244 100644 --- a/src/setup/setup.ts +++ b/src/setup/setup.ts @@ -19,8 +19,10 @@ import {DirectOptions} from './directApi' /** * Default options applied when API is called per `userEvent.anyApi()` + * We use a function to make sure the document is always the instance + * of the time the API is called. */ -const defaultOptionsDirect: Required = { +const getDefaultOptionsDirect = (): Required => ({ applyAccept: true, autoModify: true, delay: 0, @@ -33,15 +35,15 @@ const defaultOptionsDirect: Required = { skipHover: false, writeToClipboard: false, advanceTimers: () => Promise.resolve(), -} +}) /** * Default options applied when API is called per `userEvent().anyApi()` */ -const defaultOptionsSetup: Required = { - ...defaultOptionsDirect, +const getDefaultOptionsSetup = (): Required => ({ + ...getDefaultOptionsDirect(), writeToClipboard: true, -} +}) export type UserEventApi = typeof userEventApi @@ -65,7 +67,7 @@ export type Config = Required export function createConfig( options: Options = {}, - defaults: Required = defaultOptionsSetup, + defaults: Required = getDefaultOptionsSetup(), node?: Node, ): Config { const document = getDocument(options, node, defaults) @@ -101,10 +103,10 @@ export function setupDirect( keyboardState, pointerState, ...options - }: DirectOptions & {keyboardState?: System, pointerState?: System} = {}, // backward-compatibility + }: DirectOptions & {keyboardState?: System; pointerState?: System} = {}, // backward-compatibility node?: Node, ) { - const config = createConfig(options, defaultOptionsDirect, node) + const config = createConfig(options, getDefaultOptionsDirect(), node) prepareDocument(config.document) patchFocus(getWindow(config.document).HTMLElement) @@ -146,9 +148,9 @@ export function createInstance( config: Config, system: System = new System(), ): { - instance: Instance - api: UserEvent - } { + instance: Instance + api: UserEvent +} { const instance = {} as Instance Object.assign(instance, { config,