|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { getTranslation, type TranslationsConfig } from './translations'; |
| 3 | + |
| 4 | +describe('getTranslation', () => { |
| 5 | + it('should return default English translation when no custom translations provided', () => { |
| 6 | + const translation = getTranslation('errors', 'userNotFound'); |
| 7 | + expect(translation).toBe('No account found with this email address'); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should use custom translation when provided', () => { |
| 11 | + const customTranslations: TranslationsConfig = { |
| 12 | + en: { |
| 13 | + errors: { |
| 14 | + userNotFound: 'Custom error message', |
| 15 | + }, |
| 16 | + }, |
| 17 | + }; |
| 18 | + |
| 19 | + const translation = getTranslation('errors', 'userNotFound', customTranslations); |
| 20 | + expect(translation).toBe('Custom error message'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should use custom translation in specified language', () => { |
| 24 | + const customTranslations: TranslationsConfig = { |
| 25 | + es: { |
| 26 | + errors: { |
| 27 | + userNotFound: 'Usuario no encontrado', |
| 28 | + }, |
| 29 | + }, |
| 30 | + }; |
| 31 | + |
| 32 | + const translation = getTranslation('errors', 'userNotFound', customTranslations, 'es'); |
| 33 | + expect(translation).toBe('Usuario no encontrado'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should fallback to English when specified language is not available', () => { |
| 37 | + const customTranslations: TranslationsConfig = { |
| 38 | + en: { |
| 39 | + errors: { |
| 40 | + userNotFound: 'Custom English message', |
| 41 | + }, |
| 42 | + }, |
| 43 | + }; |
| 44 | + |
| 45 | + const translation = getTranslation('errors', 'userNotFound', customTranslations, 'fr'); |
| 46 | + expect(translation).toBe('Custom English message'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should fallback to default English when no custom translations match', () => { |
| 50 | + const customTranslations: TranslationsConfig = { |
| 51 | + es: { |
| 52 | + errors: { |
| 53 | + wrongPassword: 'Contraseña incorrecta', |
| 54 | + }, |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + const translation = getTranslation('errors', 'userNotFound', customTranslations, 'es'); |
| 59 | + expect(translation).toBe('No account found with this email address'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should work with different translation categories', () => { |
| 63 | + const translation = getTranslation('messages', 'passwordResetEmailSent'); |
| 64 | + expect(translation).toBe('Password reset email sent successfully'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle partial custom translations', () => { |
| 68 | + const customTranslations: TranslationsConfig = { |
| 69 | + en: { |
| 70 | + errors: { |
| 71 | + // Only override one error message |
| 72 | + userNotFound: 'Custom not found message', |
| 73 | + }, |
| 74 | + }, |
| 75 | + }; |
| 76 | + |
| 77 | + const userNotFound = getTranslation('errors', 'userNotFound', customTranslations); |
| 78 | + const wrongPassword = getTranslation('errors', 'wrongPassword', customTranslations); |
| 79 | + |
| 80 | + expect(userNotFound).toBe('Custom not found message'); |
| 81 | + expect(wrongPassword).toBe('Incorrect password'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should handle empty custom translations object', () => { |
| 85 | + const customTranslations: TranslationsConfig = {}; |
| 86 | + const translation = getTranslation('errors', 'userNotFound', customTranslations); |
| 87 | + expect(translation).toBe('No account found with this email address'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should handle undefined custom translations', () => { |
| 91 | + const translation = getTranslation('errors', 'userNotFound', undefined); |
| 92 | + expect(translation).toBe('No account found with this email address'); |
| 93 | + }); |
| 94 | +}); |
0 commit comments