-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: localization tests + tiny format refactor
- Loading branch information
Showing
6 changed files
with
133 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { | ||
LocalLocalizationService, | ||
type Locale, | ||
type LocalizationService, | ||
type Translations | ||
} from '.'; | ||
|
||
const translations = { | ||
greet: (name: string) => `Hello, ${name}!`, | ||
bye: 'Goodbye!' | ||
} satisfies Translations; | ||
|
||
const en = { | ||
code: 'en', | ||
displayName: 'English', | ||
translations | ||
} as const satisfies Locale<typeof translations>; | ||
|
||
const fr = { | ||
code: 'fr', | ||
displayName: 'Français', | ||
translations: { | ||
greet: (name: string) => `Bonjour, ${name}!`, | ||
bye: 'Au revoir!' | ||
} | ||
} as const satisfies Locale<typeof translations>; | ||
|
||
describe('the LocalLocalizationService', () => { | ||
const locales = [en, fr] satisfies Locale<typeof translations>[]; | ||
|
||
it('should be initialized with the fallback locale if no locale is set', () => { | ||
const service: LocalizationService<typeof translations, typeof locales> = | ||
new LocalLocalizationService({ | ||
fallback: 'en', | ||
locales | ||
}); | ||
|
||
expect(service.locale()).toBe('en'); | ||
}); | ||
|
||
it('should be initialized with the fallback locale if the default locale does not exist', () => { | ||
const service: LocalizationService<typeof translations, typeof locales> = | ||
new LocalLocalizationService({ | ||
fallback: 'en', | ||
default: 'it', | ||
locales | ||
}); | ||
|
||
expect(service.locale()).toBe('en'); | ||
}); | ||
|
||
it('should be initialized with the given locale if set', () => { | ||
const service: LocalizationService<typeof translations, typeof locales> = | ||
new LocalLocalizationService({ | ||
fallback: 'en', | ||
default: 'fr', | ||
locales | ||
}); | ||
|
||
expect(service.locale()).toBe('fr'); | ||
}); | ||
|
||
it('should be able to change the locale', () => { | ||
const service: LocalizationService<typeof translations, typeof locales> = | ||
new LocalLocalizationService({ | ||
fallback: 'en', | ||
locales | ||
}); | ||
|
||
service.locale('fr'); | ||
|
||
expect(service.locale()).toBe('fr'); | ||
}); | ||
|
||
const service: LocalizationService<typeof translations, typeof locales> = | ||
new LocalLocalizationService({ | ||
fallback: 'en', | ||
locales | ||
}); | ||
|
||
it('should returns all available locales', () => { | ||
expect(service.locales()).toEqual(locales); | ||
}); | ||
|
||
it('should returns the translation key if the translation does not exist', () => { | ||
expect(service.translate('not-found' as any)).toBe('not-found'); | ||
}); | ||
|
||
it('should correctly translate a string', () => { | ||
expect(service.translate('bye')).toBe('Goodbye!'); | ||
}); | ||
|
||
it('should correctly translate a string with arguments', () => { | ||
expect(service.translate('greet', ['John'])).toBe('Hello, John!'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters