|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { generateTranslationSkeleton, TRANSLATE_PLACEHOLDER } from './translation-skeleton'; |
| 3 | +import type { ServiceObject } from '../data/object.zod'; |
| 4 | + |
| 5 | +// ──────────────────────────────────────────────────────────────────────────── |
| 6 | +// Test fixture — minimal ServiceObject |
| 7 | +// ──────────────────────────────────────────────────────────────────────────── |
| 8 | + |
| 9 | +const mockObject = { |
| 10 | + name: 'test_obj', |
| 11 | + label: 'Test Object', |
| 12 | + pluralLabel: 'Test Objects', |
| 13 | + fields: { |
| 14 | + title: { type: 'text', label: 'Title' }, |
| 15 | + body: { type: 'textarea', label: 'Body', description: 'Main content area' }, |
| 16 | + status: { |
| 17 | + type: 'select', |
| 18 | + label: 'Status', |
| 19 | + options: [ |
| 20 | + { label: 'Open', value: 'open' }, |
| 21 | + { label: 'Closed', value: 'closed' }, |
| 22 | + ], |
| 23 | + }, |
| 24 | + priority: { |
| 25 | + type: 'select', |
| 26 | + label: 'Priority', |
| 27 | + options: [ |
| 28 | + { label: 'Low', value: 'low' }, |
| 29 | + { label: 'Normal', value: 'normal' }, |
| 30 | + { label: 'High', value: 'high' }, |
| 31 | + ], |
| 32 | + }, |
| 33 | + plain: { type: 'number', label: 'Amount' }, |
| 34 | + }, |
| 35 | +} as unknown as ServiceObject; |
| 36 | + |
| 37 | +describe('generateTranslationSkeleton', () => { |
| 38 | + |
| 39 | + it('should output valid JSON', () => { |
| 40 | + const json = generateTranslationSkeleton(mockObject); |
| 41 | + expect(() => JSON.parse(json)).not.toThrow(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should include object label and pluralLabel', () => { |
| 45 | + const skeleton = JSON.parse(generateTranslationSkeleton(mockObject)); |
| 46 | + expect(skeleton.label).toContain(TRANSLATE_PLACEHOLDER); |
| 47 | + expect(skeleton.label).toContain('Test Object'); |
| 48 | + expect(skeleton.pluralLabel).toContain('Test Objects'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should include all field keys', () => { |
| 52 | + const skeleton = JSON.parse(generateTranslationSkeleton(mockObject)); |
| 53 | + const fieldKeys = Object.keys(skeleton.fields); |
| 54 | + expect(fieldKeys).toEqual(['title', 'body', 'status', 'priority', 'plain']); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should add help placeholder for fields with description', () => { |
| 58 | + const skeleton = JSON.parse(generateTranslationSkeleton(mockObject)); |
| 59 | + expect(skeleton.fields.body.help).toContain(TRANSLATE_PLACEHOLDER); |
| 60 | + expect(skeleton.fields.body.help).toContain('Main content area'); |
| 61 | + // Field without description should not have help |
| 62 | + expect(skeleton.fields.title.help).toBeUndefined(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should include options map for select fields', () => { |
| 66 | + const skeleton = JSON.parse(generateTranslationSkeleton(mockObject)); |
| 67 | + expect(skeleton.fields.status.options).toEqual({ |
| 68 | + open: `${TRANSLATE_PLACEHOLDER}: "Open"`, |
| 69 | + closed: `${TRANSLATE_PLACEHOLDER}: "Closed"`, |
| 70 | + }); |
| 71 | + expect(skeleton.fields.priority.options).toEqual({ |
| 72 | + low: `${TRANSLATE_PLACEHOLDER}: "Low"`, |
| 73 | + normal: `${TRANSLATE_PLACEHOLDER}: "Normal"`, |
| 74 | + high: `${TRANSLATE_PLACEHOLDER}: "High"`, |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should not include options for non-select fields', () => { |
| 79 | + const skeleton = JSON.parse(generateTranslationSkeleton(mockObject)); |
| 80 | + expect(skeleton.fields.title.options).toBeUndefined(); |
| 81 | + expect(skeleton.fields.plain.options).toBeUndefined(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should handle object without pluralLabel', () => { |
| 85 | + const objWithoutPlural = { |
| 86 | + ...mockObject, |
| 87 | + pluralLabel: undefined, |
| 88 | + } as unknown as ServiceObject; |
| 89 | + const skeleton = JSON.parse(generateTranslationSkeleton(objWithoutPlural)); |
| 90 | + expect(skeleton.pluralLabel).toBeUndefined(); |
| 91 | + }); |
| 92 | +}); |
0 commit comments