Skip to content

setup({ writeToClipboard: true}) does not write to clipboard #1289

Open
@ghengeveld

Description

@ghengeveld

Reproduction example

https://codesandbox.io/p/sandbox/modest-hermann-pp9wgz

Prerequisites

  1. Run userEvent.setup({ writeToClipboard: true }) at the start of a script
  2. Call navigator.clipboard.writeText("Hello")
  3. Confirm the text "Hello" has not been copied to the actual clipboard

Expected behavior

When passing writeToClipboard: true I expect the clipboard stub to copy text to the actual clipboard as normal. Currently it doesn't actually make a difference whether writeToClipboard is set or not. The property name seems misleading.

This is a problem in Storybook where we instrument user's stories/components with userEvent.setup(), causing their clipboard behavior to no longer work.

We can work around it by restoring the original clipboard after calling setup() but that's potentially brittle and does not allow users to leverage the actual stub in their tests. I have attempted to use detachClipboardStub but that method does not appear to be publicly accessible.

Ideally we would have a clipboard stub that actually calls the real clipboard as well, if possible/available.

Actual behavior

Calling navigator.clipboard.writeText("hello") is intercepted by user-event's clipboard stub and doesn't forward the call to the actual clipboard API, regardless of the writeToClipboard option.

User-event version

14.6.1

Environment

Testing Library framework:

JS framework:

Test environment:

DOM implementation:

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds assessmentThis needs to be looked at by a team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions