product.e2e-spec.ts 50 KB

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