asset.e2e-spec.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* tslint:disable:no-non-null-assertion */
  2. import { omit } from '@vendure/common/lib/omit';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import gql from 'graphql-tag';
  5. import path from 'path';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  8. import { ASSET_FRAGMENT } from './graphql/fragments';
  9. import {
  10. CreateAssets,
  11. DeleteAsset,
  12. DeletionResult,
  13. GetAsset,
  14. GetAssetList,
  15. GetProductWithVariants,
  16. SortOrder,
  17. UpdateAsset,
  18. } from './graphql/generated-e2e-admin-types';
  19. import {
  20. DELETE_ASSET,
  21. GET_ASSET_LIST,
  22. GET_PRODUCT_WITH_VARIANTS,
  23. UPDATE_ASSET,
  24. } from './graphql/shared-definitions';
  25. describe('Asset resolver', () => {
  26. const { server, adminClient } = createTestEnvironment(testConfig);
  27. let firstAssetId: string;
  28. let createdAssetId: string;
  29. beforeAll(async () => {
  30. await server.init({
  31. initialData,
  32. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  33. customerCount: 1,
  34. });
  35. await adminClient.asSuperAdmin();
  36. }, TEST_SETUP_TIMEOUT_MS);
  37. afterAll(async () => {
  38. await server.destroy();
  39. });
  40. it('assets', async () => {
  41. const { assets } = await adminClient.query<GetAssetList.Query, GetAssetList.Variables>(
  42. GET_ASSET_LIST,
  43. {
  44. options: {
  45. sort: {
  46. name: SortOrder.ASC,
  47. },
  48. },
  49. },
  50. );
  51. expect(assets.totalItems).toBe(4);
  52. expect(assets.items.map((a) => omit(a, ['id']))).toEqual([
  53. {
  54. fileSize: 1680,
  55. mimeType: 'image/jpeg',
  56. name: 'alexandru-acea-686569-unsplash.jpg',
  57. preview: 'test-url/test-assets/alexandru-acea-686569-unsplash__preview.jpg',
  58. source: 'test-url/test-assets/alexandru-acea-686569-unsplash.jpg',
  59. type: 'IMAGE',
  60. },
  61. {
  62. fileSize: 1680,
  63. mimeType: 'image/jpeg',
  64. name: 'derick-david-409858-unsplash.jpg',
  65. preview: 'test-url/test-assets/derick-david-409858-unsplash__preview.jpg',
  66. source: 'test-url/test-assets/derick-david-409858-unsplash.jpg',
  67. type: 'IMAGE',
  68. },
  69. {
  70. fileSize: 1680,
  71. mimeType: 'image/jpeg',
  72. name: 'florian-olivo-1166419-unsplash.jpg',
  73. preview: 'test-url/test-assets/florian-olivo-1166419-unsplash__preview.jpg',
  74. source: 'test-url/test-assets/florian-olivo-1166419-unsplash.jpg',
  75. type: 'IMAGE',
  76. },
  77. {
  78. fileSize: 1680,
  79. mimeType: 'image/jpeg',
  80. name: 'vincent-botta-736919-unsplash.jpg',
  81. preview: 'test-url/test-assets/vincent-botta-736919-unsplash__preview.jpg',
  82. source: 'test-url/test-assets/vincent-botta-736919-unsplash.jpg',
  83. type: 'IMAGE',
  84. },
  85. ]);
  86. firstAssetId = assets.items[0].id;
  87. });
  88. it('asset', async () => {
  89. const { asset } = await adminClient.query<GetAsset.Query, GetAsset.Variables>(GET_ASSET, {
  90. id: firstAssetId,
  91. });
  92. expect(asset).toEqual({
  93. fileSize: 1680,
  94. height: 48,
  95. id: firstAssetId,
  96. mimeType: 'image/jpeg',
  97. name: 'alexandru-acea-686569-unsplash.jpg',
  98. preview: 'test-url/test-assets/alexandru-acea-686569-unsplash__preview.jpg',
  99. source: 'test-url/test-assets/alexandru-acea-686569-unsplash.jpg',
  100. type: 'IMAGE',
  101. width: 48,
  102. });
  103. });
  104. it('createAssets', async () => {
  105. const filesToUpload = [
  106. path.join(__dirname, 'fixtures/assets/pps1.jpg'),
  107. path.join(__dirname, 'fixtures/assets/pps2.jpg'),
  108. ];
  109. const { createAssets }: CreateAssets.Mutation = await adminClient.fileUploadMutation({
  110. mutation: CREATE_ASSETS,
  111. filePaths: filesToUpload,
  112. mapVariables: (filePaths) => ({
  113. input: filePaths.map((p) => ({ file: null })),
  114. }),
  115. });
  116. expect(createAssets.map((a) => omit(a, ['id'])).sort((a, b) => (a.name < b.name ? -1 : 1))).toEqual([
  117. {
  118. fileSize: 1680,
  119. focalPoint: null,
  120. mimeType: 'image/jpeg',
  121. name: 'pps1.jpg',
  122. preview: 'test-url/test-assets/pps1__preview.jpg',
  123. source: 'test-url/test-assets/pps1.jpg',
  124. type: 'IMAGE',
  125. },
  126. {
  127. fileSize: 1680,
  128. focalPoint: null,
  129. mimeType: 'image/jpeg',
  130. name: 'pps2.jpg',
  131. preview: 'test-url/test-assets/pps2__preview.jpg',
  132. source: 'test-url/test-assets/pps2.jpg',
  133. type: 'IMAGE',
  134. },
  135. ]);
  136. createdAssetId = createAssets[0].id;
  137. });
  138. describe('updateAsset', () => {
  139. it('update name', async () => {
  140. const { updateAsset } = await adminClient.query<UpdateAsset.Mutation, UpdateAsset.Variables>(
  141. UPDATE_ASSET,
  142. {
  143. input: {
  144. id: firstAssetId,
  145. name: 'new name',
  146. },
  147. },
  148. );
  149. expect(updateAsset.name).toEqual('new name');
  150. });
  151. it('update focalPoint', async () => {
  152. const { updateAsset } = await adminClient.query<UpdateAsset.Mutation, UpdateAsset.Variables>(
  153. UPDATE_ASSET,
  154. {
  155. input: {
  156. id: firstAssetId,
  157. focalPoint: {
  158. x: 0.3,
  159. y: 0.9,
  160. },
  161. },
  162. },
  163. );
  164. expect(updateAsset.focalPoint).toEqual({
  165. x: 0.3,
  166. y: 0.9,
  167. });
  168. });
  169. it('unset focalPoint', async () => {
  170. const { updateAsset } = await adminClient.query<UpdateAsset.Mutation, UpdateAsset.Variables>(
  171. UPDATE_ASSET,
  172. {
  173. input: {
  174. id: firstAssetId,
  175. focalPoint: null,
  176. },
  177. },
  178. );
  179. expect(updateAsset.focalPoint).toEqual(null);
  180. });
  181. });
  182. describe('deleteAsset', () => {
  183. let firstProduct: GetProductWithVariants.Product;
  184. beforeAll(async () => {
  185. const { product } = await adminClient.query<
  186. GetProductWithVariants.Query,
  187. GetProductWithVariants.Variables
  188. >(GET_PRODUCT_WITH_VARIANTS, {
  189. id: 'T_1',
  190. });
  191. firstProduct = product!;
  192. });
  193. it('non-featured asset', async () => {
  194. const { deleteAsset } = await adminClient.query<DeleteAsset.Mutation, DeleteAsset.Variables>(
  195. DELETE_ASSET,
  196. {
  197. id: createdAssetId,
  198. },
  199. );
  200. expect(deleteAsset.result).toBe(DeletionResult.DELETED);
  201. const { asset } = await adminClient.query<GetAsset.Query, GetAsset.Variables>(GET_ASSET, {
  202. id: createdAssetId,
  203. });
  204. expect(asset).toBeNull();
  205. });
  206. it('featured asset not deleted', async () => {
  207. const { deleteAsset } = await adminClient.query<DeleteAsset.Mutation, DeleteAsset.Variables>(
  208. DELETE_ASSET,
  209. {
  210. id: firstProduct.featuredAsset!.id,
  211. },
  212. );
  213. expect(deleteAsset.result).toBe(DeletionResult.NOT_DELETED);
  214. expect(deleteAsset.message).toContain(`The selected Asset is featured by 1 Product`);
  215. const { asset } = await adminClient.query<GetAsset.Query, GetAsset.Variables>(GET_ASSET, {
  216. id: firstAssetId,
  217. });
  218. expect(asset).not.toBeNull();
  219. });
  220. it('featured asset force deleted', async () => {
  221. const { product: p1 } = await adminClient.query<
  222. GetProductWithVariants.Query,
  223. GetProductWithVariants.Variables
  224. >(GET_PRODUCT_WITH_VARIANTS, {
  225. id: firstProduct.id,
  226. });
  227. expect(p1!.assets.length).toEqual(1);
  228. const { deleteAsset } = await adminClient.query<DeleteAsset.Mutation, DeleteAsset.Variables>(
  229. DELETE_ASSET,
  230. {
  231. id: firstProduct.featuredAsset!.id,
  232. force: true,
  233. },
  234. );
  235. expect(deleteAsset.result).toBe(DeletionResult.DELETED);
  236. const { asset } = await adminClient.query<GetAsset.Query, GetAsset.Variables>(GET_ASSET, {
  237. id: firstAssetId,
  238. });
  239. expect(asset).not.toBeNull();
  240. const { product } = await adminClient.query<
  241. GetProductWithVariants.Query,
  242. GetProductWithVariants.Variables
  243. >(GET_PRODUCT_WITH_VARIANTS, {
  244. id: firstProduct.id,
  245. });
  246. expect(product!.featuredAsset).toBeNull();
  247. expect(product!.assets.length).toEqual(0);
  248. });
  249. });
  250. });
  251. export const GET_ASSET = gql`
  252. query GetAsset($id: ID!) {
  253. asset(id: $id) {
  254. ...Asset
  255. width
  256. height
  257. }
  258. }
  259. ${ASSET_FRAGMENT}
  260. `;
  261. export const CREATE_ASSETS = gql`
  262. mutation CreateAssets($input: [CreateAssetInput!]!) {
  263. createAssets(input: $input) {
  264. ...Asset
  265. focalPoint {
  266. x
  267. y
  268. }
  269. }
  270. }
  271. ${ASSET_FRAGMENT}
  272. `;