Extract initCopyUrl to module, add tests for copy-url and event-map coverage

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-03-20 21:37:12 +01:00
parent 0cd1e0f061
commit e154713878
4 changed files with 79 additions and 14 deletions

33
tests/js/copy-url.test.js Normal file
View File

@@ -0,0 +1,33 @@
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')
})
})

View File

@@ -0,0 +1,32 @@
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { initEventMap } from '../../assets/modules/event-map.js'
describe('initEventMap', () => {
beforeEach(() => {
document.body.innerHTML = ''
document.head.innerHTML = ''
})
it('does nothing without map element', () => {
expect(() => initEventMap()).not.toThrow()
})
it('does nothing without data-address', () => {
document.body.innerHTML = '<div id="event-map"></div>'
expect(() => initEventMap()).not.toThrow()
})
it('loads leaflet css and js when map element exists', () => {
document.body.innerHTML = '<div id="event-map" data-address="12 rue test, 75001 Paris"></div>'
initEventMap()
const link = document.querySelector('link[rel="stylesheet"]')
expect(link).not.toBeNull()
expect(link.href).toContain('leaflet')
const script = document.querySelector('script')
expect(script).not.toBeNull()
expect(script.src).toContain('leaflet')
})
})