diff --git a/tests/unit/components/copy.test.ts b/tests/unit/components/copy.test.ts index 5805540bd..69f0c5034 100644 --- a/tests/unit/components/copy.test.ts +++ b/tests/unit/components/copy.test.ts @@ -1,9 +1,7 @@ import '@testing-library/jest-dom'; -import { render } from '@testing-library/svelte'; +import { render, fireEvent } from '@testing-library/svelte'; import { Copy } from '../../../src/lib/components'; -//TODO: test after button click value is copied to clipboard - const value = 'This is a test'; test('shows copy component', () => { @@ -16,3 +14,27 @@ test('shows copy component', () => { expect(input).toHaveAttribute('type', 'text'); expect(input).toBeDisabled(); }); + +test('copy to clipboard fallback function called on click', async () => { + const { getByRole } = render(Copy, { value }); + const button = getByRole('button'); + document.execCommand = jest.fn(); + + await fireEvent.click(button); + expect(document.execCommand).toHaveBeenCalledWith('copy'); +}); + +test('copy to clipboard function called on click', async () => { + const { getByRole } = render(Copy, { value }); + + Object.assign(window.navigator, { + clipboard: { + writeText: jest.fn().mockImplementation(() => Promise.resolve()) + } + }); + + const button = getByRole('button'); + await fireEvent.click(button); + + expect(window.navigator.clipboard.writeText).toHaveBeenCalledWith('This is a test'); +});