test: added tests for copy functions

This commit is contained in:
Arman
2022-05-09 15:03:06 +02:00
parent 0997a624fd
commit 7c39df85f7
+25 -3
View File
@@ -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');
});