detect-locale.test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {detectLocale} from '../../../src/lib/detect-locale.js';
  2. const supportedLocales = ['en', 'es', 'pt-br', 'de', 'it'];
  3. Object.defineProperty(window.location,
  4. 'search',
  5. {value: '?name=val', configurable: true}
  6. );
  7. Object.defineProperty(window.navigator,
  8. 'language',
  9. {value: 'en-US', configurable: true}
  10. );
  11. describe('detectLocale', () => {
  12. test('uses locale from the URL when present', () => {
  13. Object.defineProperty(window.location,
  14. 'search',
  15. {value: '?locale=pt-br'}
  16. );
  17. expect(detectLocale(supportedLocales)).toEqual('pt-br');
  18. });
  19. test('is case insensitive', () => {
  20. Object.defineProperty(window.location,
  21. 'search',
  22. {value: '?locale=pt-BR'}
  23. );
  24. expect(detectLocale(supportedLocales)).toEqual('pt-br');
  25. });
  26. test('also accepts lang from the URL when present', () => {
  27. Object.defineProperty(window.location,
  28. 'search',
  29. {value: '?lang=it'}
  30. );
  31. expect(detectLocale(supportedLocales)).toEqual('it');
  32. });
  33. test('ignores unsupported locales', () => {
  34. Object.defineProperty(window.location,
  35. 'search',
  36. {value: '?lang=sv'}
  37. );
  38. expect(detectLocale(supportedLocales)).toEqual('en');
  39. });
  40. test('ignores other parameters', () => {
  41. Object.defineProperty(window.location,
  42. 'search',
  43. {value: '?enable=language'}
  44. );
  45. expect(detectLocale(supportedLocales)).toEqual('en');
  46. });
  47. test('uses navigator language property for default if supported', () => {
  48. Object.defineProperty(window.navigator,
  49. 'language',
  50. {value: 'pt-BR'}
  51. );
  52. expect(detectLocale(supportedLocales)).toEqual('pt-br');
  53. });
  54. test('ignores navigator language property if unsupported', () => {
  55. Object.defineProperty(window.navigator,
  56. 'language',
  57. {value: 'da'}
  58. );
  59. expect(detectLocale(supportedLocales)).toEqual('en');
  60. });
  61. test('works with an empty locale', () => {
  62. Object.defineProperty(window.location,
  63. 'search',
  64. {value: '?locale='}
  65. );
  66. expect(detectLocale(supportedLocales)).toEqual('en');
  67. });
  68. test('if multiple, uses the first locale', () => {
  69. Object.defineProperty(window.location,
  70. 'search',
  71. {value: '?locale=de&locale=en'}
  72. );
  73. expect(detectLocale(supportedLocales)).toEqual('de');
  74. });
  75. });