62 lines
1.6 KiB
TypeScript
62 lines
1.6 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { deriveFieldKey, deriveTypeKey } from '@/lib/schemaBuilderKeys'
|
|
|
|
describe('type key from the name', () => {
|
|
it.each([
|
|
['Events', 'events'],
|
|
['Press mentions', 'press-mentions'],
|
|
['Café & Bar', 'cafe-bar'],
|
|
[' Job openings! ', 'job-openings'],
|
|
['2026 Awards', '2026-awards'],
|
|
['!!!', ''],
|
|
['', ''],
|
|
])('%j gives %j', (name, key) => {
|
|
expect(deriveTypeKey(name)).toBe(key)
|
|
})
|
|
|
|
it('keeps the first 40 characters of a long name', () => {
|
|
const name = 'abcdefghij'.repeat(6)
|
|
|
|
expect(deriveTypeKey(name)).toBe(name.slice(0, 40))
|
|
})
|
|
|
|
it('drops a hyphen left at the end by the cut', () => {
|
|
const name = `${'a'.repeat(39)} bbbbbbbbbb`
|
|
|
|
expect(deriveTypeKey(name)).toBe('a'.repeat(39))
|
|
})
|
|
})
|
|
|
|
describe('field key from the label', () => {
|
|
it.each([
|
|
['Title', 'title'],
|
|
['Starts on', 'starts_on'],
|
|
['Éléments', 'elements'],
|
|
['Ends on (date)', 'ends_on_date'],
|
|
['2nd room', 'nd_room'],
|
|
['!!!', ''],
|
|
['', ''],
|
|
])('%j gives %j', (label, key) => {
|
|
expect(deriveFieldKey(label, [])).toBe(key)
|
|
})
|
|
|
|
it('adds _2 when the key already exists, then _3', () => {
|
|
expect(deriveFieldKey('Title', ['title'])).toBe('title_2')
|
|
expect(deriveFieldKey('Title', ['title', 'title_2'])).toBe('title_3')
|
|
})
|
|
|
|
it('keeps the first 40 characters of a long label', () => {
|
|
const label = 'abcdefghij'.repeat(6)
|
|
|
|
expect(deriveFieldKey(label, [])).toBe(label.slice(0, 40))
|
|
})
|
|
|
|
it('keeps the suffixed key within 40 characters', () => {
|
|
const long = 'a'.repeat(40)
|
|
|
|
const key = deriveFieldKey(long, [long])
|
|
|
|
expect(key).toBe(`${'a'.repeat(38)}_2`)
|
|
})
|
|
})
|