Theme switcher, table of contents, missing-page redirect, form placeholders and time field; audit and revalidate fixes
- buildRevalidateHook skips outside Next (no NEXT_RUNTIME) and with context.disableRevalidate - BuilderForm focuses only on a real step change (no scroll to the form on remount) - mediaImageProps: placeholder colour only for JPEG (placeholderColor); hook stores no colour for transparent images - buildLinkField: interface name per shape (CmsLink, CmsLinkPlain, CmsLinkNoLabel) + interfaceName option - Cookie Settings: buttons.save -> buttons.savePreferences (Mongoose reserved name) - ThemeScript, ThemeSwitcher, useTheme; Site Settings defaultTheme 'system' + themeToggleLabel; Turnstile follows page theme - extractHeadings, RenderRichText headingIds, TableOfContents, buildTableOfContentsField - createPageRoute onMissing: 'redirectHome' - Form builder: localized placeholder on text/email/textarea/number, new time field - ipal-audit: no false positive for object keys between JSX elements; /md route not reported as dynamic Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
23e033d7d7
commit
faa4ad679c
@@ -74,6 +74,7 @@ describe('form validation — choice lists and dates', () => {
|
||||
{ name: 'services', blockType: 'checkboxGroup', maxSelected: 2, minSelected: 1, options: [{ value: 'www' }, { value: 'seo' }, { value: 'ads' }] },
|
||||
{ name: 'size', blockType: 'radio', options: [{ value: 's' }, { value: 'l' }] },
|
||||
{ name: 'deadline', blockType: 'date' },
|
||||
{ name: 'hour', blockType: 'time' },
|
||||
{ blockType: 'step' },
|
||||
],
|
||||
},
|
||||
@@ -96,6 +97,12 @@ describe('form validation — choice lists and dates', () => {
|
||||
expect(await v({ deadline: '2026-13-45' })).toMatchObject({ field: 'deadline', kind: 'invalid_format' })
|
||||
})
|
||||
|
||||
test('time: HH:MM within a day', async () => {
|
||||
expect(await v({ hour: ' 09:30 ' })).toMatchObject({ cleaned: { hour: '09:30' }, ok: true })
|
||||
expect(await v({ hour: '24:00' })).toMatchObject({ field: 'hour', kind: 'invalid_format' })
|
||||
expect(await v({ hour: 'rano' })).toMatchObject({ field: 'hour', kind: 'invalid_format' })
|
||||
})
|
||||
|
||||
test('a Step block carries no value and is not an unknown field', async () => {
|
||||
expect(await v({})).toMatchObject({ ok: true })
|
||||
})
|
||||
|
||||
@@ -9,6 +9,16 @@ const config = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }, { c
|
||||
const opts = { config, homeSlug: 'start', locale: 'pl' }
|
||||
const page = (slug: string) => ({ type: 'internal' as const, label: slug, page: { slug } })
|
||||
|
||||
describe('buildLinkField interface names', () => {
|
||||
test('each shape has its own type name, so generate:types never depends on field order', () => {
|
||||
expect(buildLinkField().interfaceName).toBe('CmsLink')
|
||||
expect(buildLinkField({ appearances: false }).interfaceName).toBe('CmsLinkPlain')
|
||||
expect(buildLinkField({ disableLabel: true }).interfaceName).toBe('CmsLinkNoLabel')
|
||||
expect(buildLinkField({ appearances: false, disableLabel: true }).interfaceName).toBe('CmsLinkPlainNoLabel')
|
||||
expect(buildLinkField({ appearances: false, interfaceName: 'CardLink' }).interfaceName).toBe('CardLink')
|
||||
})
|
||||
})
|
||||
|
||||
describe('resolveLink', () => {
|
||||
test('internal page → next/link, no target', () => {
|
||||
expect(resolveLink(page('kontakt'), opts)).toEqual({ href: '/pl/kontakt', internal: true, label: 'kontakt', newTab: false, targetProps: {} })
|
||||
|
||||
+19
-1
@@ -24,11 +24,19 @@ describe('mediaImageProps', () => {
|
||||
})
|
||||
|
||||
test('placeholder and focal point', () => {
|
||||
const p = mediaImageProps({ ...image, focalX: 30, focalY: 70, placeholder: { blurDataURL: 'data:x', color: '#123456' } })
|
||||
const p = mediaImageProps({ ...image, focalX: 30, focalY: 70, mimeType: 'image/jpeg', placeholder: { blurDataURL: 'data:x', color: '#123456' } })
|
||||
expect(p).toMatchObject({ blurDataURL: 'data:x', placeholder: 'blur', style: { backgroundColor: '#123456', objectPosition: '30% 70%' } })
|
||||
expect(mediaImageProps({ ...image, focalX: 50, focalY: 50 })?.style).toBeUndefined()
|
||||
})
|
||||
|
||||
test('placeholder colour only where no alpha can show it (JPEG), unless forced', () => {
|
||||
const png = { ...image, mimeType: 'image/png', placeholder: { blurDataURL: 'data:x', color: '#000000' } }
|
||||
expect(mediaImageProps(png)?.style).toBeUndefined()
|
||||
expect(mediaImageProps(png)?.placeholder).toBe('blur')
|
||||
expect(mediaImageProps(png, { placeholderColor: true })?.style).toEqual({ backgroundColor: '#000000' })
|
||||
expect(mediaImageProps({ ...png, mimeType: 'image/jpeg' }, { placeholderColor: false })?.style).toBeUndefined()
|
||||
})
|
||||
|
||||
test('video, PDF and bare ids give null; decorative alt override', () => {
|
||||
expect(mediaImageProps({ ...image, mimeType: 'video/mp4' })).toBeNull()
|
||||
expect(mediaImageProps({ mimeType: 'application/pdf', url: '/x.pdf' })).toBeNull()
|
||||
@@ -88,4 +96,14 @@ describe('buildMediaPlaceholderHook', () => {
|
||||
await buildMediaPlaceholderHook()({ args: { data: svgData }, operation: 'create', req: { ...req, file: { data: png, mimetype: 'image/svg+xml' } } } as never)
|
||||
expect(svgData.placeholder).toBeUndefined()
|
||||
})
|
||||
|
||||
test('transparent image: blur kept, no colour', async () => {
|
||||
const logo = await sharp({ create: { background: { alpha: 0, b: 255, g: 255, r: 255 }, channels: 4, height: 100, width: 300 } }).png().toBuffer()
|
||||
const data: Record<string, unknown> = {}
|
||||
const req = { file: { data: logo, mimetype: 'image/png' }, payload: { config: { sharp }, logger: { warn: vi.fn() } } }
|
||||
await buildMediaPlaceholderHook()({ args: { data }, operation: 'create', req } as never)
|
||||
const ph = data.placeholder as { blurDataURL: string; color: null | string }
|
||||
expect(ph.blurDataURL).toMatch(/^data:image\/webp;base64,/)
|
||||
expect(ph.color).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { describe, expect, test, vi } from 'vitest'
|
||||
|
||||
import { createPageRoute } from '../src/modules/frontend/pageRoute.js'
|
||||
|
||||
const thrower = (code: string) => vi.fn((url?: string) => { throw new Error(`${code}${url ? ` ${url}` : ''}`) }) as unknown as (url?: string) => never
|
||||
|
||||
const helpers = (home: null | string) =>
|
||||
({
|
||||
getSystemLinks: vi.fn(async () => (home ? [{ href: home, role: 'homepage' }] : [])),
|
||||
i18n: { defaultLocale: 'pl', locales: [{ code: 'pl' }, { code: 'en' }] },
|
||||
resolvePageRedirect: vi.fn(async () => null),
|
||||
resolveRoute: vi.fn(async () => null),
|
||||
}) as never
|
||||
|
||||
const page = (segments: string[], locale = 'pl') => ({ params: Promise.resolve({ slug: segments, locale }) })
|
||||
|
||||
describe('createPageRoute onMissing', () => {
|
||||
test('default: 404', async () => {
|
||||
const route = createPageRoute(helpers('/pl'), { blocks: {}, notFound: thrower('NOT_FOUND'), permanentRedirect: thrower('308') })
|
||||
await expect(route.Page(page(['uslugi']))).rejects.toThrow('NOT_FOUND')
|
||||
})
|
||||
|
||||
test("'redirectHome': 307 to the homepage in the same language", async () => {
|
||||
const route = createPageRoute(helpers('/en'), {
|
||||
blocks: {}, notFound: thrower('NOT_FOUND'), onMissing: 'redirectHome', permanentRedirect: thrower('308'), redirect: thrower('REDIRECT'),
|
||||
})
|
||||
await expect(route.Page(page(['services'], 'en'))).rejects.toThrow('REDIRECT /en')
|
||||
})
|
||||
|
||||
test("'redirectHome': no homepage assigned, or the homepage itself missing → 404 (no loop)", async () => {
|
||||
const opts = { blocks: {}, notFound: thrower('NOT_FOUND'), onMissing: 'redirectHome' as const, permanentRedirect: thrower('308'), redirect: thrower('REDIRECT') }
|
||||
await expect(createPageRoute(helpers(null), opts).Page(page(['uslugi']))).rejects.toThrow('NOT_FOUND')
|
||||
await expect(createPageRoute(helpers('/pl'), opts).Page(page([]))).rejects.toThrow('NOT_FOUND')
|
||||
})
|
||||
|
||||
test("'redirectHome' without redirect fails at setup; a function decides itself", async () => {
|
||||
expect(() => createPageRoute(helpers('/pl'), { blocks: {}, notFound: thrower('NOT_FOUND'), onMissing: 'redirectHome', permanentRedirect: thrower('308') })).toThrow(/redirect/)
|
||||
const onMissing = vi.fn(({ locale }: { locale: string }) => { throw new Error(`CUSTOM ${locale}`) }) as never
|
||||
const route = createPageRoute(helpers('/pl'), { blocks: {}, notFound: thrower('NOT_FOUND'), onMissing, permanentRedirect: thrower('308') })
|
||||
await expect(route.Page(page(['x']))).rejects.toThrow('CUSTOM pl')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,72 @@
|
||||
import { createElement as h } from 'react'
|
||||
import { renderToStaticMarkup } from 'react-dom/server'
|
||||
import { describe, expect, test } from 'vitest'
|
||||
|
||||
import { buildTableOfContentsField, extractHeadings } from '../src/modules/richtext/headings.js'
|
||||
import { RenderRichText } from '../src/modules/richtext/RenderRichText.js'
|
||||
import { TableOfContents } from '../src/modules/richtext/TableOfContents.js'
|
||||
|
||||
const text = (t: string, format = 0) => ({ type: 'text', detail: 0, format, mode: 'normal', style: '', text: t, version: 1 })
|
||||
const heading = (tag: string, ...children: object[]) => ({ type: 'heading', children, direction: 'ltr', format: '', indent: 0, tag, version: 1 })
|
||||
const paragraph = (t: string) => ({ type: 'paragraph', children: [text(t)], direction: 'ltr', format: '', indent: 0, textFormat: 0, version: 1 })
|
||||
|
||||
const doc = {
|
||||
root: {
|
||||
type: 'root',
|
||||
children: [
|
||||
heading('h2', text('Administrator '), text('danych', 1)),
|
||||
paragraph('Treść'),
|
||||
heading('h3', text('Szczegóły')),
|
||||
heading('h2', text('Pliki cookies')),
|
||||
heading('h2', text('Pliki cookies')),
|
||||
heading('h2'),
|
||||
],
|
||||
direction: 'ltr',
|
||||
format: '',
|
||||
indent: 0,
|
||||
version: 1,
|
||||
},
|
||||
} as never
|
||||
|
||||
describe('table of contents', () => {
|
||||
test('extractHeadings: h2 in order, slug ids, repeats numbered, empty skipped', () => {
|
||||
expect(extractHeadings(doc)).toEqual([
|
||||
{ id: 'administrator-danych', level: 'h2', text: 'Administrator danych' },
|
||||
{ id: 'pliki-cookies', level: 'h2', text: 'Pliki cookies' },
|
||||
{ id: 'pliki-cookies-2', level: 'h2', text: 'Pliki cookies' },
|
||||
])
|
||||
expect(extractHeadings(doc, { levels: ['h2', 'h3'] }).map((h) => h.id)).toContain('szczegoly')
|
||||
expect(extractHeadings(null)).toEqual([])
|
||||
})
|
||||
|
||||
test('RenderRichText headingIds: the same ids on the headings, project converter kept', () => {
|
||||
const html = renderToStaticMarkup(
|
||||
h(RenderRichText, {
|
||||
converters: ({ defaultConverters }) => ({
|
||||
...defaultConverters,
|
||||
heading: ({ node, nodesToJSX }) => h(node.tag, { className: 'title' }, ...nodesToJSX({ nodes: node.children })),
|
||||
}),
|
||||
data: doc,
|
||||
headingIds: { className: 'scroll-mt-24' },
|
||||
}),
|
||||
)
|
||||
expect(html).toContain('<h2 class="title scroll-mt-24" id="administrator-danych">')
|
||||
expect(html).toContain('id="pliki-cookies-2"')
|
||||
expect(html).toMatch(/<h3 class="title">Szczegóły<\/h3>/)
|
||||
expect(renderToStaticMarkup(h(RenderRichText, { data: doc, headingIds: true }))).toContain('<h2 id="pliki-cookies">')
|
||||
})
|
||||
|
||||
test('TableOfContents: links to the ids, first section current before JS', () => {
|
||||
const html = renderToStaticMarkup(h(TableOfContents, { classNames: { linkActive: 'on' }, items: extractHeadings(doc), label: 'Spis treści' }))
|
||||
expect(html).toContain('aria-label="Spis treści"')
|
||||
expect(html).toContain('<a aria-current="location" class="on" href="#administrator-danych">')
|
||||
expect(html).toContain('href="#pliki-cookies-2"')
|
||||
expect(renderToStaticMarkup(h(TableOfContents, { items: [] }))).toBe('')
|
||||
})
|
||||
|
||||
test('panel field: show + localized label', () => {
|
||||
const field = buildTableOfContentsField()
|
||||
expect(field).toMatchObject({ name: 'tableOfContents', interfaceName: 'TableOfContentsSettings' })
|
||||
expect(field.fields.map((f) => ('name' in f ? f.name : ''))).toEqual(['show', 'label'])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user