Skip to content

Commit f24a13d

Browse files
ghengeveldstorybook-bot
authored andcommitted
Merge pull request #31730 from storybookjs/restore-original-clipboard
Core: Restore original clipboard after invoking `userEvent.setup()` (cherry picked from commit 4d2a896)
1 parent b662427 commit f24a13d

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

code/core/src/test/preview.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,19 @@ const enhanceContext: LoaderFunction = async (context) => {
9292

9393
// userEvent.setup() cannot be called in non browser environment and will attempt to access window.navigator.clipboard
9494
// which will throw an error in react native for example.
95-
if (globalThis.window?.navigator?.clipboard) {
95+
const clipboard = globalThis.window?.navigator?.clipboard;
96+
if (clipboard) {
9697
context.userEvent = instrument(
9798
{ userEvent: uninstrumentedUserEvent.setup() },
9899
{ intercept: true }
99100
).userEvent;
100101

102+
// Restore original clipboard, which was replaced with a stub by userEvent.setup()
103+
Object.defineProperty(globalThis.window.navigator, 'clipboard', {
104+
get: () => clipboard,
105+
configurable: true,
106+
});
107+
101108
let currentFocus = HTMLElement.prototype.focus;
102109

103110
if (!patchedFocus) {

0 commit comments

Comments
 (0)