localization.e2e-spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { LanguageCode } from '@vendure/common/lib/generated-types';
  2. import { pick } from '@vendure/common/lib/pick';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import path from 'path';
  5. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  8. import {
  9. getProductWithVariantsDocument,
  10. updateProductDocument,
  11. updateProductOptionGroupDocument,
  12. } from './graphql/shared-definitions';
  13. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  14. describe('Localization', () => {
  15. const { server, adminClient } = createTestEnvironment(testConfig());
  16. beforeAll(async () => {
  17. await server.init({
  18. initialData,
  19. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  20. customerCount: 1,
  21. });
  22. await adminClient.asSuperAdmin();
  23. const { updateProduct } = await adminClient.query(updateProductDocument, {
  24. input: {
  25. id: 'T_1',
  26. translations: [
  27. {
  28. languageCode: LanguageCode.en,
  29. name: 'en name',
  30. slug: 'en-slug',
  31. description: 'en-description',
  32. },
  33. {
  34. languageCode: LanguageCode.de,
  35. name: 'de name',
  36. slug: 'de-slug',
  37. description: 'de-description',
  38. },
  39. {
  40. languageCode: LanguageCode.zh,
  41. name: 'zh name',
  42. slug: 'zh-slug',
  43. description: 'zh-description',
  44. },
  45. ],
  46. },
  47. });
  48. await adminClient.query(updateProductOptionGroupDocument, {
  49. input: {
  50. id: 'T_1',
  51. translations: [
  52. { languageCode: LanguageCode.en, name: 'en name' },
  53. { languageCode: LanguageCode.de, name: 'de name' },
  54. { languageCode: LanguageCode.zh, name: 'zh name' },
  55. ],
  56. },
  57. });
  58. }, TEST_SETUP_TIMEOUT_MS);
  59. afterAll(async () => {
  60. await server.destroy();
  61. });
  62. it('returns default language when none specified', async () => {
  63. const { product } = await adminClient.query(getProductWithVariantsDocument, {
  64. id: 'T_1',
  65. });
  66. expect(pick(product!, ['name', 'slug', 'description'])).toEqual({
  67. name: 'en name',
  68. slug: 'en-slug',
  69. description: 'en-description',
  70. });
  71. });
  72. it('returns specified language', async () => {
  73. const { product } = await adminClient.query(
  74. getProductWithVariantsDocument,
  75. {
  76. id: 'T_1',
  77. },
  78. { languageCode: LanguageCode.de },
  79. );
  80. expect(pick(product!, ['name', 'slug', 'description'])).toEqual({
  81. name: 'de name',
  82. slug: 'de-slug',
  83. description: 'de-description',
  84. });
  85. });
  86. it('falls back to default language code', async () => {
  87. const { product } = await adminClient.query(
  88. getProductWithVariantsDocument,
  89. {
  90. id: 'T_1',
  91. },
  92. { languageCode: LanguageCode.ga },
  93. );
  94. expect(pick(product!, ['name', 'slug', 'description'])).toEqual({
  95. name: 'en name',
  96. slug: 'en-slug',
  97. description: 'en-description',
  98. });
  99. });
  100. it('nested entites are translated', async () => {
  101. const { product } = await adminClient.query(
  102. getProductWithVariantsDocument,
  103. {
  104. id: 'T_1',
  105. },
  106. { languageCode: LanguageCode.zh },
  107. );
  108. expect(pick(product!.optionGroups[0], ['name'])).toEqual({
  109. name: 'zh name',
  110. });
  111. });
  112. it('translates results of mutation', async () => {
  113. const { updateProduct } = await adminClient.query(
  114. updateProductDocument,
  115. {
  116. input: {
  117. id: 'T_1',
  118. enabled: true,
  119. },
  120. },
  121. { languageCode: LanguageCode.zh },
  122. );
  123. expect(updateProduct.name).toBe('zh name');
  124. expect(pick(updateProduct.optionGroups[0], ['name'])).toEqual({
  125. name: 'zh name',
  126. });
  127. });
  128. });