Skip to content

Commit

Permalink
Fix clipboard tests
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Smock <[email protected]>
  • Loading branch information
tsmock committed Jun 2, 2023
1 parent 2808165 commit b90a6dc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
14 changes: 3 additions & 11 deletions frontend/src/components/taskSelection/tests/imagery.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from '@testing-library/react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';

Expand Down Expand Up @@ -178,21 +178,13 @@ describe('Imagery', () => {
});

it('should copy value to the clipboard', async () => {
const originalClipboard = { ...global.navigator.clipboard };
const mockClipboard = {
writeText: jest.fn().mockImplementation(() => Promise.resolve()),
};
global.navigator.clipboard = mockClipboard;
const user = userEvent.setup();
const user = userEvent.setup({ writeToClipboard: true });
render(
<IntlProviders>
<Imagery value={'wms'} />
</IntlProviders>,
);
await user.click(screen.getByRole('img'));
expect(navigator.clipboard.writeText).toHaveBeenCalledTimes(1);
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('wms');
jest.resetAllMocks();
global.navigator.clipboard = originalClipboard;
await waitFor(async () => expect(await navigator.clipboard.readText()).toBe('wms'));
});
});
14 changes: 3 additions & 11 deletions frontend/src/components/user/tests/content.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import { render, screen } from '@testing-library/react';
import { render, screen, waitFor } from '@testing-library/react';
import { APIKeyCard } from '../content';
import userEvent from '@testing-library/user-event';
import { IntlProviders } from '../../../utils/testWithIntl';

test('should copy API key to the clipboard', async () => {
const originalClipboard = { ...global.navigator.clipboard };
const mockClipboard = {
writeText: jest.fn().mockImplementation(() => Promise.resolve()),
};
global.navigator.clipboard = mockClipboard;
const user = userEvent.setup();
const user = userEvent.setup({ writeToClipboard: true });
render(
<IntlProviders>
<APIKeyCard token="validToken" />
</IntlProviders>,
);
await user.click(screen.getByRole('img'));
expect(navigator.clipboard.writeText).toHaveBeenCalledTimes(1);
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('Token validToken');
jest.resetAllMocks();
global.navigator.clipboard = originalClipboard;
await waitFor(async () => expect(await navigator.clipboard.readText()).toBe('Token validToken'));
});

0 comments on commit b90a6dc

Please sign in to comment.