58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import type { BuiltinField } from '@/composables/useContentTypes'
|
|
import { fieldTypeSpec } from '@/lib/fieldTypes'
|
|
import { fieldRules } from '@/lib/schemaFields'
|
|
|
|
function field(overrides: Partial<BuiltinField>): BuiltinField {
|
|
return {
|
|
key: 'f',
|
|
label: 'F',
|
|
type: 'text',
|
|
required: false,
|
|
unique: false,
|
|
default: null,
|
|
min: null,
|
|
max: null,
|
|
options: null,
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
describe('field types', () => {
|
|
it.each([
|
|
['text', 'Text', 'i-lucide-type'],
|
|
['richtext', 'Rich text', 'i-lucide-pilcrow'],
|
|
['number', 'Number', 'i-lucide-hash'],
|
|
['boolean', 'Yes or no', 'i-lucide-toggle-left'],
|
|
['date', 'Date', 'i-lucide-calendar'],
|
|
['select', 'Choice', 'i-lucide-list'],
|
|
])('%s reads %s with %s', (type, label, icon) => {
|
|
expect(fieldTypeSpec(type).label()).toBe(label)
|
|
expect(fieldTypeSpec(type).icon).toBe(icon)
|
|
})
|
|
})
|
|
|
|
describe('field rules', () => {
|
|
it.each([
|
|
[{ type: 'text', required: true, max: 200 }, 'Required · up to 200 characters'],
|
|
[{ type: 'text', required: true, unique: true }, 'Required · unique'],
|
|
[{ type: 'richtext', max: 10000 }, 'Up to 10,000 characters'],
|
|
[{ type: 'boolean', default: false }, 'Off by default'],
|
|
[{ type: 'boolean', default: true }, 'On by default'],
|
|
[{ type: 'date' }, ''],
|
|
[{ type: 'text', min: 3, max: 80 }, '3 to 80 characters'],
|
|
[{ type: 'text', min: 3 }, 'At least 3 characters'],
|
|
[{ type: 'number', min: 1, max: 5000 }, 'Between 1 and 5000'],
|
|
[{ type: 'number', min: 1 }, 'At least 1'],
|
|
[{ type: 'number', max: 5000 }, 'At most 5000'],
|
|
[{ type: 'select', options: ['a', 'b', 'c'] }, '3 choices'],
|
|
[{ type: 'text', default: 'Draft' }, 'Default: Draft'],
|
|
])('%j reads %s', (settings, rules) => {
|
|
expect(fieldRules(field(settings as Partial<BuiltinField>))).toBe(rules)
|
|
})
|
|
|
|
it('reads a missing setting as absent', () => {
|
|
expect(fieldRules({ key: 'k', label: 'K', type: 'date' } as BuiltinField)).toBe('')
|
|
})
|
|
})
|