2026-03-20 16:02:36 +01:00
|
|
|
import { describe, it, expect, beforeEach } from 'vitest'
|
|
|
|
|
import { initCookieConsent } from '../../assets/modules/cookie-consent.js'
|
|
|
|
|
|
|
|
|
|
describe('initCookieConsent', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
document.cookie = 'e_ticket_consent=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/'
|
|
|
|
|
document.body.innerHTML = `
|
|
|
|
|
<div id="cookie-banner" class="hidden">
|
|
|
|
|
<button id="cookie-accept"></button>
|
|
|
|
|
<button id="cookie-refuse"></button>
|
|
|
|
|
</div>
|
|
|
|
|
`
|
2026-03-26 16:21:55 +01:00
|
|
|
delete document.body.dataset.env
|
2026-03-20 16:02:36 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('shows banner when no consent cookie', () => {
|
|
|
|
|
initCookieConsent()
|
|
|
|
|
const banner = document.getElementById('cookie-banner')
|
|
|
|
|
expect(banner.classList.contains('hidden')).toBe(false)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('hides banner and sets cookie on accept', () => {
|
|
|
|
|
initCookieConsent()
|
|
|
|
|
document.getElementById('cookie-accept').click()
|
|
|
|
|
const banner = document.getElementById('cookie-banner')
|
|
|
|
|
expect(banner.classList.contains('hidden')).toBe(true)
|
|
|
|
|
expect(document.cookie).toContain('e_ticket_consent=accepted')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('hides banner and sets cookie on refuse', () => {
|
|
|
|
|
initCookieConsent()
|
|
|
|
|
document.getElementById('cookie-refuse').click()
|
|
|
|
|
const banner = document.getElementById('cookie-banner')
|
|
|
|
|
expect(banner.classList.contains('hidden')).toBe(true)
|
|
|
|
|
expect(document.cookie).toContain('e_ticket_consent=refused')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('does not show banner if already accepted', () => {
|
|
|
|
|
document.cookie = 'e_ticket_consent=accepted;path=/'
|
|
|
|
|
initCookieConsent()
|
|
|
|
|
const banner = document.getElementById('cookie-banner')
|
|
|
|
|
expect(banner.classList.contains('hidden')).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('does not show banner if already refused', () => {
|
|
|
|
|
document.cookie = 'e_ticket_consent=refused;path=/'
|
|
|
|
|
initCookieConsent()
|
|
|
|
|
const banner = document.getElementById('cookie-banner')
|
|
|
|
|
expect(banner.classList.contains('hidden')).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-20 19:13:31 +01:00
|
|
|
it('handles banner without buttons', () => {
|
|
|
|
|
document.body.innerHTML = '<div id="cookie-banner" class="hidden"></div>'
|
|
|
|
|
initCookieConsent()
|
|
|
|
|
const banner = document.getElementById('cookie-banner')
|
|
|
|
|
expect(banner.classList.contains('hidden')).toBe(false)
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-20 16:02:36 +01:00
|
|
|
it('does nothing without banner element', () => {
|
|
|
|
|
document.body.innerHTML = ''
|
|
|
|
|
expect(() => initCookieConsent()).not.toThrow()
|
|
|
|
|
})
|
|
|
|
|
})
|