|
| 1 | +import { describe, it, expect, expectTypeOf } from 'vitest'; |
| 2 | +import type { StrictObjectTranslation } from './translation-typegen'; |
| 3 | + |
| 4 | +// ──────────────────────────────────────────────────────────────────────────── |
| 5 | +// Test fixtures — minimal object shapes that mimic ObjectSchema.create() output |
| 6 | +// ──────────────────────────────────────────────────────────────────────────── |
| 7 | + |
| 8 | +const SimpleObject = { |
| 9 | + fields: { |
| 10 | + name: { type: 'text' as const, label: 'Name' }, |
| 11 | + email: { type: 'email' as const, label: 'Email' }, |
| 12 | + }, |
| 13 | +}; |
| 14 | + |
| 15 | +const ObjectWithSelect = { |
| 16 | + fields: { |
| 17 | + title: { type: 'text' as const, label: 'Title' }, |
| 18 | + status: { |
| 19 | + type: 'select' as const, |
| 20 | + label: 'Status', |
| 21 | + options: [ |
| 22 | + { label: 'Open', value: 'open' }, |
| 23 | + { label: 'Closed', value: 'closed' }, |
| 24 | + ] as const, |
| 25 | + }, |
| 26 | + priority: { |
| 27 | + type: 'select' as const, |
| 28 | + label: 'Priority', |
| 29 | + options: [ |
| 30 | + { label: 'Low', value: 'low' }, |
| 31 | + { label: 'High', value: 'high' }, |
| 32 | + ] as const, |
| 33 | + }, |
| 34 | + }, |
| 35 | +}; |
| 36 | + |
| 37 | +// ──────────────────────────────────────────────────────────────────────────── |
| 38 | +// Type-level tests |
| 39 | +// ──────────────────────────────────────────────────────────────────────────── |
| 40 | + |
| 41 | +describe('StrictObjectTranslation', () => { |
| 42 | + |
| 43 | + it('should require label at object level', () => { |
| 44 | + type T = StrictObjectTranslation<typeof SimpleObject>; |
| 45 | + expectTypeOf<T>().toHaveProperty('label'); |
| 46 | + expectTypeOf<T['label']>().toBeString(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should make pluralLabel optional', () => { |
| 50 | + type T = StrictObjectTranslation<typeof SimpleObject>; |
| 51 | + expectTypeOf<T>().toHaveProperty('pluralLabel'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should require all field keys', () => { |
| 55 | + type T = StrictObjectTranslation<typeof SimpleObject>; |
| 56 | + type FieldKeys = keyof T['fields']; |
| 57 | + expectTypeOf<FieldKeys>().toEqualTypeOf<'name' | 'email'>(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should require label on non-select fields', () => { |
| 61 | + type T = StrictObjectTranslation<typeof SimpleObject>; |
| 62 | + expectTypeOf<T['fields']['name']>().toHaveProperty('label'); |
| 63 | + expectTypeOf<T['fields']['name']['label']>().toBeString(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should require options map on select fields', () => { |
| 67 | + type T = StrictObjectTranslation<typeof ObjectWithSelect>; |
| 68 | + expectTypeOf<T['fields']['status']>().toHaveProperty('options'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should require all option values as keys in options map', () => { |
| 72 | + type T = StrictObjectTranslation<typeof ObjectWithSelect>; |
| 73 | + type StatusOptions = keyof T['fields']['status']['options']; |
| 74 | + expectTypeOf<StatusOptions>().toEqualTypeOf<'open' | 'closed'>(); |
| 75 | + |
| 76 | + type PriorityOptions = keyof T['fields']['priority']['options']; |
| 77 | + expectTypeOf<PriorityOptions>().toEqualTypeOf<'low' | 'high'>(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should accept a valid complete translation', () => { |
| 81 | + type T = StrictObjectTranslation<typeof ObjectWithSelect>; |
| 82 | + const valid: T = { |
| 83 | + label: 'Test', |
| 84 | + fields: { |
| 85 | + title: { label: 'Title' }, |
| 86 | + status: { |
| 87 | + label: 'Status', |
| 88 | + options: { open: 'Open', closed: 'Closed' }, |
| 89 | + }, |
| 90 | + priority: { |
| 91 | + label: 'Priority', |
| 92 | + options: { low: 'Low', high: 'High' }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }; |
| 96 | + expect(valid.label).toBe('Test'); |
| 97 | + expect(valid.fields.status.options.open).toBe('Open'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should report TS error when a field is missing', () => { |
| 101 | + type T = StrictObjectTranslation<typeof SimpleObject>; |
| 102 | + // @ts-expect-error — missing 'email' field |
| 103 | + const _invalid: T = { |
| 104 | + label: 'Test', |
| 105 | + fields: { |
| 106 | + name: { label: 'Name' }, |
| 107 | + }, |
| 108 | + }; |
| 109 | + expect(_invalid).toBeDefined(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should report TS error when an extra field is present', () => { |
| 113 | + type T = StrictObjectTranslation<typeof SimpleObject>; |
| 114 | + const _invalid: T = { |
| 115 | + label: 'Test', |
| 116 | + fields: { |
| 117 | + name: { label: 'Name' }, |
| 118 | + email: { label: 'Email' }, |
| 119 | + // @ts-expect-error — 'ghost' does not exist in source fields |
| 120 | + ghost: { label: 'Ghost' }, |
| 121 | + }, |
| 122 | + }; |
| 123 | + expect(_invalid).toBeDefined(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should report TS error when an option is missing', () => { |
| 127 | + type T = StrictObjectTranslation<typeof ObjectWithSelect>; |
| 128 | + // @ts-expect-error — missing 'closed' option |
| 129 | + const _invalid: T = { |
| 130 | + label: 'Test', |
| 131 | + fields: { |
| 132 | + title: { label: 'Title' }, |
| 133 | + status: { |
| 134 | + label: 'Status', |
| 135 | + options: { open: 'Open' }, |
| 136 | + }, |
| 137 | + priority: { |
| 138 | + label: 'Priority', |
| 139 | + options: { low: 'Low', high: 'High' }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }; |
| 143 | + expect(_invalid).toBeDefined(); |
| 144 | + }); |
| 145 | +}); |
0 commit comments