Files
faunosteo/apps/app/tests/unit/clients.validation.test.ts
T
bchanot b38e62770e feat(app): clients list, page, create, edit, archive and restore
Routes /app/clients, /new, /:clientId, /:clientId/edit behind a workspace
gate that remounts per workspace; debounced POST search, archived toggle and
« Afficher plus » pages; form on field-limits (D-R7) with the phone entry
rule and ISO countries; request id per creation intent; conflict shows the
saved values; archive confirmation with the sole-owner preview; animals
read-only; leave guard on in-app links; Clients entry after the dashboard.
2026-10-26 01:59:25 +01:00

114 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest'
import { CLIENT_FIELD_LIMITS } from '@faunosteo/supabase'
import { COUNTRY_CODES, countryName, countryOptions } from '@/clients/countries'
import {
EMPTY_CLIENT_VALUES,
clientFieldErrors,
clientSchema,
normalizePhoneEntry,
type ClientFormValues,
} from '@/clients/validation'
const parse = (patch: Partial<ClientFormValues>) => clientSchema.safeParse({ ...EMPTY_CLIENT_VALUES, ...patch })
const errorsOf = (patch: Partial<ClientFormValues>) => {
const result = parse(patch)
return result.success ? {} : clientFieldErrors(result.error)
}
describe('client form schema (D-R7 on field-limits)', () => {
it('needs only a kind and a display name; empty optionals become null', () => {
const result = parse({ display_name: ' Marie Dupont ' })
expect(result.success && result.data).toEqual({
kind: 'person',
display_name: 'Marie Dupont',
email: null,
phone: null,
address_line1: null,
address_line2: null,
postal_code: null,
city: null,
country: null,
})
})
it('refuses a missing name and names the field', () => {
expect(errorsOf({ display_name: ' ' })).toEqual({ display_name: 'clients.error.required' })
})
it('counts code points like char_length: 120 accepted, 121 refused, astral characters count once', () => {
expect(errorsOf({ display_name: 'é'.repeat(120) })).toEqual({})
expect(errorsOf({ display_name: '𝄞'.repeat(120) })).toEqual({})
expect(errorsOf({ display_name: 'é'.repeat(121) })).toEqual({ display_name: 'clients.error.too_long|120' })
expect(errorsOf({ display_name: 'M', city: 'x'.repeat(CLIENT_FIELD_LIMITS.city + 1) })).toEqual({ city: 'clients.error.too_long|80' })
expect(errorsOf({ display_name: 'M', postal_code: '9'.repeat(17) })).toEqual({ postal_code: 'clients.error.too_long|16' })
expect(errorsOf({ display_name: 'M', address_line1: 'a'.repeat(121) })).toEqual({ address_line1: 'clients.error.too_long|120' })
})
it('refuses control characters, U+2028 included, and trims Unicode spaces', () => {
expect(errorsOf({ display_name: 'Tab\there' })).toEqual({ display_name: 'clients.error.characters' })
expect(errorsOf({ display_name: 'M', city: 'Line
sep' })).toEqual({ city: 'clients.error.characters' })
const result = parse({ display_name: ' Marie ' })
expect(result.success && result.data.display_name).toBe('Marie')
})
it('lower-cases the email and checks the shared pattern and its 254 bound', () => {
const result = parse({ display_name: 'M', email: ' Marie@Example.TEST ' })
expect(result.success && result.data.email).toBe('marie@example.test')
expect(errorsOf({ display_name: 'M', email: 'a b@c.de' })).toEqual({ email: 'clients.error.email_format' })
expect(errorsOf({ display_name: 'M', email: 'a'.repeat(247) + '@ex.test' })).toEqual({ email: 'clients.error.too_long|254' })
expect(errorsOf({ display_name: 'M', email: 'a'.repeat(246) + '@ex.test' })).toEqual({})
})
it('accepts only listed countries and sets none by default', () => {
expect(errorsOf({ display_name: 'M', country: 'XX' })).toEqual({ country: 'clients.error.country' })
const result = parse({ display_name: 'M', country: 'BE' })
expect(result.success && result.data.country).toBe('BE')
})
it('reports every field error at once, the phone included', () => {
expect(errorsOf({ display_name: '', phone: '12' })).toEqual({
display_name: 'clients.error.required',
phone: 'clients.error.phone_format',
})
})
it('stores the converted phone', () => {
const result = parse({ display_name: 'M', phone: '06 12 34 56 78' })
expect(result.success && result.data.phone).toBe('+33612345678')
})
})
describe('phone entry rule (contract §11.3)', () => {
it.each([
['06 12 34 56 78', '', { ok: true, value: '+33612345678' }],
['06.12.34.56.78', 'FR', { ok: true, value: '+33612345678' }],
['+32 475 12 34 56', 'BE', { ok: true, value: '+32475123456' }],
['+33 6 12 34 56 78', 'BE', { ok: true, value: '+33612345678' }],
['', 'BE', { ok: true, value: null }],
['0475 12 34 56', 'BE', { ok: false, error: 'clients.error.phone_country_code' }],
['06 12 34 56 78', 'CH', { ok: false, error: 'clients.error.phone_country_code' }],
['0612', '', { ok: false, error: 'clients.error.phone_format' }],
['0032475123456', 'FR', { ok: false, error: 'clients.error.phone_format' }],
['+0612345678', '', { ok: false, error: 'clients.error.phone_format' }],
])('%s with country %s', (raw, country, expected) => {
expect(normalizePhoneEntry(raw, country)).toEqual(expected)
})
})
describe('countries', () => {
it('commits the 249 ISO 3166-1 alpha-2 codes, unique and upper-case', () => {
expect(COUNTRY_CODES).toHaveLength(249)
expect(new Set(COUNTRY_CODES).size).toBe(249)
expect(COUNTRY_CODES.every((code) => /^[A-Z]{2}$/.test(code))).toBe(true)
})
it('names them in the UI language, sorted by name', () => {
expect(countryName('BE', 'fr')).toBe('Belgique')
expect(countryName('BE', 'en')).toBe('Belgium')
const fr = countryOptions('fr')
expect(fr).toHaveLength(249)
const labels = fr.map((o) => o.label)
expect([...labels].sort((a, b) => a.localeCompare(b, 'fr'))).toEqual(labels)
})
})