- admin-i18n module: AdminText { en, pl }, component texts under the ipal
namespace (project translations win), en + pl admin languages when the
project sets none (project's supportedLanguages wins)
- every plugin label, description, option, tab and group as { en, pl };
explicit labels instead of auto-labels; sentence case; glossary terms
- visible renames (slugs, names, data unchanged): Cookie banner, Form
messages, Privacy group, Search engines, Maintenance, Spam protection,
E-mail sending, stats nav link Overview (hidden without access)
- Polish visitor defaults by page locale: cookie banner, form messages,
validators, lightbox, accessibility widget; {field} uses the label
- fixes: e-mail status banner used Turnstile wording; developer words and
env var names out of editor messages; stats numbers/dates in the admin
language; test e-mail no longer says ipal-kit
- test/adminUx.test.ts: length limits, en + pl present, no developer words
Co-Authored-By: Claude Opus 5.5 <[email protected]>
291 lines
12 KiB
TypeScript
291 lines
12 KiB
TypeScript
// @vitest-environment happy-dom
|
|
import { act, createElement, type ReactElement } from 'react'
|
|
import { createRoot, type Root } from 'react-dom/client'
|
|
import { renderToStaticMarkup } from 'react-dom/server'
|
|
import { afterEach, beforeEach, describe, expect, test } from 'vitest'
|
|
|
|
import { buildNotifications } from '../src/globals/Notifications/index.js'
|
|
import { LightboxDialog } from '../src/modules/lightbox/LightboxDialog.js'
|
|
import { lightboxImage } from '../src/modules/lightbox/lightboxImage.js'
|
|
import { LightboxProvider, LightboxTrigger, useLightbox } from '../src/modules/lightbox/LightboxProvider.js'
|
|
import { formatLightboxCounter, LIGHTBOX_FALLBACK, lightboxIndex, resolveLightboxTexts } from '../src/modules/lightbox/texts.js'
|
|
import { RenderRichText } from '../src/modules/richtext/RenderRichText.js'
|
|
|
|
// Loosely typed createElement: children passed positionally, as JSX would.
|
|
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 photo = (n: number) => ({ alt: `Photo ${n}`, src: `/p${n}.jpg` })
|
|
|
|
let container: HTMLDivElement
|
|
let root: Root
|
|
|
|
beforeEach(() => {
|
|
container = document.createElement('div')
|
|
document.body.appendChild(container)
|
|
root = createRoot(container)
|
|
})
|
|
|
|
afterEach(() => {
|
|
act(() => root.unmount())
|
|
container.remove()
|
|
document.documentElement.removeAttribute('data-a11y-motion')
|
|
})
|
|
|
|
const render = (node: React.ReactNode) => act(() => root.render(node))
|
|
const dialog = () => document.querySelector<HTMLElement>('[role="dialog"]')
|
|
const shown = () => dialog()?.querySelector('img')?.getAttribute('src')
|
|
const counter = () => document.querySelector('[data-ipal="lightbox-counter"]')?.textContent
|
|
const triggers = () => [...container.querySelectorAll<HTMLButtonElement>('[data-ipal="lightbox-trigger"]')]
|
|
const key = (k: string, init: KeyboardEventInit = {}) =>
|
|
act(() => {
|
|
document.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, key: k, ...init }))
|
|
})
|
|
const click = (el: Element | null | undefined) =>
|
|
act(() => {
|
|
;(el as HTMLElement).click()
|
|
})
|
|
|
|
const gallery = (props: Record<string, unknown> = {}) =>
|
|
h(
|
|
LightboxProvider,
|
|
{ texts: { close: 'Zamknij', open: 'Powiększ' }, ...props },
|
|
h(LightboxTrigger, { group: 'g', image: photo(1) }, h('img', { alt: '', src: '/t1.jpg' })),
|
|
h(LightboxTrigger, { group: 'g', image: photo(2) }, h('img', { alt: '', src: '/t2.jpg' })),
|
|
h(LightboxTrigger, { group: 'g', image: { ...photo(3), caption: 'Third' } }, h('img', { alt: '', src: '/t3.jpg' })),
|
|
h(LightboxTrigger, { image: photo(9) }, h('img', { alt: '', src: '/t9.jpg' })),
|
|
)
|
|
|
|
describe('lightbox helpers', () => {
|
|
test('lightboxImage: srcSet from Payload sizes + original, size as src, caption, alt', () => {
|
|
const doc = {
|
|
alt: 'Sofa',
|
|
caption: 'Living room',
|
|
height: 2000,
|
|
mimeType: 'image/jpeg',
|
|
sizes: {
|
|
card: { height: 512, url: '/card.jpg', width: 768 },
|
|
empty: { height: null, url: null, width: null },
|
|
hero: { height: 1080, url: '/hero.jpg', width: 1920 },
|
|
},
|
|
url: '/full.jpg',
|
|
width: 3000,
|
|
}
|
|
expect(lightboxImage(doc, { size: 'hero' })).toEqual({
|
|
alt: 'Sofa',
|
|
caption: 'Living room',
|
|
height: 1080,
|
|
sizes: '100vw',
|
|
src: '/hero.jpg',
|
|
srcSet: '/card.jpg 768w, /hero.jpg 1920w, /full.jpg 3000w',
|
|
width: 1920,
|
|
})
|
|
expect(lightboxImage(doc, { alt: 'Other', caption: false, srcSet: false })).toEqual({ alt: 'Other', height: 2000, src: '/full.jpg', width: 3000 })
|
|
expect(lightboxImage({ mimeType: 'video/mp4', url: '/v.mp4' })).toBeNull()
|
|
expect(lightboxImage({ mimeType: 'application/pdf', url: '/a.pdf' })).toBeNull()
|
|
expect(lightboxImage(42)).toBeNull()
|
|
})
|
|
|
|
test('index wraps or clamps, counter and texts fall back per field', () => {
|
|
expect(lightboxIndex(2, 1, 3)).toBe(0)
|
|
expect(lightboxIndex(0, -1, 3)).toBe(2)
|
|
expect(lightboxIndex(2, 1, 3, false)).toBe(2)
|
|
expect(lightboxIndex(0, -1, 3, false)).toBe(0)
|
|
expect(formatLightboxCounter('{current} z {total}', 2, 5)).toBe('2 z 5')
|
|
expect(resolveLightboxTexts({ close: 'Zamknij', next: ' ', previous: null })).toEqual({ ...LIGHTBOX_FALLBACK, close: 'Zamknij' })
|
|
// a Polish page falls back to Polish, any other language to English
|
|
expect(resolveLightboxTexts({ close: 'X' }, 'pl')).toMatchObject({ close: 'X', next: 'Następne zdjęcie' })
|
|
expect(resolveLightboxTexts(null, 'de').next).toBe(LIGHTBOX_FALLBACK.next)
|
|
})
|
|
|
|
test('Notifications gets the Lightbox group only when asked', () => {
|
|
const names = (g: ReturnType<typeof buildNotifications>) => g.fields.map((f) => ('name' in f ? f.name : null))
|
|
expect(names(buildNotifications())).not.toContain('lightbox')
|
|
expect(names(buildNotifications({ lightbox: true }))).toContain('lightbox')
|
|
})
|
|
})
|
|
|
|
describe('lightbox behaviour', () => {
|
|
test('nothing in the DOM until opened; triggers are labelled buttons', () => {
|
|
render(gallery())
|
|
expect(dialog()).toBeNull()
|
|
const [first] = triggers()
|
|
expect(first.tagName).toBe('BUTTON')
|
|
expect(first.getAttribute('aria-haspopup')).toBe('dialog')
|
|
expect(first.getAttribute('aria-label')).toBe('Powiększ: Photo 1')
|
|
})
|
|
|
|
test('open: accessible dialog at the clicked image, focus on Close, page scroll locked', () => {
|
|
render(gallery())
|
|
click(triggers()[1])
|
|
const d = dialog()!
|
|
expect(d.getAttribute('aria-modal')).toBe('true')
|
|
expect(d.getAttribute('aria-label')).toBe(LIGHTBOX_FALLBACK.dialog)
|
|
expect(shown()).toBe('/p2.jpg')
|
|
expect(d.querySelector('img')?.getAttribute('alt')).toBe('Photo 2')
|
|
expect(counter()).toBe('2 / 3')
|
|
expect(document.activeElement?.getAttribute('aria-label')).toBe('Zamknij')
|
|
expect(document.documentElement.style.overflow).toBe('hidden')
|
|
})
|
|
|
|
test('keyboard: arrows browse the group with wrap-around, Home/End jump', () => {
|
|
render(gallery())
|
|
click(triggers()[1])
|
|
key('ArrowRight')
|
|
expect(shown()).toBe('/p3.jpg')
|
|
expect(dialog()!.querySelector('figcaption')?.textContent).toBe('Third')
|
|
key('ArrowRight')
|
|
expect(shown()).toBe('/p1.jpg')
|
|
key('ArrowLeft')
|
|
expect(shown()).toBe('/p3.jpg')
|
|
key('Home')
|
|
expect(counter()).toBe('1 / 3')
|
|
key('End')
|
|
expect(counter()).toBe('3 / 3')
|
|
})
|
|
|
|
test('buttons and swipe browse; loop={false} stops at the ends', () => {
|
|
render(gallery({ loop: false }))
|
|
click(triggers()[0])
|
|
const prev = document.querySelector<HTMLButtonElement>('[data-ipal="lightbox-previous"]')!
|
|
expect(prev.disabled).toBe(true)
|
|
click(document.querySelector('[data-ipal="lightbox-next"]'))
|
|
expect(shown()).toBe('/p2.jpg')
|
|
const figure = document.querySelector('[data-ipal="lightbox-figure"]')!
|
|
const swipe = (from: number, to: number) =>
|
|
act(() => {
|
|
figure.dispatchEvent(new MouseEvent('pointerdown', { bubbles: true, clientX: from, clientY: 100 }))
|
|
figure.dispatchEvent(new MouseEvent('pointerup', { bubbles: true, clientX: to, clientY: 100 }))
|
|
})
|
|
swipe(300, 100) // swipe left → next
|
|
expect(shown()).toBe('/p3.jpg')
|
|
swipe(300, 100) // already last, no loop
|
|
expect(shown()).toBe('/p3.jpg')
|
|
swipe(100, 300) // swipe right → previous
|
|
expect(shown()).toBe('/p2.jpg')
|
|
swipe(100, 110) // too short
|
|
expect(shown()).toBe('/p2.jpg')
|
|
})
|
|
|
|
test('Esc closes, focus returns to the trigger, scroll restored', () => {
|
|
render(gallery())
|
|
const trigger = triggers()[2]
|
|
click(trigger)
|
|
key('Escape')
|
|
expect(dialog()).toBeNull()
|
|
expect(document.activeElement).toBe(trigger)
|
|
expect(document.documentElement.style.overflow).toBe('')
|
|
})
|
|
|
|
test('Close button and backdrop close it too', () => {
|
|
render(gallery())
|
|
click(triggers()[0])
|
|
click(document.querySelector('[data-ipal="lightbox-close"]'))
|
|
expect(dialog()).toBeNull()
|
|
click(triggers()[0])
|
|
click(document.querySelector('[data-ipal="lightbox-backdrop"]'))
|
|
expect(dialog()).toBeNull()
|
|
})
|
|
|
|
test('focus trap: Tab and Shift+Tab cycle inside the dialog', () => {
|
|
render(gallery())
|
|
click(triggers()[0])
|
|
const close = document.querySelector<HTMLElement>('[data-ipal="lightbox-close"]')!
|
|
const prev = document.querySelector<HTMLElement>('[data-ipal="lightbox-previous"]')!
|
|
expect(document.activeElement).toBe(close)
|
|
key('Tab')
|
|
expect(document.activeElement).toBe(prev)
|
|
key('Tab', { shiftKey: true })
|
|
expect(document.activeElement).toBe(close)
|
|
})
|
|
|
|
test('a trigger without a group opens alone — no counter, no arrows', () => {
|
|
render(gallery())
|
|
click(triggers()[3])
|
|
expect(shown()).toBe('/p9.jpg')
|
|
expect(counter()).toBeUndefined()
|
|
expect(document.querySelector('[data-ipal="lightbox-next"]')).toBeNull()
|
|
})
|
|
|
|
test('a standalone trigger brings its own viewer', () => {
|
|
render(h(LightboxTrigger, { image: photo(5) }, h('img', { alt: '', src: '/t5.jpg' })))
|
|
click(triggers()[0])
|
|
expect(shown()).toBe('/p5.jpg')
|
|
expect(document.activeElement?.getAttribute('aria-label')).toBe(LIGHTBOX_FALLBACK.close)
|
|
})
|
|
|
|
test('useLightbox opens any list; renderImage and renderDialog replace the parts', () => {
|
|
function Custom() {
|
|
const box = useLightbox()
|
|
return h('button', { id: 'all', onClick: () => box.open([photo(1), photo(2)], 1) }, 'all')
|
|
}
|
|
render(
|
|
h(
|
|
LightboxProvider,
|
|
{ renderImage: ({ imgProps }: { imgProps: object }) => h('img', { ...imgProps, 'data-custom': 'yes' }) },
|
|
h(Custom),
|
|
),
|
|
)
|
|
click(container.querySelector('#all'))
|
|
expect(dialog()!.querySelector('img')?.getAttribute('data-custom')).toBe('yes')
|
|
expect(counter()).toBe('2 / 2')
|
|
key('Escape')
|
|
|
|
render(h(LightboxProvider, { renderDialog: (p: { images: { alt: string }[]; index: number }) => h('div', { id: 'mine' }, p.images[p.index].alt) }, h(Custom)))
|
|
click(container.querySelector('#all'))
|
|
expect(container.querySelector('#mine')?.textContent).toBe('Photo 2')
|
|
})
|
|
|
|
test('reduced motion: no transition; otherwise a fade', () => {
|
|
const noop = () => {}
|
|
render(h(LightboxDialog, { images: [photo(1)], index: 0, onClose: noop, onIndexChange: noop }))
|
|
expect(dialog()!.style.transition).toContain('opacity')
|
|
act(() => root.render(null))
|
|
document.documentElement.setAttribute('data-a11y-motion', 'reduce')
|
|
render(h(LightboxDialog, { images: [photo(1)], index: 0, onClose: noop, onIndexChange: noop }))
|
|
expect(dialog()!.style.transition).toBe('')
|
|
})
|
|
|
|
test('unstyled: no inline styles, classNames applied', () => {
|
|
const noop = () => {}
|
|
render(h(LightboxDialog, { classNames: { close: 'x', dialog: 'box' }, images: [photo(1)], index: 0, onClose: noop, onIndexChange: noop, unstyled: true }))
|
|
expect(dialog()!.getAttribute('style')).toBeNull()
|
|
expect(dialog()!.className).toBe('box')
|
|
expect(document.querySelector('[data-ipal="lightbox-close"]')!.className).toBe('x')
|
|
})
|
|
})
|
|
|
|
describe('RenderRichText lightbox', () => {
|
|
const upload = (value: object) => ({ type: 'upload', fields: null, format: '', relationTo: 'media', value, version: 3 })
|
|
const data = {
|
|
root: {
|
|
type: 'root',
|
|
children: [
|
|
upload({ alt: 'Chart', height: 600, mimeType: 'image/png', url: '/chart.png', width: 800 }),
|
|
upload({ filename: 'a.pdf', mimeType: 'application/pdf', url: '/a.pdf' }),
|
|
],
|
|
direction: 'ltr',
|
|
format: '',
|
|
indent: 0,
|
|
version: 1,
|
|
},
|
|
} as never
|
|
|
|
test('off by default; `lightbox` wraps images only, project upload converter kept', () => {
|
|
expect(renderToStaticMarkup(h(RenderRichText, { data }))).not.toContain('lightbox-trigger')
|
|
const html = renderToStaticMarkup(
|
|
h(RenderRichText, {
|
|
converters: ({ defaultConverters }: { defaultConverters: object }) => ({
|
|
...defaultConverters,
|
|
upload: ({ node }: { node: { value: unknown } }) => h('img', { className: 'mine', src: (node.value as { url: string }).url }),
|
|
}),
|
|
data,
|
|
lightbox: { group: 'post' },
|
|
}),
|
|
)
|
|
expect(html).toMatch(/<button[^>]*aria-label="Enlarge image: Chart"[^>]*data-group="post"[^>]*data-ipal="lightbox-trigger"[^>]*><img class="mine" src="\/chart.png"\/><\/button>/)
|
|
expect(html.match(/lightbox-trigger/g)).toHaveLength(1)
|
|
})
|
|
})
|