asset.e2e-spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { omit } from '@vendure/common/lib/omit';
  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 { ASSET_FRAGMENT } from './graphql/fragments';
  8. import {
  9. CreateAssets,
  10. GetAsset,
  11. GetAssetList,
  12. SortOrder,
  13. UpdateAsset,
  14. } from './graphql/generated-e2e-admin-types';
  15. import { GET_ASSET_LIST, UPDATE_ASSET } from './graphql/shared-definitions';
  16. describe('Asset resolver', () => {
  17. const { server, adminClient } = createTestEnvironment(testConfig);
  18. let firstAssetId: string;
  19. beforeAll(async () => {
  20. await server.init({
  21. initialData,
  22. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  23. customerCount: 1,
  24. });
  25. await adminClient.asSuperAdmin();
  26. }, TEST_SETUP_TIMEOUT_MS);
  27. afterAll(async () => {
  28. await server.destroy();
  29. });
  30. it('assets', async () => {
  31. const { assets } = await adminClient.query<GetAssetList.Query, GetAssetList.Variables>(
  32. GET_ASSET_LIST,
  33. {
  34. options: {
  35. sort: {
  36. name: SortOrder.ASC,
  37. },
  38. },
  39. },
  40. );
  41. expect(assets.totalItems).toBe(4);
  42. expect(assets.items.map(a => omit(a, ['id']))).toEqual([
  43. {
  44. fileSize: 1680,
  45. mimeType: 'image/jpeg',
  46. name: 'alexandru-acea-686569-unsplash.jpg',
  47. preview: 'test-url/test-assets/alexandru-acea-686569-unsplash__preview.jpg',
  48. source: 'test-url/test-assets/alexandru-acea-686569-unsplash.jpg',
  49. type: 'IMAGE',
  50. },
  51. {
  52. fileSize: 1680,
  53. mimeType: 'image/jpeg',
  54. name: 'derick-david-409858-unsplash.jpg',
  55. preview: 'test-url/test-assets/derick-david-409858-unsplash__preview.jpg',
  56. source: 'test-url/test-assets/derick-david-409858-unsplash.jpg',
  57. type: 'IMAGE',
  58. },
  59. {
  60. fileSize: 1680,
  61. mimeType: 'image/jpeg',
  62. name: 'florian-olivo-1166419-unsplash.jpg',
  63. preview: 'test-url/test-assets/florian-olivo-1166419-unsplash__preview.jpg',
  64. source: 'test-url/test-assets/florian-olivo-1166419-unsplash.jpg',
  65. type: 'IMAGE',
  66. },
  67. {
  68. fileSize: 1680,
  69. mimeType: 'image/jpeg',
  70. name: 'vincent-botta-736919-unsplash.jpg',
  71. preview: 'test-url/test-assets/vincent-botta-736919-unsplash__preview.jpg',
  72. source: 'test-url/test-assets/vincent-botta-736919-unsplash.jpg',
  73. type: 'IMAGE',
  74. },
  75. ]);
  76. firstAssetId = assets.items[0].id;
  77. });
  78. it('asset', async () => {
  79. const { asset } = await adminClient.query<GetAsset.Query, GetAsset.Variables>(GET_ASSET, {
  80. id: firstAssetId,
  81. });
  82. expect(asset).toEqual({
  83. fileSize: 1680,
  84. height: 48,
  85. id: firstAssetId,
  86. mimeType: 'image/jpeg',
  87. name: 'alexandru-acea-686569-unsplash.jpg',
  88. preview: 'test-url/test-assets/alexandru-acea-686569-unsplash__preview.jpg',
  89. source: 'test-url/test-assets/alexandru-acea-686569-unsplash.jpg',
  90. type: 'IMAGE',
  91. width: 48,
  92. });
  93. });
  94. it('createAssets', async () => {
  95. const filesToUpload = [
  96. path.join(__dirname, 'fixtures/assets/pps1.jpg'),
  97. path.join(__dirname, 'fixtures/assets/pps2.jpg'),
  98. ];
  99. const { createAssets }: CreateAssets.Mutation = await adminClient.fileUploadMutation({
  100. mutation: CREATE_ASSETS,
  101. filePaths: filesToUpload,
  102. mapVariables: filePaths => ({
  103. input: filePaths.map(p => ({ file: null })),
  104. }),
  105. });
  106. expect(createAssets.map(a => omit(a, ['id'])).sort((a, b) => (a.name < b.name ? -1 : 1))).toEqual([
  107. {
  108. fileSize: 1680,
  109. focalPoint: null,
  110. mimeType: 'image/jpeg',
  111. name: 'pps1.jpg',
  112. preview: 'test-url/test-assets/pps1__preview.jpg',
  113. source: 'test-url/test-assets/pps1.jpg',
  114. type: 'IMAGE',
  115. },
  116. {
  117. fileSize: 1680,
  118. focalPoint: null,
  119. mimeType: 'image/jpeg',
  120. name: 'pps2.jpg',
  121. preview: 'test-url/test-assets/pps2__preview.jpg',
  122. source: 'test-url/test-assets/pps2.jpg',
  123. type: 'IMAGE',
  124. },
  125. ]);
  126. });
  127. describe('updateAsset', () => {
  128. it('update name', async () => {
  129. const { updateAsset } = await adminClient.query<UpdateAsset.Mutation, UpdateAsset.Variables>(
  130. UPDATE_ASSET,
  131. {
  132. input: {
  133. id: firstAssetId,
  134. name: 'new name',
  135. },
  136. },
  137. );
  138. expect(updateAsset.name).toEqual('new name');
  139. });
  140. it('update focalPoint', async () => {
  141. const { updateAsset } = await adminClient.query<UpdateAsset.Mutation, UpdateAsset.Variables>(
  142. UPDATE_ASSET,
  143. {
  144. input: {
  145. id: firstAssetId,
  146. focalPoint: {
  147. x: 0.3,
  148. y: 0.9,
  149. },
  150. },
  151. },
  152. );
  153. expect(updateAsset.focalPoint).toEqual({
  154. x: 0.3,
  155. y: 0.9,
  156. });
  157. });
  158. it('unset focalPoint', async () => {
  159. const { updateAsset } = await adminClient.query<UpdateAsset.Mutation, UpdateAsset.Variables>(
  160. UPDATE_ASSET,
  161. {
  162. input: {
  163. id: firstAssetId,
  164. focalPoint: null,
  165. },
  166. },
  167. );
  168. expect(updateAsset.focalPoint).toEqual(null);
  169. });
  170. });
  171. });
  172. export const GET_ASSET = gql`
  173. query GetAsset($id: ID!) {
  174. asset(id: $id) {
  175. ...Asset
  176. width
  177. height
  178. }
  179. }
  180. ${ASSET_FRAGMENT}
  181. `;
  182. export const CREATE_ASSETS = gql`
  183. mutation CreateAssets($input: [CreateAssetInput!]!) {
  184. createAssets(input: $input) {
  185. ...Asset
  186. focalPoint {
  187. x
  188. y
  189. }
  190. }
  191. }
  192. ${ASSET_FRAGMENT}
  193. `;