localeForBrowserLanguage.spec.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { EnumTextDirection } from '@shared/graphql/types'
  3. import localeForBrowserLanguage from '../localeForBrowserLanguage'
  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. })