From eb57fb427d06b7648b27ff37afbe6a52208b7b65 Mon Sep 17 00:00:00 2001 From: Tom Ratcliffe Date: Thu, 8 May 2025 15:35:01 +0100 Subject: [PATCH] Chore: Attempt to fix flaky clipboard button test (#105105) --- .../ClipboardButton/ClipboardButton.test.tsx | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/grafana-ui/src/components/ClipboardButton/ClipboardButton.test.tsx b/packages/grafana-ui/src/components/ClipboardButton/ClipboardButton.test.tsx index df8945bb224..0cf2a5ee010 100644 --- a/packages/grafana-ui/src/components/ClipboardButton/ClipboardButton.test.tsx +++ b/packages/grafana-ui/src/components/ClipboardButton/ClipboardButton.test.tsx @@ -1,11 +1,15 @@ -import { render, screen } from '@testing-library/react'; +import { act, render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { ClipboardButton } from './ClipboardButton'; const setup = (jsx: JSX.Element) => { return { - user: userEvent.setup(), + user: userEvent.setup({ + // Ensure that user events correctly advance timers: + // https://github.com/testing-library/react-testing-library/issues/1197 + advanceTimers: jest.advanceTimersByTime, + }), ...render(jsx), }; }; @@ -14,6 +18,7 @@ describe('ClipboardButton', () => { const originalWindow = { ...window }; beforeAll(() => { + jest.useFakeTimers(); Object.assign(window, { isSecureContext: true, }); @@ -21,10 +26,10 @@ describe('ClipboardButton', () => { afterAll(() => { Object.assign(window, originalWindow); + jest.useRealTimers(); }); - // TODO: flaky https://github.com/grafana/grafana/issues/105102 - it.skip('should copy text to clipboard when clicked', async () => { + it('should copy text to clipboard when clicked', async () => { const textToCopy = 'Copy me!'; const onClipboardCopy = jest.fn(); @@ -36,6 +41,13 @@ describe('ClipboardButton', () => { const button = screen.getByRole('button'); await user.click(button); + expect(await screen.findByText('Copied')).toBeInTheDocument(); + + act(() => { + jest.runAllTimers(); + }); + + expect(screen.queryByText('Copied')).not.toBeInTheDocument(); const clipboardText = await navigator.clipboard.readText();