Files
ipal-kit/test/links.test.ts
T

107 lines
5.3 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' }] } as const
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'])
})
})
describe('project-defined link styles', () => {
test('appearances accept the project\'s own names and panel labels', async () => {
const { buildLinkField } = await import('../src/modules/links/buildLinkField.js')
const field = buildLinkField({ appearances: ['primary', { label: 'Highlighted', value: 'highlight' }] })
const select = field.fields.find((f) => 'name' in f && f.name === 'appearance') as { defaultValue: string; options: unknown[] }
expect(select.options).toEqual([{ label: 'primary', value: 'primary' }, { label: 'Highlighted', value: 'highlight' }])
expect(select.defaultValue).toBe('primary')
expect(resolveLink({ ...page('oferta'), appearance: 'highlight' }, opts)?.appearance).toBe('highlight')
})
})
describe('resolveSystemLinks', () => {
test('href and label (page title) from System Pages; unassigned roles skipped', async () => {
const { resolveSystemLinks } = await import('../src/modules/links/resolveNav.js')
const settings = {
cookiePolicy: null,
privacyPolicy: { slug: 'polityka-prywatnosci', title: 'Polityka prywatności' },
termsOfService: { slug: 'regulamin', title: 'Regulamin' },
}
expect(resolveSystemLinks(settings, opts).map((l) => [l.role, l.href, l.label])).toEqual([
['privacyPolicy', '/pl/polityka-prywatnosci', 'Polityka prywatności'],
['termsOfService', '/pl/regulamin', 'Regulamin'],
])
expect(resolveSystemLinks(settings, { ...opts, roles: ['termsOfService'] })).toHaveLength(1)
expect(resolveSystemLinks(null, opts)).toEqual([])
})
})