localization.e2e-spec.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { pick } from '@vendure/common/lib/pick';
  2. import { createTestEnvironment } from '@vendure/testing';
  3. import gql from 'graphql-tag';
  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 { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  8. import { LanguageCode } from './graphql/generated-e2e-admin-types';
  9. import * as Codegen from './graphql/generated-e2e-admin-types';
  10. import { GET_PRODUCT_WITH_VARIANTS, UPDATE_PRODUCT } from './graphql/shared-definitions';
  11. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  12. describe('Localization', () => {
  13. const { server, adminClient } = createTestEnvironment(testConfig());
  14. beforeAll(async () => {
  15. await server.init({
  16. initialData,
  17. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  18. customerCount: 1,
  19. });
  20. await adminClient.asSuperAdmin();
  21. const { updateProduct } = await adminClient.query<
  22. Codegen.UpdateProductMutation,
  23. Codegen.UpdateProductMutationVariables
  24. >(UPDATE_PRODUCT, {
  25. input: {
  26. id: 'T_1',
  27. translations: [
  28. {
  29. languageCode: LanguageCode.en,
  30. name: 'en name',
  31. slug: 'en-slug',
  32. description: 'en-description',
  33. },
  34. {
  35. languageCode: LanguageCode.de,
  36. name: 'de name',
  37. slug: 'de-slug',
  38. description: 'de-description',
  39. },
  40. {
  41. languageCode: LanguageCode.zh,
  42. name: 'zh name',
  43. slug: 'zh-slug',
  44. description: 'zh-description',
  45. },
  46. ],
  47. },
  48. });
  49. await adminClient.query<
  50. Codegen.UpdateOptionGroupMutation,
  51. Codegen.UpdateOptionGroupMutationVariables
  52. >(UPDATE_OPTION_GROUP, {
  53. input: {
  54. id: 'T_1',
  55. translations: [
  56. { languageCode: LanguageCode.en, name: 'en name' },
  57. { languageCode: LanguageCode.de, name: 'de name' },
  58. { languageCode: LanguageCode.zh, name: 'zh name' },
  59. ],
  60. },
  61. });
  62. }, TEST_SETUP_TIMEOUT_MS);
  63. afterAll(async () => {
  64. await server.destroy();
  65. });
  66. it('returns default language when none specified', async () => {
  67. const { product } = await adminClient.query<
  68. Codegen.GetProductWithVariantsQuery,
  69. Codegen.GetProductWithVariantsQueryVariables
  70. >(GET_PRODUCT_WITH_VARIANTS, {
  71. id: 'T_1',
  72. });
  73. expect(pick(product!, ['name', 'slug', 'description'])).toEqual({
  74. name: 'en name',
  75. slug: 'en-slug',
  76. description: 'en-description',
  77. });
  78. });
  79. it('returns specified language', async () => {
  80. const { product } = await adminClient.query<
  81. Codegen.GetProductWithVariantsQuery,
  82. Codegen.GetProductWithVariantsQueryVariables
  83. >(
  84. GET_PRODUCT_WITH_VARIANTS,
  85. {
  86. id: 'T_1',
  87. },
  88. { languageCode: LanguageCode.de },
  89. );
  90. expect(pick(product!, ['name', 'slug', 'description'])).toEqual({
  91. name: 'de name',
  92. slug: 'de-slug',
  93. description: 'de-description',
  94. });
  95. });
  96. it('falls back to default language code', async () => {
  97. const { product } = await adminClient.query<
  98. Codegen.GetProductWithVariantsQuery,
  99. Codegen.GetProductWithVariantsQueryVariables
  100. >(
  101. GET_PRODUCT_WITH_VARIANTS,
  102. {
  103. id: 'T_1',
  104. },
  105. { languageCode: LanguageCode.ga },
  106. );
  107. expect(pick(product!, ['name', 'slug', 'description'])).toEqual({
  108. name: 'en name',
  109. slug: 'en-slug',
  110. description: 'en-description',
  111. });
  112. });
  113. it('nested entites are translated', async () => {
  114. const { product } = await adminClient.query<
  115. Codegen.GetProductWithVariantsQuery,
  116. Codegen.GetProductWithVariantsQueryVariables
  117. >(
  118. GET_PRODUCT_WITH_VARIANTS,
  119. {
  120. id: 'T_1',
  121. },
  122. { languageCode: LanguageCode.zh },
  123. );
  124. expect(pick(product!.optionGroups[0], ['name'])).toEqual({
  125. name: 'zh name',
  126. });
  127. });
  128. it('translates results of mutation', async () => {
  129. const { updateProduct } = await adminClient.query<
  130. Codegen.UpdateProductMutation,
  131. Codegen.UpdateProductMutationVariables
  132. >(
  133. UPDATE_PRODUCT,
  134. {
  135. input: {
  136. id: 'T_1',
  137. enabled: true,
  138. },
  139. },
  140. { languageCode: LanguageCode.zh },
  141. );
  142. expect(updateProduct.name).toBe('zh name');
  143. expect(pick(updateProduct.optionGroups[0], ['name'])).toEqual({
  144. name: 'zh name',
  145. });
  146. });
  147. });
  148. const UPDATE_OPTION_GROUP = gql`
  149. mutation UpdateOptionGroup($input: UpdateProductOptionGroupInput!) {
  150. updateProductOptionGroup(input: $input) {
  151. id
  152. }
  153. }
  154. `;