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