localization.e2e-spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import gql from 'graphql-tag';
  2. import path from 'path';
  3. import { pick } from '../../common/lib/pick';
  4. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  5. import {
  6. GetProductWithVariants,
  7. LanguageCode,
  8. UpdateOptionGroup,
  9. UpdateProduct,
  10. } from './graphql/generated-e2e-admin-types';
  11. import { GET_PRODUCT_WITH_VARIANTS, UPDATE_PRODUCT } from './graphql/shared-definitions';
  12. import { TestAdminClient } from './test-client';
  13. import { TestServer } from './test-server';
  14. /* tslint:disable:no-non-null-assertion */
  15. describe('Role resolver', () => {
  16. const client = new TestAdminClient();
  17. const server = new TestServer();
  18. beforeAll(async () => {
  19. const token = await server.init({
  20. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  21. customerCount: 1,
  22. });
  23. await client.init();
  24. const { updateProduct } = await client.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 client.query<UpdateOptionGroup.Mutation, UpdateOptionGroup.Variables>(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 client.query<
  68. GetProductWithVariants.Query,
  69. GetProductWithVariants.Variables
  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 client.query<
  81. GetProductWithVariants.Query,
  82. GetProductWithVariants.Variables
  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 client.query<
  98. GetProductWithVariants.Query,
  99. GetProductWithVariants.Variables
  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 client.query<
  115. GetProductWithVariants.Query,
  116. GetProductWithVariants.Variables
  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 client.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  130. UPDATE_PRODUCT,
  131. {
  132. input: {
  133. id: 'T_1',
  134. enabled: true,
  135. },
  136. },
  137. { languageCode: LanguageCode.zh },
  138. );
  139. expect(updateProduct.name).toBe('zh name');
  140. expect(pick(updateProduct.optionGroups[0], ['name'])).toEqual({
  141. name: 'zh name',
  142. });
  143. });
  144. });
  145. const UPDATE_OPTION_GROUP = gql`
  146. mutation UpdateOptionGroup($input: UpdateProductOptionGroupInput!) {
  147. updateProductOptionGroup(input: $input) {
  148. id
  149. }
  150. }
  151. `;