Chore: Attempt to fix flaky clipboard button test (#105105)

This commit is contained in:
Tom Ratcliffe
2025-05-08 15:35:01 +01:00
committed by GitHub
parent 620260fabc
commit eb57fb427d

View File

@ -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();