|
| 1 | +import { render, h, describe, it, expect, vi, beforeEach, afterEach } from '@stencil/vitest'; |
| 2 | + |
| 3 | +import '../ch-button/ch-button'; |
| 4 | +import './ch-copy-button.tsx'; |
| 5 | + |
| 6 | +const settle = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); |
| 7 | + |
| 8 | +function innerButton(root: HTMLElement) { |
| 9 | + return root.shadowRoot.querySelector('ch-button') as HTMLElement & { shadowRoot: ShadowRoot; variant?: string; size?: string; disabled?: boolean; label?: string }; |
| 10 | +} |
| 11 | + |
| 12 | +// mock-doc does not project a <slot> that is itself assigned to another |
| 13 | +// component's slot, so the inner ch-button renders empty here. The real |
| 14 | +// projection is covered in tests/browser/ch-copy-button.browser.spec.ts; |
| 15 | +// these specs dispatch chClick on the ch-button host directly. |
| 16 | +function click(root: HTMLElement) { |
| 17 | + innerButton(root).dispatchEvent(new CustomEvent('chClick')); |
| 18 | +} |
| 19 | + |
| 20 | +describe('ch-copy-button', () => { |
| 21 | + const writeText = vi.fn(); |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + writeText.mockReset().mockResolvedValue(undefined); |
| 25 | + Object.defineProperty(globalThis.navigator, 'clipboard', { value: { writeText }, configurable: true }); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + Object.defineProperty(globalThis.navigator, 'clipboard', { value: undefined, configurable: true }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('renders on top of ch-button', async () => { |
| 33 | + const { root } = await render(<ch-copy-button value="abc">Copiar</ch-copy-button>); |
| 34 | + |
| 35 | + expect(innerButton(root)).not.toBeNull(); |
| 36 | + expect(innerButton(root).variant).toBe('ghost'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('forwards variant, size, disabled and label to the inner button', async () => { |
| 40 | + const { root } = await render( |
| 41 | + <ch-copy-button value="abc" variant="secondary" size="sm" label="Copiar clave"> |
| 42 | + Copiar |
| 43 | + </ch-copy-button>, |
| 44 | + ); |
| 45 | + const button = innerButton(root); |
| 46 | + |
| 47 | + expect(button.variant).toBe('secondary'); |
| 48 | + expect(button.size).toBe('sm'); |
| 49 | + expect(button.label).toBe('Copiar clave'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('is disabled when there is no value to copy', async () => { |
| 53 | + const { root } = await render(<ch-copy-button value="">Copiar</ch-copy-button>); |
| 54 | + |
| 55 | + expect(innerButton(root).disabled).toBe(true); |
| 56 | + }); |
| 57 | + |
| 58 | + it('writes the value, emits chCopy and shows the copied label', async () => { |
| 59 | + const { root, waitForChanges } = await render( |
| 60 | + <ch-copy-button value="secreto" copiedLabel="¡Copiado!" copiedDuration={50}> |
| 61 | + Copiar |
| 62 | + </ch-copy-button>, |
| 63 | + ); |
| 64 | + const spy = vi.fn(); |
| 65 | + root.addEventListener('chCopy', spy as EventListener); |
| 66 | + |
| 67 | + click(root); |
| 68 | + await waitForChanges(); |
| 69 | + await settle(10); |
| 70 | + |
| 71 | + expect(writeText).toHaveBeenCalledWith('secreto'); |
| 72 | + expect(spy).toHaveBeenCalledTimes(1); |
| 73 | + expect(root.shadowRoot.textContent).toContain('¡Copiado!'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('reverts to the slot after copiedDuration', async () => { |
| 77 | + const { root, waitForChanges } = await render( |
| 78 | + <ch-copy-button value="secreto" copiedLabel="¡Copiado!" copiedDuration={30}> |
| 79 | + Copiar |
| 80 | + </ch-copy-button>, |
| 81 | + ); |
| 82 | + |
| 83 | + click(root); |
| 84 | + await waitForChanges(); |
| 85 | + await settle(80); |
| 86 | + await waitForChanges(); |
| 87 | + |
| 88 | + expect(root.shadowRoot.textContent).not.toContain('¡Copiado!'); |
| 89 | + expect(root.shadowRoot.querySelector('slot')).not.toBeNull(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('emits chCopyError when the clipboard is unavailable', async () => { |
| 93 | + Object.defineProperty(globalThis.navigator, 'clipboard', { value: undefined, configurable: true }); |
| 94 | + const { root, waitForChanges } = await render(<ch-copy-button value="x">Copiar</ch-copy-button>); |
| 95 | + const copied = vi.fn(); |
| 96 | + const failed = vi.fn(); |
| 97 | + root.addEventListener('chCopy', copied as EventListener); |
| 98 | + root.addEventListener('chCopyError', failed as EventListener); |
| 99 | + |
| 100 | + click(root); |
| 101 | + await waitForChanges(); |
| 102 | + await settle(10); |
| 103 | + |
| 104 | + expect(copied).not.toHaveBeenCalled(); |
| 105 | + expect(failed).toHaveBeenCalledTimes(1); |
| 106 | + }); |
| 107 | + |
| 108 | + it('emits chCopyError when the write rejects', async () => { |
| 109 | + writeText.mockRejectedValue(new Error('denied')); |
| 110 | + const { root, waitForChanges } = await render(<ch-copy-button value="x">Copiar</ch-copy-button>); |
| 111 | + const copied = vi.fn(); |
| 112 | + const failed = vi.fn(); |
| 113 | + root.addEventListener('chCopy', copied as EventListener); |
| 114 | + root.addEventListener('chCopyError', failed as EventListener); |
| 115 | + |
| 116 | + click(root); |
| 117 | + await waitForChanges(); |
| 118 | + await settle(10); |
| 119 | + |
| 120 | + expect(copied).not.toHaveBeenCalled(); |
| 121 | + expect(failed).toHaveBeenCalledTimes(1); |
| 122 | + }); |
| 123 | +}); |
0 commit comments