localization.e2e-spec.ts 5.4 KB

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