asset-custom-fields.e2e-spec.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { LanguageCode } from '@vendure/common/lib/generated-types';
  2. import { mergeConfig } from '@vendure/core';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import path from 'node:path';
  5. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  8. import { graphql } from './graphql/graphql-admin';
  9. describe('Asset with translatable custom fields', () => {
  10. const { server, adminClient } = createTestEnvironment(
  11. mergeConfig(testConfig(), {
  12. customFields: {
  13. Asset: [
  14. { name: 'alt', type: 'localeString' as const },
  15. { name: 'title', type: 'localeString' as const },
  16. ],
  17. },
  18. }),
  19. );
  20. let assetId: string;
  21. beforeAll(async () => {
  22. await server.init({
  23. initialData,
  24. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  25. customerCount: 1,
  26. });
  27. await adminClient.asSuperAdmin();
  28. }, TEST_SETUP_TIMEOUT_MS);
  29. afterAll(async () => {
  30. await server.destroy();
  31. });
  32. it('creates an asset with translatable custom fields', async () => {
  33. const filesToUpload = [path.join(__dirname, 'fixtures/assets/pps1.jpg')];
  34. const { createAssets } = await adminClient.fileUploadMutation({
  35. mutation: createAssetsWithCustomFieldsDocument,
  36. filePaths: filesToUpload,
  37. mapVariables: filePaths => ({
  38. input: filePaths.map(p => ({
  39. file: null,
  40. translations: [
  41. {
  42. languageCode: LanguageCode.en,
  43. name: 'pps1.jpg',
  44. customFields: {
  45. alt: 'Default alt text',
  46. title: 'Default title',
  47. },
  48. },
  49. ],
  50. })),
  51. }),
  52. });
  53. expect(createAssets.length).toBe(1);
  54. const asset = createAssets[0];
  55. expect(asset).toHaveProperty('name', 'pps1.jpg');
  56. expect(asset).toHaveProperty('customFields');
  57. expect(asset.customFields.alt).toBe('Default alt text');
  58. expect(asset.customFields.title).toBe('Default title');
  59. assetId = asset.id;
  60. });
  61. it('updates asset with English translations', async () => {
  62. const { updateAsset } = await adminClient.query(updateAssetWithCustomFieldsDocument, {
  63. input: {
  64. id: assetId,
  65. translations: [
  66. {
  67. languageCode: LanguageCode.en,
  68. customFields: {
  69. alt: 'English alt text',
  70. title: 'English title',
  71. },
  72. },
  73. ],
  74. },
  75. });
  76. expect(updateAsset.customFields.alt).toBe('English alt text');
  77. expect(updateAsset.customFields.title).toBe('English title');
  78. });
  79. it('updates asset with German translations', async () => {
  80. const { updateAsset } = await adminClient.query(
  81. updateAssetWithCustomFieldsDocument,
  82. {
  83. input: {
  84. id: assetId,
  85. translations: [
  86. {
  87. languageCode: LanguageCode.de,
  88. name: 'pps1.jpg',
  89. customFields: {
  90. alt: 'German alt text',
  91. title: 'German title',
  92. },
  93. },
  94. ],
  95. },
  96. },
  97. { languageCode: LanguageCode.de },
  98. );
  99. expect(updateAsset.customFields.alt).toBe('German alt text');
  100. expect(updateAsset.customFields.title).toBe('German title');
  101. });
  102. it('retrieves English translations when querying in English', async () => {
  103. const { asset } = await adminClient.query(
  104. getAssetWithCustomFieldsDocument,
  105. { id: assetId },
  106. { languageCode: LanguageCode.en },
  107. );
  108. expect(asset).not.toBeNull();
  109. if (asset) {
  110. expect(asset.customFields.alt).toBe('English alt text');
  111. expect(asset.customFields.title).toBe('English title');
  112. }
  113. });
  114. it('retrieves German translations when querying in German', async () => {
  115. const { asset } = await adminClient.query(
  116. getAssetWithCustomFieldsDocument,
  117. { id: assetId },
  118. { languageCode: LanguageCode.de },
  119. );
  120. expect(asset).not.toBeNull();
  121. if (asset) {
  122. expect(asset.customFields.alt).toBe('German alt text');
  123. expect(asset.customFields.title).toBe('German title');
  124. }
  125. });
  126. it('falls back to default language when translation is not available', async () => {
  127. const { asset } = await adminClient.query(
  128. getAssetWithCustomFieldsDocument,
  129. { id: assetId },
  130. { languageCode: LanguageCode.zh },
  131. );
  132. expect(asset).not.toBeNull();
  133. if (asset) {
  134. // Should fall back to English (the default language)
  135. expect(asset.customFields.alt).toBe('English alt text');
  136. expect(asset.customFields.title).toBe('English title');
  137. }
  138. });
  139. });
  140. const createAssetsWithCustomFieldsDocument = graphql(`
  141. mutation CreateAssetsWithCustomFields($input: [CreateAssetInput!]!) {
  142. createAssets(input: $input) {
  143. ... on Asset {
  144. id
  145. name
  146. customFields {
  147. alt
  148. title
  149. }
  150. }
  151. ... on MimeTypeError {
  152. message
  153. fileName
  154. mimeType
  155. }
  156. }
  157. }
  158. `);
  159. const updateAssetWithCustomFieldsDocument = graphql(`
  160. mutation UpdateAssetWithCustomFields($input: UpdateAssetInput!) {
  161. updateAsset(input: $input) {
  162. id
  163. name
  164. customFields {
  165. alt
  166. title
  167. }
  168. }
  169. }
  170. `);
  171. const getAssetWithCustomFieldsDocument = graphql(`
  172. query GetAssetWithCustomFields($id: ID!) {
  173. asset(id: $id) {
  174. id
  175. name
  176. customFields {
  177. alt
  178. title
  179. }
  180. }
  181. }
  182. `);