localization.e2e-spec.ts 5.4 KB

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