import { describe, it, expect, beforeEach, vi } from 'vitest' import { initCopyUrl } from '../../assets/modules/copy-url.js' describe('initCopyUrl', () => { beforeEach(() => { document.body.innerHTML = '' }) it('does nothing without button', () => { expect(() => initCopyUrl()).not.toThrow() }) it('does nothing without url element', () => { document.body.innerHTML = '' expect(() => initCopyUrl()).not.toThrow() }) it('copies url on click', async () => { document.body.innerHTML = `
https://example.com/event/1-test
` const writeText = vi.fn().mockResolvedValue(undefined) globalThis.navigator = { clipboard: { writeText } } initCopyUrl() document.getElementById('copy-url-btn').click() await new Promise(r => { setTimeout(r, 10) }) expect(writeText).toHaveBeenCalledWith('https://example.com/event/1-test') expect(document.getElementById('copy-url-btn').textContent).toBe('Copie !') }) it('restores button text after 2 seconds', async () => { vi.useFakeTimers() document.body.innerHTML = `https://example.com
` globalThis.navigator = { clipboard: { writeText: vi.fn().mockResolvedValue(undefined) } } initCopyUrl() document.getElementById('copy-url-btn').click() await vi.advanceTimersByTimeAsync(100) expect(document.getElementById('copy-url-btn').textContent).toBe('Copie !') vi.advanceTimersByTime(2000) expect(document.getElementById('copy-url-btn').textContent).toBe('Copier le lien') vi.useRealTimers() }) })