|
| 1 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { CopyButton } from './copy-button'; |
| 6 | + |
| 7 | +describe('Mosaic CopyButton', () => { |
| 8 | + it('writes the value to the clipboard', async () => { |
| 9 | + const user = userEvent.setup(); |
| 10 | + render(<CopyButton value='acme-inc' />); |
| 11 | + |
| 12 | + await user.click(screen.getByRole('button', { name: 'Copy' })); |
| 13 | + |
| 14 | + await expect(navigator.clipboard.readText()).resolves.toBe('acme-inc'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('announces the copy and keeps the button named', async () => { |
| 18 | + const user = userEvent.setup(); |
| 19 | + render(<CopyButton value='acme-inc' />); |
| 20 | + |
| 21 | + await user.click(screen.getByRole('button', { name: 'Copy' })); |
| 22 | + |
| 23 | + await waitFor(() => expect(screen.getByRole('status')).toHaveTextContent('Copied')); |
| 24 | + expect(screen.getByRole('button', { name: 'Copy' })).toBeInTheDocument(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('confirms the copy in a tooltip', async () => { |
| 28 | + const user = userEvent.setup(); |
| 29 | + render(<CopyButton value='acme-inc' />); |
| 30 | + |
| 31 | + expect(screen.queryByRole('tooltip')).not.toBeInTheDocument(); |
| 32 | + |
| 33 | + await user.click(screen.getByRole('button', { name: 'Copy' })); |
| 34 | + |
| 35 | + const tooltip = await screen.findByRole('tooltip'); |
| 36 | + expect(tooltip).toHaveTextContent('Copied'); |
| 37 | + expect(tooltip.querySelector('.cl-icon')).toBeInTheDocument(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('takes the tooltip label from copiedLabel', async () => { |
| 41 | + const user = userEvent.setup(); |
| 42 | + render( |
| 43 | + <CopyButton |
| 44 | + value='acme-inc' |
| 45 | + copiedLabel='Slug copied' |
| 46 | + />, |
| 47 | + ); |
| 48 | + |
| 49 | + await user.click(screen.getByRole('button', { name: 'Copy' })); |
| 50 | + |
| 51 | + await waitFor(() => expect(screen.getByRole('tooltip')).toHaveTextContent('Slug copied')); |
| 52 | + }); |
| 53 | + |
| 54 | + it('settles back after the copied state expires', async () => { |
| 55 | + const user = userEvent.setup(); |
| 56 | + render( |
| 57 | + <CopyButton |
| 58 | + value='acme-inc' |
| 59 | + resetAfter={200} |
| 60 | + />, |
| 61 | + ); |
| 62 | + |
| 63 | + await user.click(screen.getByRole('button', { name: 'Copy' })); |
| 64 | + await waitFor(() => expect(screen.getByRole('status')).toHaveTextContent('Copied')); |
| 65 | + |
| 66 | + await waitFor(() => expect(screen.getByRole('status')).toBeEmptyDOMElement()); |
| 67 | + }); |
| 68 | + |
| 69 | + it('copies through onCopy when one is given', async () => { |
| 70 | + const onCopy = vi.fn(); |
| 71 | + const user = userEvent.setup(); |
| 72 | + const writeText = vi.spyOn(navigator.clipboard, 'writeText'); |
| 73 | + render( |
| 74 | + <CopyButton |
| 75 | + value='acme-inc' |
| 76 | + onCopy={onCopy} |
| 77 | + />, |
| 78 | + ); |
| 79 | + |
| 80 | + await user.click(screen.getByRole('button', { name: 'Copy' })); |
| 81 | + |
| 82 | + expect(onCopy).toHaveBeenCalledWith('acme-inc'); |
| 83 | + expect(writeText).not.toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('stays quiet when the copy fails', async () => { |
| 87 | + const user = userEvent.setup(); |
| 88 | + render( |
| 89 | + <CopyButton |
| 90 | + value='acme-inc' |
| 91 | + onCopy={() => Promise.reject(new Error('denied'))} |
| 92 | + />, |
| 93 | + ); |
| 94 | + |
| 95 | + await user.click(screen.getByRole('button', { name: 'Copy' })); |
| 96 | + |
| 97 | + expect(screen.getByRole('status')).toBeEmptyDOMElement(); |
| 98 | + }); |
| 99 | +}); |
0 commit comments