product.e2e-spec.ts 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. import { omit } from '@vendure/common/lib/omit';
  2. import { pick } from '@vendure/common/lib/pick';
  3. import { notNullOrUndefined } from '@vendure/common/lib/shared-utils';
  4. import { createTestEnvironment } from '@vendure/testing';
  5. import gql from 'graphql-tag';
  6. import path from 'path';
  7. import { initialData } from '../../../e2e-common/e2e-initial-data';
  8. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  9. import {
  10. AddOptionGroupToProduct,
  11. CreateProduct,
  12. CreateProductVariants,
  13. DeleteProduct,
  14. DeleteProductVariant,
  15. DeletionResult,
  16. GetAssetList,
  17. GetOptionGroup,
  18. GetProductList,
  19. GetProductSimple,
  20. GetProductWithVariants,
  21. LanguageCode,
  22. ProductWithVariants,
  23. RemoveOptionGroupFromProduct,
  24. SortOrder,
  25. UpdateProduct,
  26. UpdateProductVariants,
  27. } from './graphql/generated-e2e-admin-types';
  28. import {
  29. CREATE_PRODUCT,
  30. CREATE_PRODUCT_VARIANTS,
  31. DELETE_PRODUCT,
  32. DELETE_PRODUCT_VARIANT,
  33. GET_ASSET_LIST,
  34. GET_PRODUCT_LIST,
  35. GET_PRODUCT_SIMPLE,
  36. GET_PRODUCT_WITH_VARIANTS,
  37. UPDATE_PRODUCT,
  38. UPDATE_PRODUCT_VARIANTS,
  39. } from './graphql/shared-definitions';
  40. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  41. // tslint:disable:no-non-null-assertion
  42. describe('Product resolver', () => {
  43. const { server, adminClient, shopClient } = createTestEnvironment(testConfig);
  44. beforeAll(async () => {
  45. await server.init({
  46. dataDir: path.join(__dirname, '__data__'),
  47. initialData,
  48. customerCount: 1,
  49. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  50. });
  51. await adminClient.asSuperAdmin();
  52. }, TEST_SETUP_TIMEOUT_MS);
  53. afterAll(async () => {
  54. await server.destroy();
  55. });
  56. describe('products list query', () => {
  57. it('returns all products when no options passed', async () => {
  58. const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
  59. GET_PRODUCT_LIST,
  60. {},
  61. );
  62. expect(result.products.items.length).toBe(20);
  63. expect(result.products.totalItems).toBe(20);
  64. });
  65. it('limits result set with skip & take', async () => {
  66. const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
  67. GET_PRODUCT_LIST,
  68. {
  69. options: {
  70. skip: 0,
  71. take: 3,
  72. },
  73. },
  74. );
  75. expect(result.products.items.length).toBe(3);
  76. expect(result.products.totalItems).toBe(20);
  77. });
  78. it('filters by name admin', async () => {
  79. const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
  80. GET_PRODUCT_LIST,
  81. {
  82. options: {
  83. filter: {
  84. name: {
  85. contains: 'skateboard',
  86. },
  87. },
  88. },
  89. },
  90. );
  91. expect(result.products.items.length).toBe(1);
  92. expect(result.products.items[0].name).toBe('Cruiser Skateboard');
  93. });
  94. it('filters multiple admin', async () => {
  95. const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
  96. GET_PRODUCT_LIST,
  97. {
  98. options: {
  99. filter: {
  100. name: {
  101. contains: 'camera',
  102. },
  103. slug: {
  104. contains: 'tent',
  105. },
  106. },
  107. },
  108. },
  109. );
  110. expect(result.products.items.length).toBe(0);
  111. });
  112. it('sorts by name admin', async () => {
  113. const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
  114. GET_PRODUCT_LIST,
  115. {
  116. options: {
  117. sort: {
  118. name: SortOrder.ASC,
  119. },
  120. },
  121. },
  122. );
  123. expect(result.products.items.map(p => p.name)).toEqual([
  124. 'Bonsai Tree',
  125. 'Boxing Gloves',
  126. 'Camera Lens',
  127. 'Clacky Keyboard',
  128. 'Cruiser Skateboard',
  129. 'Curvy Monitor',
  130. 'Football',
  131. 'Gaming PC',
  132. 'Hard Drive',
  133. 'Instant Camera',
  134. 'Laptop',
  135. 'Orchid',
  136. 'Road Bike',
  137. 'Running Shoe',
  138. 'SLR Camera',
  139. 'Skipping Rope',
  140. 'Spiky Cactus',
  141. 'Tent',
  142. 'Tripod',
  143. 'USB Cable',
  144. ]);
  145. });
  146. it('filters by name shop', async () => {
  147. const result = await shopClient.query<GetProductList.Query, GetProductList.Variables>(
  148. GET_PRODUCT_LIST,
  149. {
  150. options: {
  151. filter: {
  152. name: {
  153. contains: 'skateboard',
  154. },
  155. },
  156. },
  157. },
  158. );
  159. expect(result.products.items.length).toBe(1);
  160. expect(result.products.items[0].name).toBe('Cruiser Skateboard');
  161. });
  162. it('sorts by name shop', async () => {
  163. const result = await shopClient.query<GetProductList.Query, GetProductList.Variables>(
  164. GET_PRODUCT_LIST,
  165. {
  166. options: {
  167. sort: {
  168. name: SortOrder.ASC,
  169. },
  170. },
  171. },
  172. );
  173. expect(result.products.items.map(p => p.name)).toEqual([
  174. 'Bonsai Tree',
  175. 'Boxing Gloves',
  176. 'Camera Lens',
  177. 'Clacky Keyboard',
  178. 'Cruiser Skateboard',
  179. 'Curvy Monitor',
  180. 'Football',
  181. 'Gaming PC',
  182. 'Hard Drive',
  183. 'Instant Camera',
  184. 'Laptop',
  185. 'Orchid',
  186. 'Road Bike',
  187. 'Running Shoe',
  188. 'SLR Camera',
  189. 'Skipping Rope',
  190. 'Spiky Cactus',
  191. 'Tent',
  192. 'Tripod',
  193. 'USB Cable',
  194. ]);
  195. });
  196. });
  197. describe('product query', () => {
  198. it('by id', async () => {
  199. const { product } = await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
  200. GET_PRODUCT_SIMPLE,
  201. { id: 'T_2' },
  202. );
  203. if (!product) {
  204. fail('Product not found');
  205. return;
  206. }
  207. expect(product.id).toBe('T_2');
  208. });
  209. it('by slug', async () => {
  210. const { product } = await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
  211. GET_PRODUCT_SIMPLE,
  212. { slug: 'curvy-monitor' },
  213. );
  214. if (!product) {
  215. fail('Product not found');
  216. return;
  217. }
  218. expect(product.slug).toBe('curvy-monitor');
  219. });
  220. it(
  221. 'throws if neither id nor slug provided',
  222. assertThrowsWithMessage(async () => {
  223. await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
  224. GET_PRODUCT_SIMPLE,
  225. {},
  226. );
  227. }, 'Either the product id or slug must be provided'),
  228. );
  229. it(
  230. 'throws if id and slug do not refer to the same Product',
  231. assertThrowsWithMessage(async () => {
  232. await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
  233. GET_PRODUCT_SIMPLE,
  234. {
  235. id: 'T_2',
  236. slug: 'laptop',
  237. },
  238. );
  239. }, 'The provided id and slug refer to different Products'),
  240. );
  241. it('returns expected properties', async () => {
  242. const { product } = await adminClient.query<
  243. GetProductWithVariants.Query,
  244. GetProductWithVariants.Variables
  245. >(GET_PRODUCT_WITH_VARIANTS, {
  246. id: 'T_2',
  247. });
  248. if (!product) {
  249. fail('Product not found');
  250. return;
  251. }
  252. expect(omit(product, ['variants'])).toMatchSnapshot();
  253. expect(product.variants.length).toBe(2);
  254. });
  255. it('ProductVariant price properties are correct', async () => {
  256. const result = await adminClient.query<
  257. GetProductWithVariants.Query,
  258. GetProductWithVariants.Variables
  259. >(GET_PRODUCT_WITH_VARIANTS, {
  260. id: 'T_2',
  261. });
  262. if (!result.product) {
  263. fail('Product not found');
  264. return;
  265. }
  266. expect(result.product.variants[0].price).toBe(14374);
  267. expect(result.product.variants[0].taxCategory).toEqual({
  268. id: 'T_1',
  269. name: 'Standard Tax',
  270. });
  271. });
  272. it('returns null when id not found', async () => {
  273. const result = await adminClient.query<
  274. GetProductWithVariants.Query,
  275. GetProductWithVariants.Variables
  276. >(GET_PRODUCT_WITH_VARIANTS, {
  277. id: 'bad_id',
  278. });
  279. expect(result.product).toBeNull();
  280. });
  281. });
  282. describe('product mutation', () => {
  283. let newProduct: ProductWithVariants.Fragment;
  284. let newProductWithAssets: ProductWithVariants.Fragment;
  285. it('createProduct creates a new Product', async () => {
  286. const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
  287. CREATE_PRODUCT,
  288. {
  289. input: {
  290. translations: [
  291. {
  292. languageCode: LanguageCode.en,
  293. name: 'en Baked Potato',
  294. slug: 'en Baked Potato',
  295. description: 'A baked potato',
  296. },
  297. {
  298. languageCode: LanguageCode.de,
  299. name: 'de Baked Potato',
  300. slug: 'de-baked-potato',
  301. description: 'Eine baked Erdapfel',
  302. },
  303. ],
  304. },
  305. },
  306. );
  307. expect(result.createProduct).toMatchSnapshot();
  308. newProduct = result.createProduct;
  309. });
  310. it('createProduct creates a new Product with assets', async () => {
  311. const assetsResult = await adminClient.query<GetAssetList.Query, GetAssetList.Variables>(
  312. GET_ASSET_LIST,
  313. );
  314. const assetIds = assetsResult.assets.items.slice(0, 2).map(a => a.id);
  315. const featuredAssetId = assetsResult.assets.items[0].id;
  316. const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
  317. CREATE_PRODUCT,
  318. {
  319. input: {
  320. assetIds,
  321. featuredAssetId,
  322. translations: [
  323. {
  324. languageCode: LanguageCode.en,
  325. name: 'en Has Assets',
  326. slug: 'en-has-assets',
  327. description: 'A product with assets',
  328. },
  329. ],
  330. },
  331. },
  332. );
  333. expect(result.createProduct.assets.map(a => a.id)).toEqual(assetIds);
  334. expect(result.createProduct.featuredAsset!.id).toBe(featuredAssetId);
  335. newProductWithAssets = result.createProduct;
  336. });
  337. it('updateProduct updates a Product', async () => {
  338. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  339. UPDATE_PRODUCT,
  340. {
  341. input: {
  342. id: newProduct.id,
  343. translations: [
  344. {
  345. languageCode: LanguageCode.en,
  346. name: 'en Mashed Potato',
  347. slug: 'en-mashed-potato',
  348. description: 'A blob of mashed potato',
  349. },
  350. {
  351. languageCode: LanguageCode.de,
  352. name: 'de Mashed Potato',
  353. slug: 'de-mashed-potato',
  354. description: 'Eine blob von gemashed Erdapfel',
  355. },
  356. ],
  357. },
  358. },
  359. );
  360. expect(result.updateProduct).toMatchSnapshot();
  361. });
  362. it('slug is normalized to be url-safe', async () => {
  363. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  364. UPDATE_PRODUCT,
  365. {
  366. input: {
  367. id: newProduct.id,
  368. translations: [
  369. {
  370. languageCode: LanguageCode.en,
  371. name: 'en Mashed Potato',
  372. slug: 'A (very) nice potato!!',
  373. description: 'A blob of mashed potato',
  374. },
  375. ],
  376. },
  377. },
  378. );
  379. expect(result.updateProduct.slug).toBe('a-very-nice-potato');
  380. });
  381. it('create with duplicate slug is renamed to be unique', async () => {
  382. const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
  383. CREATE_PRODUCT,
  384. {
  385. input: {
  386. translations: [
  387. {
  388. languageCode: LanguageCode.en,
  389. name: 'Another baked potato',
  390. slug: 'a-very-nice-potato',
  391. description: 'Another baked potato but a bit different',
  392. },
  393. ],
  394. },
  395. },
  396. );
  397. expect(result.createProduct.slug).toBe('a-very-nice-potato-2');
  398. });
  399. it('update with duplicate slug is renamed to be unique', async () => {
  400. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  401. UPDATE_PRODUCT,
  402. {
  403. input: {
  404. id: newProduct.id,
  405. translations: [
  406. {
  407. languageCode: LanguageCode.en,
  408. name: 'Yet another baked potato',
  409. slug: 'a-very-nice-potato-2',
  410. description: 'Possibly the final baked potato',
  411. },
  412. ],
  413. },
  414. },
  415. );
  416. expect(result.updateProduct.slug).toBe('a-very-nice-potato-3');
  417. });
  418. it('slug duplicate check does not include self', async () => {
  419. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  420. UPDATE_PRODUCT,
  421. {
  422. input: {
  423. id: newProduct.id,
  424. translations: [
  425. {
  426. languageCode: LanguageCode.en,
  427. slug: 'a-very-nice-potato-3',
  428. },
  429. ],
  430. },
  431. },
  432. );
  433. expect(result.updateProduct.slug).toBe('a-very-nice-potato-3');
  434. });
  435. it('updateProduct accepts partial input', async () => {
  436. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  437. UPDATE_PRODUCT,
  438. {
  439. input: {
  440. id: newProduct.id,
  441. translations: [
  442. {
  443. languageCode: LanguageCode.en,
  444. name: 'en Very Mashed Potato',
  445. },
  446. ],
  447. },
  448. },
  449. );
  450. expect(result.updateProduct.translations.length).toBe(2);
  451. expect(result.updateProduct.translations[0].name).toBe('de Mashed Potato');
  452. expect(result.updateProduct.translations[1].name).toBe('en Very Mashed Potato');
  453. expect(result.updateProduct.translations[1].description).toBe('Possibly the final baked potato');
  454. });
  455. it('updateProduct adds Assets to a product and sets featured asset', async () => {
  456. const assetsResult = await adminClient.query<GetAssetList.Query, GetAssetList.Variables>(
  457. GET_ASSET_LIST,
  458. );
  459. const assetIds = assetsResult.assets.items.map(a => a.id);
  460. const featuredAssetId = assetsResult.assets.items[2].id;
  461. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  462. UPDATE_PRODUCT,
  463. {
  464. input: {
  465. id: newProduct.id,
  466. assetIds,
  467. featuredAssetId,
  468. },
  469. },
  470. );
  471. expect(result.updateProduct.assets.map(a => a.id)).toEqual(assetIds);
  472. expect(result.updateProduct.featuredAsset!.id).toBe(featuredAssetId);
  473. });
  474. it('updateProduct sets a featured asset', async () => {
  475. const productResult = await adminClient.query<
  476. GetProductWithVariants.Query,
  477. GetProductWithVariants.Variables
  478. >(GET_PRODUCT_WITH_VARIANTS, {
  479. id: newProduct.id,
  480. });
  481. const assets = productResult.product!.assets;
  482. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  483. UPDATE_PRODUCT,
  484. {
  485. input: {
  486. id: newProduct.id,
  487. featuredAssetId: assets[0].id,
  488. },
  489. },
  490. );
  491. expect(result.updateProduct.featuredAsset!.id).toBe(assets[0].id);
  492. });
  493. it('updateProduct updates assets', async () => {
  494. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  495. UPDATE_PRODUCT,
  496. {
  497. input: {
  498. id: newProduct.id,
  499. featuredAssetId: 'T_1',
  500. assetIds: ['T_1', 'T_2'],
  501. },
  502. },
  503. );
  504. expect(result.updateProduct.assets.map(a => a.id)).toEqual(['T_1', 'T_2']);
  505. });
  506. it('updateProduct updates FacetValues', async () => {
  507. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  508. UPDATE_PRODUCT,
  509. {
  510. input: {
  511. id: newProduct.id,
  512. facetValueIds: ['T_1'],
  513. },
  514. },
  515. );
  516. expect(result.updateProduct.facetValues.length).toEqual(1);
  517. });
  518. it(
  519. 'updateProduct errors with an invalid productId',
  520. assertThrowsWithMessage(
  521. () =>
  522. adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  523. input: {
  524. id: '999',
  525. translations: [
  526. {
  527. languageCode: LanguageCode.en,
  528. name: 'en Mashed Potato',
  529. slug: 'en-mashed-potato',
  530. description: 'A blob of mashed potato',
  531. },
  532. {
  533. languageCode: LanguageCode.de,
  534. name: 'de Mashed Potato',
  535. slug: 'de-mashed-potato',
  536. description: 'Eine blob von gemashed Erdapfel',
  537. },
  538. ],
  539. },
  540. }),
  541. `No Product with the id '999' could be found`,
  542. ),
  543. );
  544. it('addOptionGroupToProduct adds an option group', async () => {
  545. const result = await adminClient.query<
  546. AddOptionGroupToProduct.Mutation,
  547. AddOptionGroupToProduct.Variables
  548. >(ADD_OPTION_GROUP_TO_PRODUCT, {
  549. optionGroupId: 'T_2',
  550. productId: newProduct.id,
  551. });
  552. expect(result.addOptionGroupToProduct.optionGroups.length).toBe(1);
  553. expect(result.addOptionGroupToProduct.optionGroups[0].id).toBe('T_2');
  554. });
  555. it(
  556. 'addOptionGroupToProduct errors with an invalid productId',
  557. assertThrowsWithMessage(
  558. () =>
  559. adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
  560. ADD_OPTION_GROUP_TO_PRODUCT,
  561. {
  562. optionGroupId: 'T_1',
  563. productId: '999',
  564. },
  565. ),
  566. `No Product with the id '999' could be found`,
  567. ),
  568. );
  569. it(
  570. 'addOptionGroupToProduct errors with an invalid optionGroupId',
  571. assertThrowsWithMessage(
  572. () =>
  573. adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
  574. ADD_OPTION_GROUP_TO_PRODUCT,
  575. {
  576. optionGroupId: '999',
  577. productId: newProduct.id,
  578. },
  579. ),
  580. `No ProductOptionGroup with the id '999' could be found`,
  581. ),
  582. );
  583. it('removeOptionGroupFromProduct removes an option group', async () => {
  584. const { addOptionGroupToProduct } = await adminClient.query<
  585. AddOptionGroupToProduct.Mutation,
  586. AddOptionGroupToProduct.Variables
  587. >(ADD_OPTION_GROUP_TO_PRODUCT, {
  588. optionGroupId: 'T_1',
  589. productId: newProductWithAssets.id,
  590. });
  591. expect(addOptionGroupToProduct.optionGroups.length).toBe(1);
  592. const result = await adminClient.query<
  593. RemoveOptionGroupFromProduct.Mutation,
  594. RemoveOptionGroupFromProduct.Variables
  595. >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
  596. optionGroupId: 'T_1',
  597. productId: newProductWithAssets.id,
  598. });
  599. expect(result.removeOptionGroupFromProduct.id).toBe(newProductWithAssets.id);
  600. expect(result.removeOptionGroupFromProduct.optionGroups.length).toBe(0);
  601. });
  602. it(
  603. 'removeOptionGroupFromProduct errors if the optionGroup is being used by variants',
  604. assertThrowsWithMessage(
  605. () =>
  606. adminClient.query<
  607. RemoveOptionGroupFromProduct.Mutation,
  608. RemoveOptionGroupFromProduct.Variables
  609. >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
  610. optionGroupId: 'T_3',
  611. productId: 'T_2',
  612. }),
  613. `Cannot remove ProductOptionGroup "curvy-monitor-monitor-size" as it is used by 2 ProductVariants`,
  614. ),
  615. );
  616. it(
  617. 'removeOptionGroupFromProduct errors with an invalid productId',
  618. assertThrowsWithMessage(
  619. () =>
  620. adminClient.query<
  621. RemoveOptionGroupFromProduct.Mutation,
  622. RemoveOptionGroupFromProduct.Variables
  623. >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
  624. optionGroupId: '1',
  625. productId: '999',
  626. }),
  627. `No Product with the id '999' could be found`,
  628. ),
  629. );
  630. it(
  631. 'removeOptionGroupFromProduct errors with an invalid optionGroupId',
  632. assertThrowsWithMessage(
  633. () =>
  634. adminClient.query<
  635. RemoveOptionGroupFromProduct.Mutation,
  636. RemoveOptionGroupFromProduct.Variables
  637. >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
  638. optionGroupId: '999',
  639. productId: newProduct.id,
  640. }),
  641. `No ProductOptionGroup with the id '999' could be found`,
  642. ),
  643. );
  644. describe('variants', () => {
  645. let variants: CreateProductVariants.CreateProductVariants[];
  646. let optionGroup2: GetOptionGroup.ProductOptionGroup;
  647. let optionGroup3: GetOptionGroup.ProductOptionGroup;
  648. beforeAll(async () => {
  649. await adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
  650. ADD_OPTION_GROUP_TO_PRODUCT,
  651. {
  652. optionGroupId: 'T_3',
  653. productId: newProduct.id,
  654. },
  655. );
  656. const result1 = await adminClient.query<GetOptionGroup.Query, GetOptionGroup.Variables>(
  657. GET_OPTION_GROUP,
  658. { id: 'T_2' },
  659. );
  660. const result2 = await adminClient.query<GetOptionGroup.Query, GetOptionGroup.Variables>(
  661. GET_OPTION_GROUP,
  662. { id: 'T_3' },
  663. );
  664. optionGroup2 = result1.productOptionGroup!;
  665. optionGroup3 = result2.productOptionGroup!;
  666. });
  667. it(
  668. 'createProductVariants throws if optionIds not compatible with product',
  669. assertThrowsWithMessage(async () => {
  670. await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
  671. CREATE_PRODUCT_VARIANTS,
  672. {
  673. input: [
  674. {
  675. productId: newProduct.id,
  676. sku: 'PV1',
  677. optionIds: [],
  678. translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
  679. },
  680. ],
  681. },
  682. );
  683. }, 'ProductVariant optionIds must include one optionId from each of the groups: curvy-monitor-monitor-size, laptop-ram'),
  684. );
  685. it(
  686. 'createProductVariants throws if optionIds are duplicated',
  687. assertThrowsWithMessage(async () => {
  688. await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
  689. CREATE_PRODUCT_VARIANTS,
  690. {
  691. input: [
  692. {
  693. productId: newProduct.id,
  694. sku: 'PV1',
  695. optionIds: [optionGroup2.options[0].id, optionGroup2.options[1].id],
  696. translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
  697. },
  698. ],
  699. },
  700. );
  701. }, 'ProductVariant optionIds must include one optionId from each of the groups: curvy-monitor-monitor-size, laptop-ram'),
  702. );
  703. it('createProductVariants works', async () => {
  704. const { createProductVariants } = await adminClient.query<
  705. CreateProductVariants.Mutation,
  706. CreateProductVariants.Variables
  707. >(CREATE_PRODUCT_VARIANTS, {
  708. input: [
  709. {
  710. productId: newProduct.id,
  711. sku: 'PV1',
  712. optionIds: [optionGroup2.options[0].id, optionGroup3.options[0].id],
  713. translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
  714. },
  715. ],
  716. });
  717. expect(createProductVariants[0]!.name).toBe('Variant 1');
  718. expect(createProductVariants[0]!.options.map(pick(['id']))).toEqual([
  719. { id: optionGroup2.options[0].id },
  720. { id: optionGroup3.options[0].id },
  721. ]);
  722. });
  723. it('createProductVariants adds multiple variants at once', async () => {
  724. const { createProductVariants } = await adminClient.query<
  725. CreateProductVariants.Mutation,
  726. CreateProductVariants.Variables
  727. >(CREATE_PRODUCT_VARIANTS, {
  728. input: [
  729. {
  730. productId: newProduct.id,
  731. sku: 'PV2',
  732. optionIds: [optionGroup2.options[1].id, optionGroup3.options[0].id],
  733. translations: [{ languageCode: LanguageCode.en, name: 'Variant 2' }],
  734. },
  735. {
  736. productId: newProduct.id,
  737. sku: 'PV3',
  738. optionIds: [optionGroup2.options[1].id, optionGroup3.options[1].id],
  739. translations: [{ languageCode: LanguageCode.en, name: 'Variant 3' }],
  740. },
  741. ],
  742. });
  743. expect(createProductVariants[0]!.name).toBe('Variant 2');
  744. expect(createProductVariants[1]!.name).toBe('Variant 3');
  745. expect(createProductVariants[0]!.options.map(pick(['id']))).toEqual([
  746. { id: optionGroup2.options[1].id },
  747. { id: optionGroup3.options[0].id },
  748. ]);
  749. expect(createProductVariants[1]!.options.map(pick(['id']))).toEqual([
  750. { id: optionGroup2.options[1].id },
  751. { id: optionGroup3.options[1].id },
  752. ]);
  753. variants = createProductVariants.filter(notNullOrUndefined);
  754. });
  755. it(
  756. 'createProductVariants throws if options combination already exists',
  757. assertThrowsWithMessage(async () => {
  758. await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
  759. CREATE_PRODUCT_VARIANTS,
  760. {
  761. input: [
  762. {
  763. productId: newProduct.id,
  764. sku: 'PV2',
  765. optionIds: [optionGroup2.options[0].id, optionGroup3.options[0].id],
  766. translations: [{ languageCode: LanguageCode.en, name: 'Variant 2' }],
  767. },
  768. ],
  769. },
  770. );
  771. }, 'A ProductVariant already exists with the options: 16gb, 24-inch'),
  772. );
  773. it('updateProductVariants updates variants', async () => {
  774. const firstVariant = variants[0];
  775. const { updateProductVariants } = await adminClient.query<
  776. UpdateProductVariants.Mutation,
  777. UpdateProductVariants.Variables
  778. >(UPDATE_PRODUCT_VARIANTS, {
  779. input: [
  780. {
  781. id: firstVariant.id,
  782. translations: firstVariant.translations,
  783. sku: 'ABC',
  784. price: 432,
  785. },
  786. ],
  787. });
  788. const updatedVariant = updateProductVariants[0];
  789. if (!updatedVariant) {
  790. fail('no updated variant returned.');
  791. return;
  792. }
  793. expect(updatedVariant.sku).toBe('ABC');
  794. expect(updatedVariant.price).toBe(432);
  795. });
  796. it('updateProductVariants updates assets', async () => {
  797. const firstVariant = variants[0];
  798. const result = await adminClient.query<
  799. UpdateProductVariants.Mutation,
  800. UpdateProductVariants.Variables
  801. >(UPDATE_PRODUCT_VARIANTS, {
  802. input: [
  803. {
  804. id: firstVariant.id,
  805. assetIds: ['T_1', 'T_2'],
  806. featuredAssetId: 'T_2',
  807. },
  808. ],
  809. });
  810. const updatedVariant = result.updateProductVariants[0];
  811. if (!updatedVariant) {
  812. fail('no updated variant returned.');
  813. return;
  814. }
  815. expect(updatedVariant.assets.map(a => a.id)).toEqual(['T_1', 'T_2']);
  816. expect(updatedVariant.featuredAsset!.id).toBe('T_2');
  817. });
  818. it('updateProductVariants updates assets again', async () => {
  819. const firstVariant = variants[0];
  820. const result = await adminClient.query<
  821. UpdateProductVariants.Mutation,
  822. UpdateProductVariants.Variables
  823. >(UPDATE_PRODUCT_VARIANTS, {
  824. input: [
  825. {
  826. id: firstVariant.id,
  827. assetIds: ['T_4', 'T_3'],
  828. featuredAssetId: 'T_4',
  829. },
  830. ],
  831. });
  832. const updatedVariant = result.updateProductVariants[0];
  833. if (!updatedVariant) {
  834. fail('no updated variant returned.');
  835. return;
  836. }
  837. expect(updatedVariant.assets.map(a => a.id)).toEqual(['T_4', 'T_3']);
  838. expect(updatedVariant.featuredAsset!.id).toBe('T_4');
  839. });
  840. it('updateProductVariants updates taxCategory and priceBeforeTax', async () => {
  841. const firstVariant = variants[0];
  842. const result = await adminClient.query<
  843. UpdateProductVariants.Mutation,
  844. UpdateProductVariants.Variables
  845. >(UPDATE_PRODUCT_VARIANTS, {
  846. input: [
  847. {
  848. id: firstVariant.id,
  849. price: 105,
  850. taxCategoryId: 'T_2',
  851. },
  852. ],
  853. });
  854. const updatedVariant = result.updateProductVariants[0];
  855. if (!updatedVariant) {
  856. fail('no updated variant returned.');
  857. return;
  858. }
  859. expect(updatedVariant.price).toBe(105);
  860. expect(updatedVariant.taxCategory.id).toBe('T_2');
  861. });
  862. it('updateProductVariants updates facetValues', async () => {
  863. const firstVariant = variants[0];
  864. const result = await adminClient.query<
  865. UpdateProductVariants.Mutation,
  866. UpdateProductVariants.Variables
  867. >(UPDATE_PRODUCT_VARIANTS, {
  868. input: [
  869. {
  870. id: firstVariant.id,
  871. facetValueIds: ['T_1'],
  872. },
  873. ],
  874. });
  875. const updatedVariant = result.updateProductVariants[0];
  876. if (!updatedVariant) {
  877. fail('no updated variant returned.');
  878. return;
  879. }
  880. expect(updatedVariant.facetValues.length).toBe(1);
  881. expect(updatedVariant.facetValues[0].id).toBe('T_1');
  882. });
  883. it(
  884. 'updateProductVariants throws with an invalid variant id',
  885. assertThrowsWithMessage(
  886. () =>
  887. adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  888. UPDATE_PRODUCT_VARIANTS,
  889. {
  890. input: [
  891. {
  892. id: 'T_999',
  893. translations: variants[0].translations,
  894. sku: 'ABC',
  895. price: 432,
  896. },
  897. ],
  898. },
  899. ),
  900. `No ProductVariant with the id '999' could be found`,
  901. ),
  902. );
  903. it('deleteProductVariant', async () => {
  904. const result1 = await adminClient.query<
  905. GetProductWithVariants.Query,
  906. GetProductWithVariants.Variables
  907. >(GET_PRODUCT_WITH_VARIANTS, {
  908. id: newProduct.id,
  909. });
  910. expect(result1.product!.variants.map(v => v.id)).toEqual(['T_35', 'T_36', 'T_37']);
  911. const { deleteProductVariant } = await adminClient.query<
  912. DeleteProductVariant.Mutation,
  913. DeleteProductVariant.Variables
  914. >(DELETE_PRODUCT_VARIANT, {
  915. id: result1.product!.variants[0].id,
  916. });
  917. expect(deleteProductVariant.result).toBe(DeletionResult.DELETED);
  918. const result2 = await adminClient.query<
  919. GetProductWithVariants.Query,
  920. GetProductWithVariants.Variables
  921. >(GET_PRODUCT_WITH_VARIANTS, {
  922. id: newProduct.id,
  923. });
  924. expect(result2.product!.variants.map(v => v.id)).toEqual(['T_36', 'T_37']);
  925. });
  926. });
  927. });
  928. describe('deletion', () => {
  929. let allProducts: GetProductList.Items[];
  930. let productToDelete: GetProductList.Items;
  931. beforeAll(async () => {
  932. const result = await adminClient.query<GetProductList.Query>(GET_PRODUCT_LIST);
  933. allProducts = result.products.items;
  934. });
  935. it('deletes a product', async () => {
  936. productToDelete = allProducts[0];
  937. const result = await adminClient.query<DeleteProduct.Mutation, DeleteProduct.Variables>(
  938. DELETE_PRODUCT,
  939. { id: productToDelete.id },
  940. );
  941. expect(result.deleteProduct).toEqual({ result: DeletionResult.DELETED });
  942. });
  943. it('cannot get a deleted product', async () => {
  944. const result = await adminClient.query<
  945. GetProductWithVariants.Query,
  946. GetProductWithVariants.Variables
  947. >(GET_PRODUCT_WITH_VARIANTS, {
  948. id: productToDelete.id,
  949. });
  950. expect(result.product).toBe(null);
  951. });
  952. it('deleted product omitted from list', async () => {
  953. const result = await adminClient.query<GetProductList.Query>(GET_PRODUCT_LIST);
  954. expect(result.products.items.length).toBe(allProducts.length - 1);
  955. expect(result.products.items.map(c => c.id).includes(productToDelete.id)).toBe(false);
  956. });
  957. it(
  958. 'updateProduct throws for deleted product',
  959. assertThrowsWithMessage(
  960. () =>
  961. adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  962. input: {
  963. id: productToDelete.id,
  964. facetValueIds: ['T_1'],
  965. },
  966. }),
  967. `No Product with the id '1' could be found`,
  968. ),
  969. );
  970. it(
  971. 'addOptionGroupToProduct throws for deleted product',
  972. assertThrowsWithMessage(
  973. () =>
  974. adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
  975. ADD_OPTION_GROUP_TO_PRODUCT,
  976. {
  977. optionGroupId: 'T_1',
  978. productId: productToDelete.id,
  979. },
  980. ),
  981. `No Product with the id '1' could be found`,
  982. ),
  983. );
  984. it(
  985. 'removeOptionGroupToProduct throws for deleted product',
  986. assertThrowsWithMessage(
  987. () =>
  988. adminClient.query<
  989. RemoveOptionGroupFromProduct.Mutation,
  990. RemoveOptionGroupFromProduct.Variables
  991. >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
  992. optionGroupId: 'T_1',
  993. productId: productToDelete.id,
  994. }),
  995. `No Product with the id '1' could be found`,
  996. ),
  997. );
  998. });
  999. });
  1000. export const ADD_OPTION_GROUP_TO_PRODUCT = gql`
  1001. mutation AddOptionGroupToProduct($productId: ID!, $optionGroupId: ID!) {
  1002. addOptionGroupToProduct(productId: $productId, optionGroupId: $optionGroupId) {
  1003. id
  1004. optionGroups {
  1005. id
  1006. code
  1007. options {
  1008. id
  1009. code
  1010. }
  1011. }
  1012. }
  1013. }
  1014. `;
  1015. export const REMOVE_OPTION_GROUP_FROM_PRODUCT = gql`
  1016. mutation RemoveOptionGroupFromProduct($productId: ID!, $optionGroupId: ID!) {
  1017. removeOptionGroupFromProduct(productId: $productId, optionGroupId: $optionGroupId) {
  1018. id
  1019. optionGroups {
  1020. id
  1021. code
  1022. options {
  1023. id
  1024. code
  1025. }
  1026. }
  1027. }
  1028. }
  1029. `;
  1030. export const GET_OPTION_GROUP = gql`
  1031. query GetOptionGroup($id: ID!) {
  1032. productOptionGroup(id: $id) {
  1033. id
  1034. code
  1035. options {
  1036. id
  1037. code
  1038. }
  1039. }
  1040. }
  1041. `;