// @vitest-environment happy-dom /** * Where sits (1.17.2): the bottom corner of the window, always — it does not climb above the footer. * Only at the very end of the page it steps above a control it would keep covered. The placement before 1.17.2 * (above the footer) and "never move" stay available. */ import { act, createElement, type ReactElement } from 'react' import { createRoot, type Root } from 'react-dom/client' import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' import { ConsentProvider } from '../src/modules/consent/ConsentContext.js' import { resetConsentStore } from '../src/modules/consent/consentStore.js' import { COOKIE_BUTTON_AVOID, COOKIE_BUTTON_CLEAR, CookieButton, stepAbove } from '../src/modules/consent/CookieButton.js' import { CONSENT_FALLBACK } from '../src/modules/consent/getConsentTexts.js' import { CONSENT_COOKIE, serializeConsent } from '../src/modules/consent/storage.js' const h = createElement as unknown as (type: unknown, props?: null | object, ...children: unknown[]) => ReactElement ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true const box = (top: number, bottom: number, left = 16, right = 60) => ({ bottom, left, right, top }) /** The button's resting place in an 800 px high window: 44 px, 16 px from the bottom and the left. */ const REST = box(740, 784) describe('stepAbove', () => { test('nothing under the button: stays', () => { expect(stepAbove(REST, [], 400)).toBe(0) expect(stepAbove(REST, [box(700, 730), box(750, 780, 200, 300), box(790, 800)], 400)).toBe(0) }) test('a link under it: just above the link, with a gap', () => { expect(stepAbove(REST, [box(760, 780, 30, 120)], 400)).toBe(784 - 760 + 8) }) test('two rows of links: above both, onto text', () => { // social links at 760–780, legal links at 724–744: clearing the first lands on the second expect(stepAbove(REST, [box(760, 780, 30, 120), box(724, 744, 30, 200)], 400)).toBe(784 - 724 + 8) }) test('no free place within the limit: back to the corner', () => { const wall = Array.from({ length: 20 }, (_, i) => box(780 - (i + 1) * 30, 780 - i * 30)) expect(stepAbove(REST, wall, 400)).toBe(0) expect(stepAbove(REST, [box(300, 790)], 400)).toBe(0) }) test('the default list of controls', () => { for (const selector of ['a[href]', 'button', 'input', 'select', 'textarea', 'summary']) {expect(COOKIE_BUTTON_CLEAR).toContain(selector)} }) }) describe('placement', () => { let container: HTMLDivElement let root: Root let rects: Map> let page: { height: number; scrollY: number } const setRect = (el: Element, r: ReturnType) => rects.set(el, r) const button = () => document.querySelector('[data-ipal="cookie-button"]')! const lift = () => /translateY\(-(?[\d.]+)px\)/.exec(button().style.transform)?.groups?.px ?? '0' const frame = () => act(() => new Promise((resolve) => setTimeout(resolve, 40))) const scrollTo = async (y: number) => { page.scrollY = y window.dispatchEvent(new Event('scroll')) await frame() } const add = (html: string) => { const el = document.createElement('div') el.innerHTML = html document.body.appendChild(el) return el.firstElementChild! } const mount = async (props: object = {}) => { act(() => root.render(h(ConsentProvider, { texts: CONSENT_FALLBACK }, h(CookieButton, props)))) setRect(button(), REST) window.dispatchEvent(new Event('resize')) await frame() } beforeEach(() => { resetConsentStore() document.cookie = `${CONSENT_COOKIE}=${serializeConsent({ analytics: false, functional: false, marketing: false, necessary: true })};path=/` rects = new Map() page = { height: 3000, scrollY: 0 } vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(function (this: Element) { const r = rects.get(this) ?? { bottom: 0, left: 0, right: 0, top: 0 } // like the browser: the measured box includes the transform the button carries const moved = this === document.querySelector('[data-ipal="cookie-button"]') ? Number(/translateY\(-([\d.]+)px\)/.exec((this as HTMLElement).style.transform)?.[1] ?? 0) : 0 return { ...r, bottom: r.bottom - moved, height: r.bottom - r.top, top: r.top - moved, width: r.right - r.left, x: r.left, y: r.top - moved } as DOMRect }) vi.spyOn(window, 'innerHeight', 'get').mockReturnValue(800) vi.spyOn(window, 'scrollY', 'get').mockImplementation(() => page.scrollY) vi.spyOn(document.documentElement, 'scrollHeight', 'get').mockImplementation(() => page.height) container = document.createElement('div') document.body.appendChild(container) root = createRoot(container) }) afterEach(() => { act(() => root.unmount()) document.body.innerHTML = '' vi.restoreAllMocks() }) test('default: fixed in the bottom corner with the safe-area insets, below dialogs', async () => { await mount() const style = button().style expect(style.position).toBe('fixed') expect(style.bottom).toBe('calc(var(--ipal-cookie-button-offset, 1rem) + env(safe-area-inset-bottom, 0px))') expect(style.left).toBe('calc(var(--ipal-cookie-button-inset, 1rem) + env(safe-area-inset-left, 0px))') expect(style.zIndex).toBe('var(--ipal-z-header, 40)') expect(button().getAttribute('aria-label')).toBe(CONSENT_FALLBACK.settingsTitle) expect(button().type).toBe('button') }) test('side="right": the right corner and its safe-area inset', async () => { await mount({ side: 'right' }) expect(button().style.right).toBe('calc(var(--ipal-cookie-button-inset, 1rem) + env(safe-area-inset-right, 0px))') expect(button().style.left).toBe('') }) test('default: the footer on screen does not move it (before 1.17.2 it rode up by 300 px here)', async () => { setRect(add('
company
'), box(500, 1100, 0, 1280)) await mount() await scrollTo(1500) expect(lift()).toBe('0') }) test('avoid={COOKIE_BUTTON_AVOID}: the placement before 1.17.2, above the footer', async () => { setRect(add('
company
'), box(500, 1100, 0, 1280)) await mount({ avoid: COOKIE_BUTTON_AVOID }) expect(lift()).toBe('300') }) test('end of the page, a footer link in the corner: steps just above it; scrolling back up: returns to the corner', async () => { const link = add('Instagram') setRect(link, box(760, 780, 30, 120)) await mount() await scrollTo(1000) // mid-page: a covered link is one scroll away, the button stays expect(lift()).toBe('0') await scrollTo(2200) // 2200 + 800 = the end expect(lift()).toBe('32') await scrollTo(2200) // measured again while lifted: the same step, no creeping expect(lift()).toBe('32') await scrollTo(1000) expect(lift()).toBe('0') }) test('a page shorter than the window is always at its end', async () => { page.height = 800 setRect(add(''), box(750, 790, 20, 140)) await mount() expect(lift()).toBe('42') }) test('end of the page, nothing under it (text only): stays in the corner', async () => { setRect(add('

© Example

'), box(760, 780, 30, 400)) setRect(add('Terms'), box(760, 780, 900, 960)) await mount() await scrollTo(2200) expect(lift()).toBe('0') }) test('clear={false} and avoid={false}: never moves', async () => { setRect(add('Instagram'), box(760, 780, 30, 120)) setRect(add('
company
'), box(500, 1100, 0, 1280)) await mount({ clear: false }) await scrollTo(2200) expect(lift()).toBe('0') act(() => root.render(h(ConsentProvider, { texts: CONSENT_FALLBACK }, h(CookieButton, { avoid: false })))) await scrollTo(2200) expect(lift()).toBe('0') }) test('a selector of your own in clear', async () => { setRect(add('Instagram'), box(760, 780, 30, 120)) setRect(add('
chat
'), box(700, 790, 10, 70)) await mount({ clear: '.chat' }) await scrollTo(2200) expect(lift()).toBe(String(784 - 700 + 8)) }) test('className (the project positions it, here not fixed): no step', async () => { setRect(add('Instagram'), box(760, 780, 30, 120)) await mount({ className: 'in-the-footer' }) expect(button().style.position).toBe('') await scrollTo(2200) expect(lift()).toBe('0') }) })