Files
ipal-kit/test/links.test.ts
T
radoslaw.smolinskiandClaude Opus 5.5 412bf953a8 Import instead of code: panel forms (steps, choice lists, dates), layout, page route, GEO routes, links, section fields
- Forms: step and checkboxGroup field types (radio, date on), server validation for them,
  useBuilderForm (logic) + BuilderForm (unstyled default) + RenderForm (/rsc: rich text,
  bound action, public part of the form only — the Emails tab never reaches the browser)
- Notifications: next / back / stepOf texts
- createContentHelpers: getSiteShell, getFormTexts, getLinkOptions, resolveLink, resolveNav,
  resolvePageRedirect, one-line route handlers (llms, llms-full, md, mcp, health, indexnow, manifest)
- /rsc: SiteProviders, createPageRoute, createNotFoundPage
- resolveLink / resolveNav / homeSlugFrom, buildSectionHeaderField / buildSectionSettingsField
- Dev sandbox frontend built on these imports; tests (unit + real Payload)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:32:46 +02:00

79 lines
3.8 KiB
TypeScript

import { describe, expect, test } from 'vitest'
import { buildSectionHeaderField, buildSectionSettingsField } from '../src/modules/blocks/sectionFields.js'
import { homeSlugFrom, resolveLink, resolveNav } from '../src/modules/links/resolveNav.js'
const config = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }, { code: 'en', label: 'EN' }] }
const opts = { config, homeSlug: 'start', locale: 'pl' }
const page = (slug: string) => ({ type: 'internal' as const, label: slug, page: { slug } })
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: {} })
})
test('files always open in a new tab with rel; appearance is passed through', () => {
expect(resolveLink({ type: 'file', appearance: 'primary', file: { url: '/media/cennik.pdf' }, label: 'Cennik' }, opts)).toMatchObject({
appearance: 'primary',
href: '/media/cennik.pdf',
internal: false,
targetProps: { rel: 'noopener noreferrer', target: '_blank' },
})
})
test('nowhere → null: unsafe URL, missing page, empty link', () => {
expect(resolveLink({ type: 'external', url: 'javascript:alert(1)' }, opts)).toBeNull()
expect(resolveLink({ type: 'internal', page: 5 }, opts)).toBeNull()
expect(resolveLink(null, opts)).toBeNull()
})
test('the homepage collapses to the locale root', () => {
expect(resolveLink(page('start'), opts)?.href).toBe('/pl')
})
})
describe('resolveNav', () => {
test('nested items resolved; dead or unlabeled items dropped', () => {
const items = [
{ link: page('oferta'), subItems: [{ link: page('www') }, { link: { type: 'internal', label: 'x', page: null } }] },
{ link: { type: 'external', url: 'https://x.pl' } }, // no label
{ link: page('kontakt') },
]
const nav = resolveNav(items, opts)
expect(nav.map((i) => [i.href, i.children.map((c) => c.href)])).toEqual([
['/pl/oferta', ['/pl/www']],
['/pl/kontakt', []],
])
})
test('custom item shape and junk input', () => {
expect(resolveNav([{ children: [{ to: page('a') }], to: page('b') }], { ...opts, childrenField: 'children', linkField: 'to' })[0].children[0].href).toBe('/pl/a')
expect(resolveNav(null, opts)).toEqual([])
expect(resolveNav('x', opts)).toEqual([])
})
test('homeSlugFrom reads a populated homepage only', () => {
expect(homeSlugFrom({ homepage: { slug: 'start' } })).toBe('start')
expect(homeSlugFrom({ homepage: 3 })).toBeUndefined()
expect(homeSlugFrom(null)).toBeUndefined()
})
})
describe('section fields', () => {
test('defaults match the documented shape (types SectionHeaderData / SectionSettings)', () => {
const header = buildSectionHeaderField()
expect(header).toMatchObject({ name: 'header', interfaceName: 'SectionHeaderData' })
expect(header.fields.map((f) => ('name' in f ? f.name : ''))).toEqual(['eyebrow', 'heading', 'intro'])
const settings = buildSectionSettingsField()
expect(settings).toMatchObject({ name: 'settings', admin: { position: 'sidebar' }, interfaceName: 'SectionSettings' })
expect(settings.fields.map((f) => ('name' in f ? f.name : ''))).toEqual(['anchor', 'background'])
})
test('the project decides: own backgrounds, no anchor, extra fields', () => {
const s = buildSectionSettingsField({ anchor: false, backgrounds: [{ label: 'Brand', value: 'brand' }], fields: [{ name: 'spacing', type: 'text' }] })
expect(s.fields.map((f) => ('name' in f ? f.name : ''))).toEqual(['background', 'spacing'])
expect(s.fields[0]).toMatchObject({ defaultValue: 'brand' })
expect(buildSectionSettingsField({ backgrounds: false }).fields.map((f) => ('name' in f ? f.name : ''))).toEqual(['anchor'])
})
})