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