mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 22:49:25 +08:00
Chore: Attempt to fix flaky clipboard button test (#105105)
This commit is contained in:
@ -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();
|
||||
|
||||
|
Reference in New Issue
Block a user