forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyToClipboardInput.test.js
More file actions
46 lines (37 loc) · 1.25 KB
/
CopyToClipboardInput.test.js
File metadata and controls
46 lines (37 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, fireEvent, screen, cleanup, waitFor } from '@testing-library/vue';
import CopyToClipboardInput from './CopyToClipboardInput.vue';
// Mock the clipboard API
Object.assign(navigator, {
clipboard: {
writeText: vi.fn(() => Promise.resolve()),
},
});
describe('CopyToClipboardInput', () => {
beforeEach(() => {
render(CopyToClipboardInput, {
props: { value: 'Test text' },
});
});
afterEach(() => {
cleanup();
});
it('renders correctly', () => {
const input = screen.getByRole('textbox');
const button = screen.getByRole('button', { name: 'Copy' });
expect(input).toBeTruthy();
expect(button).toBeTruthy();
});
it('selects text when input is focused', async () => {
const input = screen.getByRole('textbox');
await fireEvent.focus(input);
expect(input.selectionStart).toBe(0);
expect(input.selectionEnd).toBe('Test text'.length);
});
it('copies text to clipboard when button is clicked', async () => {
const button = screen.getByRole('button', { name: 'Copy' });
await fireEvent.click(button);
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('Test text');
await waitFor(() => expect(button.textContent).toBe('Copied!'));
});
});