localeForBrowserLanguage.spec.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { EnumTextDirection } from '#shared/graphql/types.ts'
  3. import localeForBrowserLanguage from '../localeForBrowserLanguage.ts'
  4. describe('localeFinder', () => {
  5. let windowSpy: any
  6. beforeEach(() => {
  7. windowSpy = vi.spyOn(window.navigator, 'languages', 'get')
  8. })
  9. afterEach(() => {
  10. windowSpy.mockRestore()
  11. })
  12. const locales = [
  13. {
  14. active: true,
  15. alias: 'de',
  16. dir: EnumTextDirection.Ltr,
  17. locale: 'de-de',
  18. name: 'Deutsch',
  19. },
  20. {
  21. active: true,
  22. alias: 'es',
  23. dir: EnumTextDirection.Ltr,
  24. locale: 'es-es',
  25. name: 'Español',
  26. },
  27. {
  28. active: true,
  29. alias: '',
  30. dir: EnumTextDirection.Ltr,
  31. locale: 'es-co',
  32. name: 'Español (Colombia)',
  33. },
  34. ]
  35. it('returns correct locale for direct match', () => {
  36. windowSpy.mockImplementation(() => ['es-CO'])
  37. expect(localeForBrowserLanguage(locales)).toStrictEqual(locales[2])
  38. })
  39. it('returns correct locale for alias match', () => {
  40. windowSpy.mockImplementation(() => ['es-MX'])
  41. expect(localeForBrowserLanguage(locales)).toStrictEqual(locales[1])
  42. })
  43. it('returns default locale for no match', () => {
  44. windowSpy.mockImplementation(() => ['sv-SV'])
  45. expect(localeForBrowserLanguage(locales)).toStrictEqual({
  46. active: true,
  47. alias: 'en',
  48. dir: EnumTextDirection.Ltr,
  49. locale: 'en-us',
  50. name: 'English (United States)',
  51. })
  52. })
  53. })