template.spec.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import type { TemplateSummary } from '../interfaces';
  2. import { extractSupportedLocales, getLocalizedTemplate } from './template';
  3. describe('getLocalizedTemplate', () => {
  4. it('should return undefined if templateSummary is undefined', () => {
  5. expect(getLocalizedTemplate(undefined)).toBeUndefined();
  6. });
  7. it('should return the default template if locale is not provided', () => {
  8. const templateSummary: TemplateSummary = {
  9. default: {
  10. id: 'templateId',
  11. locale: 'en_US',
  12. isValid: true,
  13. isDefault: true,
  14. title: 'Default Title',
  15. },
  16. };
  17. expect(getLocalizedTemplate(templateSummary)).toEqual(
  18. templateSummary.default,
  19. );
  20. });
  21. it('should return the localized template if locale is provided and exists in templateSummary', () => {
  22. const templateSummary: TemplateSummary = {
  23. default: {
  24. id: 'templateId',
  25. locale: 'en_US',
  26. isValid: true,
  27. isDefault: true,
  28. title: 'Default Title',
  29. },
  30. ja_JP: {
  31. id: 'templateId',
  32. locale: 'ja_JP',
  33. isValid: true,
  34. isDefault: false,
  35. title: 'Japanese Title',
  36. },
  37. };
  38. expect(getLocalizedTemplate(templateSummary, 'ja_JP')).toEqual(
  39. templateSummary.ja_JP,
  40. );
  41. });
  42. it('should return the default template if locale is provided but does not exist in templateSummary', () => {
  43. const templateSummary: TemplateSummary = {
  44. default: {
  45. id: 'templateId',
  46. locale: 'en_US',
  47. isValid: true,
  48. isDefault: true,
  49. title: 'Default Title',
  50. },
  51. };
  52. expect(getLocalizedTemplate(templateSummary, 'fr')).toEqual(
  53. templateSummary.default,
  54. );
  55. });
  56. });
  57. describe('extractSupportedLocales', () => {
  58. it('should return undefined if templateSummary is undefined', () => {
  59. expect(extractSupportedLocales(undefined)).toBeUndefined();
  60. });
  61. it('should return a set of locales from the templateSummary', () => {
  62. const templateSummary: TemplateSummary = {
  63. default: {
  64. id: 'templateId',
  65. locale: 'en_US',
  66. isValid: true,
  67. isDefault: true,
  68. title: 'Default Title',
  69. },
  70. ja_JP: {
  71. id: 'templateId',
  72. locale: 'ja_JP',
  73. isValid: true,
  74. isDefault: false,
  75. title: 'Japanese Title',
  76. },
  77. };
  78. expect(extractSupportedLocales(templateSummary)).toEqual(
  79. new Set(['en_US', 'ja_JP']),
  80. );
  81. });
  82. });