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:
Diego Medina
2022-06-03 07:34:00 -04:00
committed by GitHub
parent 92057858c2
commit 0e38c686c6
11 changed files with 118 additions and 57 deletions

View File

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