Files
e-ticket/tests/js/copy-url.test.js
2026-03-20 21:37:12 +01:00

34 lines
1.1 KiB
JavaScript

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 = '<button id="copy-url-btn"></button>'
expect(() => initCopyUrl()).not.toThrow()
})
it('copies url on click', async () => {
document.body.innerHTML = `
<p id="event-url">https://example.com/event/1-test</p>
<button id="copy-url-btn">Copier le lien</button>
`
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')
})
})