storeLocales.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { applyTestConfig } from './helpers/testConfig.js';
  4. import {
  5. currencyMeta,
  6. isSupportedCurrency,
  7. resolveCurrency,
  8. resolveLocale,
  9. } from '../src/domain/storeLocales.js';
  10. test('language maps to its natural currency when the shop sells in it', () => {
  11. applyTestConfig();
  12. assert.equal(resolveLocale('pl').currency, 'PLN');
  13. assert.equal(resolveLocale('cs').currency, 'CZK');
  14. assert.equal(resolveLocale('de').currency, 'EUR');
  15. });
  16. test('unsupported natural currency falls back to EUR with a note', () => {
  17. applyTestConfig();
  18. const locale = resolveLocale('tr');
  19. assert.equal(locale.currency, 'EUR');
  20. assert.equal(locale.fallback, true);
  21. assert.equal(locale.reason, 'unsupported_currency');
  22. assert.equal(locale.naturalCurrency, 'TRY');
  23. assert.match(String(currencyMeta(locale, 'tr').currency_note), /TRY/);
  24. });
  25. test('an unknown language code falls back to EUR', () => {
  26. applyTestConfig();
  27. const locale = resolveLocale('xx');
  28. assert.equal(locale.currency, 'EUR');
  29. assert.equal(locale.reason, 'unknown_language');
  30. });
  31. test('no language at all is EUR without a fallback flag', () => {
  32. applyTestConfig();
  33. const locale = resolveLocale(null);
  34. assert.equal(locale.currency, 'EUR');
  35. assert.equal(locale.fallback, false);
  36. });
  37. test('an explicit supported currency override wins over the language', () => {
  38. applyTestConfig();
  39. assert.equal(resolveCurrency('pl', 'CZK').currency, 'CZK');
  40. });
  41. test('an unsupported currency override is ignored, language mapping applies', () => {
  42. applyTestConfig();
  43. assert.equal(resolveCurrency('pl', 'JPY').currency, 'PLN');
  44. });
  45. test('STORE_CURRENCIES drives what counts as supported', () => {
  46. applyTestConfig({ STORE_CURRENCIES: 'PLN,EUR' });
  47. assert.equal(isSupportedCurrency('CZK'), false);
  48. assert.equal(resolveLocale('cs').currency, 'EUR');
  49. assert.equal(resolveLocale('cs').fallback, true);
  50. });
  51. test('currencyMeta omits the fallback fields on a clean resolution', () => {
  52. applyTestConfig();
  53. const meta = currencyMeta(resolveLocale('pl'), 'pl');
  54. assert.deepEqual(meta, { currency: 'PLN' });
  55. });