mirror of
https://github.com/apache/superset.git
synced 2026-05-21 15:55:10 +00:00
fix: Support the Clipboard API in modern browsers (#20058)
* fix: Support the Clipboard API in modern browsers * fix tests * PR comment * Improvements
This commit is contained in:
@@ -18,7 +18,7 @@
|
||||
*/
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import React from 'react';
|
||||
import { render, screen } from 'spec/helpers/testing-library';
|
||||
import { render, screen, waitFor } from 'spec/helpers/testing-library';
|
||||
import { CopyToClipboardButton } from '.';
|
||||
|
||||
test('Render a button', () => {
|
||||
@@ -28,14 +28,26 @@ test('Render a button', () => {
|
||||
expect(screen.getByRole('button')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('Should copy to clipboard', () => {
|
||||
document.execCommand = jest.fn();
|
||||
test('Should copy to clipboard', async () => {
|
||||
const callback = jest.fn();
|
||||
document.execCommand = callback;
|
||||
|
||||
const originalClipboard = { ...global.navigator.clipboard };
|
||||
// @ts-ignore
|
||||
global.navigator.clipboard = { write: callback, writeText: callback };
|
||||
|
||||
render(<CopyToClipboardButton data={{ copy: 'data', data: 'copy' }} />, {
|
||||
useRedux: true,
|
||||
});
|
||||
|
||||
expect(document.execCommand).toHaveBeenCalledTimes(0);
|
||||
expect(callback).toHaveBeenCalledTimes(0);
|
||||
userEvent.click(screen.getByRole('button'));
|
||||
expect(document.execCommand).toHaveBeenCalledWith('copy');
|
||||
|
||||
await waitFor(() => {
|
||||
expect(callback).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
jest.resetAllMocks();
|
||||
// @ts-ignore
|
||||
global.navigator.clipboard = originalClipboard;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user