diff --git a/assets/app.js b/assets/app.js index 7bffb7a..54606cb 100644 --- a/assets/app.js +++ b/assets/app.js @@ -4,20 +4,7 @@ import { initTabs } from "./modules/tabs.js" import { registerEditor } from "./modules/editor.js" import { initCookieConsent } from "./modules/cookie-consent.js" import { initEventMap } from "./modules/event-map.js" - -function initCopyUrl() { - const btn = document.getElementById('copy-url-btn') - if (!btn) return - const url = document.getElementById('event-url')?.textContent?.trim() - if (!url) return - - btn.addEventListener('click', () => { - globalThis.navigator.clipboard.writeText(url).then(() => { - btn.textContent = 'Copie !' - setTimeout(() => { btn.textContent = 'Copier le lien' }, 2000) - }) - }) -} +import { initCopyUrl } from "./modules/copy-url.js" document.addEventListener('DOMContentLoaded', () => { initMobileMenu() diff --git a/assets/modules/copy-url.js b/assets/modules/copy-url.js new file mode 100644 index 0000000..37fd45d --- /dev/null +++ b/assets/modules/copy-url.js @@ -0,0 +1,13 @@ +export function initCopyUrl() { + const btn = document.getElementById('copy-url-btn') + if (!btn) return + const url = document.getElementById('event-url')?.textContent?.trim() + if (!url) return + + btn.addEventListener('click', () => { + globalThis.navigator.clipboard.writeText(url).then(() => { + btn.textContent = 'Copie !' + setTimeout(() => { btn.textContent = 'Copier le lien' }, 2000) + }) + }) +} diff --git a/tests/js/copy-url.test.js b/tests/js/copy-url.test.js new file mode 100644 index 0000000..b75b60c --- /dev/null +++ b/tests/js/copy-url.test.js @@ -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 = '' + 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') + }) +}) diff --git a/tests/js/event-map.test.js b/tests/js/event-map.test.js new file mode 100644 index 0000000..d61a2ac --- /dev/null +++ b/tests/js/event-map.test.js @@ -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 = '
' + expect(() => initEventMap()).not.toThrow() + }) + + it('loads leaflet css and js when map element exists', () => { + document.body.innerHTML = '
' + + 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') + }) +})