product.e2e-spec.ts 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  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_VARIANT_FRAGMENT, 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. GetProductWithVariantList,
  25. GetProductWithVariants,
  26. LanguageCode,
  27. ProductVariantFragment,
  28. ProductWithOptionsFragment,
  29. ProductWithVariants,
  30. RemoveOptionGroupFromProduct,
  31. SortOrder,
  32. UpdateProduct,
  33. UpdateProductVariants,
  34. } from './graphql/generated-e2e-admin-types';
  35. import {
  36. ADD_OPTION_GROUP_TO_PRODUCT,
  37. CREATE_PRODUCT,
  38. CREATE_PRODUCT_VARIANTS,
  39. DELETE_PRODUCT,
  40. DELETE_PRODUCT_VARIANT,
  41. GET_ASSET_LIST,
  42. GET_PRODUCT_LIST,
  43. GET_PRODUCT_SIMPLE,
  44. GET_PRODUCT_WITH_VARIANTS,
  45. UPDATE_PRODUCT,
  46. UPDATE_PRODUCT_VARIANTS,
  47. } from './graphql/shared-definitions';
  48. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  49. // tslint:disable:no-non-null-assertion
  50. describe('Product resolver', () => {
  51. const { server, adminClient, shopClient } = createTestEnvironment(testConfig());
  52. const removeOptionGuard: ErrorResultGuard<ProductWithOptionsFragment> = createErrorResultGuard(
  53. input => !!input.optionGroups,
  54. );
  55. beforeAll(async () => {
  56. await server.init({
  57. initialData,
  58. customerCount: 1,
  59. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  60. });
  61. await adminClient.asSuperAdmin();
  62. }, TEST_SETUP_TIMEOUT_MS);
  63. afterAll(async () => {
  64. await server.destroy();
  65. });
  66. describe('products list query', () => {
  67. it('returns all products when no options passed', async () => {
  68. const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
  69. GET_PRODUCT_LIST,
  70. {},
  71. );
  72. expect(result.products.items.length).toBe(20);
  73. expect(result.products.totalItems).toBe(20);
  74. });
  75. it('limits result set with skip & take', async () => {
  76. const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
  77. GET_PRODUCT_LIST,
  78. {
  79. options: {
  80. skip: 0,
  81. take: 3,
  82. },
  83. },
  84. );
  85. expect(result.products.items.length).toBe(3);
  86. expect(result.products.totalItems).toBe(20);
  87. });
  88. it('filters by name admin', async () => {
  89. const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
  90. GET_PRODUCT_LIST,
  91. {
  92. options: {
  93. filter: {
  94. name: {
  95. contains: 'skateboard',
  96. },
  97. },
  98. },
  99. },
  100. );
  101. expect(result.products.items.length).toBe(1);
  102. expect(result.products.items[0].name).toBe('Cruiser Skateboard');
  103. });
  104. it('filters multiple admin', async () => {
  105. const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
  106. GET_PRODUCT_LIST,
  107. {
  108. options: {
  109. filter: {
  110. name: {
  111. contains: 'camera',
  112. },
  113. slug: {
  114. contains: 'tent',
  115. },
  116. },
  117. },
  118. },
  119. );
  120. expect(result.products.items.length).toBe(0);
  121. });
  122. it('sorts by name admin', async () => {
  123. const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
  124. GET_PRODUCT_LIST,
  125. {
  126. options: {
  127. sort: {
  128. name: SortOrder.ASC,
  129. },
  130. },
  131. },
  132. );
  133. expect(result.products.items.map(p => p.name)).toEqual([
  134. 'Bonsai Tree',
  135. 'Boxing Gloves',
  136. 'Camera Lens',
  137. 'Clacky Keyboard',
  138. 'Cruiser Skateboard',
  139. 'Curvy Monitor',
  140. 'Football',
  141. 'Gaming PC',
  142. 'Hard Drive',
  143. 'Instant Camera',
  144. 'Laptop',
  145. 'Orchid',
  146. 'Road Bike',
  147. 'Running Shoe',
  148. 'Skipping Rope',
  149. 'Slr Camera',
  150. 'Spiky Cactus',
  151. 'Tent',
  152. 'Tripod',
  153. 'USB Cable',
  154. ]);
  155. });
  156. it('filters by name shop', async () => {
  157. const result = await shopClient.query<GetProductList.Query, GetProductList.Variables>(
  158. GET_PRODUCT_LIST,
  159. {
  160. options: {
  161. filter: {
  162. name: {
  163. contains: 'skateboard',
  164. },
  165. },
  166. },
  167. },
  168. );
  169. expect(result.products.items.length).toBe(1);
  170. expect(result.products.items[0].name).toBe('Cruiser Skateboard');
  171. });
  172. it('sorts by name shop', async () => {
  173. const result = await shopClient.query<GetProductList.Query, GetProductList.Variables>(
  174. GET_PRODUCT_LIST,
  175. {
  176. options: {
  177. sort: {
  178. name: SortOrder.ASC,
  179. },
  180. },
  181. },
  182. );
  183. expect(result.products.items.map(p => p.name)).toEqual([
  184. 'Bonsai Tree',
  185. 'Boxing Gloves',
  186. 'Camera Lens',
  187. 'Clacky Keyboard',
  188. 'Cruiser Skateboard',
  189. 'Curvy Monitor',
  190. 'Football',
  191. 'Gaming PC',
  192. 'Hard Drive',
  193. 'Instant Camera',
  194. 'Laptop',
  195. 'Orchid',
  196. 'Road Bike',
  197. 'Running Shoe',
  198. 'Skipping Rope',
  199. 'Slr Camera',
  200. 'Spiky Cactus',
  201. 'Tent',
  202. 'Tripod',
  203. 'USB Cable',
  204. ]);
  205. });
  206. });
  207. describe('product query', () => {
  208. it('by id', async () => {
  209. const { product } = await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
  210. GET_PRODUCT_SIMPLE,
  211. { id: 'T_2' },
  212. );
  213. if (!product) {
  214. fail('Product not found');
  215. return;
  216. }
  217. expect(product.id).toBe('T_2');
  218. });
  219. it('by slug', async () => {
  220. const { product } = await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
  221. GET_PRODUCT_SIMPLE,
  222. { slug: 'curvy-monitor' },
  223. );
  224. if (!product) {
  225. fail('Product not found');
  226. return;
  227. }
  228. expect(product.slug).toBe('curvy-monitor');
  229. });
  230. // https://github.com/vendure-ecommerce/vendure/issues/820
  231. it('by slug with multiple assets', async () => {
  232. const { product: product1 } = await adminClient.query<
  233. GetProductSimple.Query,
  234. GetProductSimple.Variables
  235. >(GET_PRODUCT_SIMPLE, { id: 'T_1' });
  236. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  237. UPDATE_PRODUCT,
  238. {
  239. input: {
  240. id: product1!.id,
  241. assetIds: ['T_1', 'T_2', 'T_3'],
  242. },
  243. },
  244. );
  245. const { product } = await adminClient.query<
  246. GetProductWithVariants.Query,
  247. GetProductWithVariants.Variables
  248. >(GET_PRODUCT_WITH_VARIANTS, { slug: product1!.slug });
  249. if (!product) {
  250. fail('Product not found');
  251. return;
  252. }
  253. expect(product.assets.map(a => a.id)).toEqual(['T_1', 'T_2', 'T_3']);
  254. });
  255. // https://github.com/vendure-ecommerce/vendure/issues/538
  256. it('falls back to default language slug', async () => {
  257. const { product } = await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
  258. GET_PRODUCT_SIMPLE,
  259. { slug: 'curvy-monitor' },
  260. { languageCode: LanguageCode.de },
  261. );
  262. if (!product) {
  263. fail('Product not found');
  264. return;
  265. }
  266. expect(product.slug).toBe('curvy-monitor');
  267. });
  268. it(
  269. 'throws if neither id nor slug provided',
  270. assertThrowsWithMessage(async () => {
  271. await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
  272. GET_PRODUCT_SIMPLE,
  273. {},
  274. );
  275. }, 'Either the Product id or slug must be provided'),
  276. );
  277. it(
  278. 'throws if id and slug do not refer to the same Product',
  279. assertThrowsWithMessage(async () => {
  280. await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
  281. GET_PRODUCT_SIMPLE,
  282. {
  283. id: 'T_2',
  284. slug: 'laptop',
  285. },
  286. );
  287. }, 'The provided id and slug refer to different Products'),
  288. );
  289. it('returns expected properties', async () => {
  290. const { product } = await adminClient.query<
  291. GetProductWithVariants.Query,
  292. GetProductWithVariants.Variables
  293. >(GET_PRODUCT_WITH_VARIANTS, {
  294. id: 'T_2',
  295. });
  296. if (!product) {
  297. fail('Product not found');
  298. return;
  299. }
  300. expect(omit(product, ['variants'])).toMatchSnapshot();
  301. expect(product.variants.length).toBe(2);
  302. });
  303. it('ProductVariant price properties are correct', async () => {
  304. const result = await adminClient.query<
  305. GetProductWithVariants.Query,
  306. GetProductWithVariants.Variables
  307. >(GET_PRODUCT_WITH_VARIANTS, {
  308. id: 'T_2',
  309. });
  310. if (!result.product) {
  311. fail('Product not found');
  312. return;
  313. }
  314. expect(result.product.variants[0].price).toBe(14374);
  315. expect(result.product.variants[0].taxCategory).toEqual({
  316. id: 'T_1',
  317. name: 'Standard Tax',
  318. });
  319. });
  320. it('returns null when id not found', async () => {
  321. const result = await adminClient.query<
  322. GetProductWithVariants.Query,
  323. GetProductWithVariants.Variables
  324. >(GET_PRODUCT_WITH_VARIANTS, {
  325. id: 'bad_id',
  326. });
  327. expect(result.product).toBeNull();
  328. });
  329. it('returns null when slug not found', async () => {
  330. const result = await adminClient.query<
  331. GetProductWithVariants.Query,
  332. GetProductWithVariants.Variables
  333. >(GET_PRODUCT_WITH_VARIANTS, {
  334. slug: 'bad_slug',
  335. });
  336. expect(result.product).toBeNull();
  337. });
  338. describe('product query with translations', () => {
  339. let translatedProduct: ProductWithVariants.Fragment;
  340. let en_translation: ProductWithVariants.Translations;
  341. let de_translation: ProductWithVariants.Translations;
  342. beforeAll(async () => {
  343. const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
  344. CREATE_PRODUCT,
  345. {
  346. input: {
  347. translations: [
  348. {
  349. languageCode: LanguageCode.en,
  350. name: 'en Pineapple',
  351. slug: 'en-pineapple',
  352. description: 'A delicious pineapple',
  353. },
  354. {
  355. languageCode: LanguageCode.de,
  356. name: 'de Ananas',
  357. slug: 'de-ananas',
  358. description: 'Eine köstliche Ananas',
  359. },
  360. ],
  361. },
  362. },
  363. );
  364. translatedProduct = result.createProduct;
  365. en_translation = translatedProduct.translations.find(
  366. t => t.languageCode === LanguageCode.en,
  367. )!;
  368. de_translation = translatedProduct.translations.find(
  369. t => t.languageCode === LanguageCode.de,
  370. )!;
  371. });
  372. it('en slug without translation arg', async () => {
  373. const { product } = await adminClient.query<
  374. GetProductSimple.Query,
  375. GetProductSimple.Variables
  376. >(GET_PRODUCT_SIMPLE, { slug: en_translation.slug });
  377. if (!product) {
  378. fail('Product not found');
  379. return;
  380. }
  381. expect(product.slug).toBe(en_translation.slug);
  382. });
  383. it('de slug without translation arg', async () => {
  384. const { product } = await adminClient.query<
  385. GetProductSimple.Query,
  386. GetProductSimple.Variables
  387. >(GET_PRODUCT_SIMPLE, { slug: de_translation.slug });
  388. if (!product) {
  389. fail('Product not found');
  390. return;
  391. }
  392. expect(product.slug).toBe(en_translation.slug);
  393. });
  394. it('en slug with translation en', async () => {
  395. const { product } = await adminClient.query<
  396. GetProductSimple.Query,
  397. GetProductSimple.Variables
  398. >(GET_PRODUCT_SIMPLE, { slug: en_translation.slug }, { languageCode: LanguageCode.en });
  399. if (!product) {
  400. fail('Product not found');
  401. return;
  402. }
  403. expect(product.slug).toBe(en_translation.slug);
  404. });
  405. it('de slug with translation en', async () => {
  406. const { product } = await adminClient.query<
  407. GetProductSimple.Query,
  408. GetProductSimple.Variables
  409. >(GET_PRODUCT_SIMPLE, { slug: de_translation.slug }, { languageCode: LanguageCode.en });
  410. if (!product) {
  411. fail('Product not found');
  412. return;
  413. }
  414. expect(product.slug).toBe(en_translation.slug);
  415. });
  416. it('en slug with translation de', async () => {
  417. const { product } = await adminClient.query<
  418. GetProductSimple.Query,
  419. GetProductSimple.Variables
  420. >(GET_PRODUCT_SIMPLE, { slug: en_translation.slug }, { languageCode: LanguageCode.de });
  421. if (!product) {
  422. fail('Product not found');
  423. return;
  424. }
  425. expect(product.slug).toBe(de_translation.slug);
  426. });
  427. it('de slug with translation de', async () => {
  428. const { product } = await adminClient.query<
  429. GetProductSimple.Query,
  430. GetProductSimple.Variables
  431. >(GET_PRODUCT_SIMPLE, { slug: de_translation.slug }, { languageCode: LanguageCode.de });
  432. if (!product) {
  433. fail('Product not found');
  434. return;
  435. }
  436. expect(product.slug).toBe(de_translation.slug);
  437. });
  438. it('de slug with translation ru', async () => {
  439. const { product } = await adminClient.query<
  440. GetProductSimple.Query,
  441. GetProductSimple.Variables
  442. >(GET_PRODUCT_SIMPLE, { slug: de_translation.slug }, { languageCode: LanguageCode.ru });
  443. if (!product) {
  444. fail('Product not found');
  445. return;
  446. }
  447. expect(product.slug).toBe(en_translation.slug);
  448. });
  449. });
  450. describe('product.variants', () => {
  451. it('returns product variants', async () => {
  452. const { product } = await adminClient.query<
  453. GetProductWithVariants.Query,
  454. GetProductWithVariants.Variables
  455. >(GET_PRODUCT_WITH_VARIANTS, {
  456. id: 'T_1',
  457. });
  458. expect(product?.variants.length).toBe(4);
  459. });
  460. it('returns product variants in existing language', async () => {
  461. const { product } = await adminClient.query<
  462. GetProductWithVariants.Query,
  463. GetProductWithVariants.Variables
  464. >(
  465. GET_PRODUCT_WITH_VARIANTS,
  466. {
  467. id: 'T_1',
  468. },
  469. { languageCode: LanguageCode.en },
  470. );
  471. expect(product?.variants.length).toBe(4);
  472. });
  473. it('returns product variants in non-existing language', async () => {
  474. const { product } = await adminClient.query<
  475. GetProductWithVariants.Query,
  476. GetProductWithVariants.Variables
  477. >(
  478. GET_PRODUCT_WITH_VARIANTS,
  479. {
  480. id: 'T_1',
  481. },
  482. { languageCode: LanguageCode.ru },
  483. );
  484. expect(product?.variants.length).toBe(4);
  485. });
  486. });
  487. describe('product.variants', () => {
  488. it('returns product variants', async () => {
  489. const { product } = await adminClient.query<
  490. GetProductWithVariants.Query,
  491. GetProductWithVariants.Variables
  492. >(GET_PRODUCT_WITH_VARIANTS, {
  493. id: 'T_1',
  494. });
  495. expect(product?.variants.length).toBe(4);
  496. });
  497. it('returns product variants in existing language', async () => {
  498. const { product } = await adminClient.query<
  499. GetProductWithVariants.Query,
  500. GetProductWithVariants.Variables
  501. >(
  502. GET_PRODUCT_WITH_VARIANTS,
  503. {
  504. id: 'T_1',
  505. },
  506. { languageCode: LanguageCode.en },
  507. );
  508. expect(product?.variants.length).toBe(4);
  509. });
  510. it('returns product variants in non-existing language', async () => {
  511. const { product } = await adminClient.query<
  512. GetProductWithVariants.Query,
  513. GetProductWithVariants.Variables
  514. >(
  515. GET_PRODUCT_WITH_VARIANTS,
  516. {
  517. id: 'T_1',
  518. },
  519. { languageCode: LanguageCode.ru },
  520. );
  521. expect(product?.variants.length).toBe(4);
  522. });
  523. });
  524. describe('product.variantList', () => {
  525. it('returns product variants', async () => {
  526. const { product } = await adminClient.query<
  527. GetProductWithVariantList.Query,
  528. GetProductWithVariantList.Variables
  529. >(GET_PRODUCT_WITH_VARIANT_LIST, {
  530. id: 'T_1',
  531. });
  532. expect(product?.variantList.items.length).toBe(4);
  533. expect(product?.variantList.totalItems).toBe(4);
  534. });
  535. it('returns product variants in existing language', async () => {
  536. const { product } = await adminClient.query<
  537. GetProductWithVariantList.Query,
  538. GetProductWithVariantList.Variables
  539. >(
  540. GET_PRODUCT_WITH_VARIANT_LIST,
  541. {
  542. id: 'T_1',
  543. },
  544. { languageCode: LanguageCode.en },
  545. );
  546. expect(product?.variantList.items.length).toBe(4);
  547. });
  548. it('returns product variants in non-existing language', async () => {
  549. const { product } = await adminClient.query<
  550. GetProductWithVariantList.Query,
  551. GetProductWithVariantList.Variables
  552. >(
  553. GET_PRODUCT_WITH_VARIANT_LIST,
  554. {
  555. id: 'T_1',
  556. },
  557. { languageCode: LanguageCode.ru },
  558. );
  559. expect(product?.variantList.items.length).toBe(4);
  560. });
  561. it('filter & sort', async () => {
  562. const { product } = await adminClient.query<
  563. GetProductWithVariantList.Query,
  564. GetProductWithVariantList.Variables
  565. >(GET_PRODUCT_WITH_VARIANT_LIST, {
  566. id: 'T_1',
  567. variantListOptions: {
  568. filter: {
  569. name: {
  570. contains: '15',
  571. },
  572. },
  573. sort: {
  574. price: SortOrder.DESC,
  575. },
  576. },
  577. });
  578. expect(product?.variantList.items.map(i => i.name)).toEqual([
  579. 'Laptop 15 inch 16GB',
  580. 'Laptop 15 inch 8GB',
  581. ]);
  582. });
  583. });
  584. });
  585. describe('productVariants list query', () => {
  586. it('returns list', async () => {
  587. const { productVariants } = await adminClient.query<
  588. GetProductVariantList.Query,
  589. GetProductVariantList.Variables
  590. >(GET_PRODUCT_VARIANT_LIST, {
  591. options: {
  592. take: 3,
  593. sort: {
  594. name: SortOrder.ASC,
  595. },
  596. },
  597. });
  598. expect(productVariants.items).toEqual([
  599. {
  600. id: 'T_34',
  601. name: 'Bonsai Tree',
  602. price: 1999,
  603. priceWithTax: 2399,
  604. sku: 'B01MXFLUSV',
  605. },
  606. {
  607. id: 'T_24',
  608. name: 'Boxing Gloves',
  609. price: 3304,
  610. priceWithTax: 3965,
  611. sku: 'B000ZYLPPU',
  612. },
  613. {
  614. id: 'T_19',
  615. name: 'Camera Lens',
  616. price: 10400,
  617. priceWithTax: 12480,
  618. sku: 'B0012UUP02',
  619. },
  620. ]);
  621. });
  622. it('sort by price', async () => {
  623. const { productVariants } = await adminClient.query<
  624. GetProductVariantList.Query,
  625. GetProductVariantList.Variables
  626. >(GET_PRODUCT_VARIANT_LIST, {
  627. options: {
  628. take: 3,
  629. sort: {
  630. price: SortOrder.ASC,
  631. },
  632. },
  633. });
  634. expect(productVariants.items).toEqual([
  635. {
  636. id: 'T_23',
  637. name: 'Skipping Rope',
  638. price: 799,
  639. priceWithTax: 959,
  640. sku: 'B07CNGXVXT',
  641. },
  642. {
  643. id: 'T_20',
  644. name: 'Tripod',
  645. price: 1498,
  646. priceWithTax: 1798,
  647. sku: 'B00XI87KV8',
  648. },
  649. {
  650. id: 'T_32',
  651. name: 'Spiky Cactus',
  652. price: 1550,
  653. priceWithTax: 1860,
  654. sku: 'SC011001',
  655. },
  656. ]);
  657. });
  658. it('sort by priceWithTax', async () => {
  659. const { productVariants } = await adminClient.query<
  660. GetProductVariantList.Query,
  661. GetProductVariantList.Variables
  662. >(GET_PRODUCT_VARIANT_LIST, {
  663. options: {
  664. take: 3,
  665. sort: {
  666. priceWithTax: SortOrder.ASC,
  667. },
  668. },
  669. });
  670. expect(productVariants.items).toEqual([
  671. {
  672. id: 'T_23',
  673. name: 'Skipping Rope',
  674. price: 799,
  675. priceWithTax: 959,
  676. sku: 'B07CNGXVXT',
  677. },
  678. {
  679. id: 'T_20',
  680. name: 'Tripod',
  681. price: 1498,
  682. priceWithTax: 1798,
  683. sku: 'B00XI87KV8',
  684. },
  685. {
  686. id: 'T_32',
  687. name: 'Spiky Cactus',
  688. price: 1550,
  689. priceWithTax: 1860,
  690. sku: 'SC011001',
  691. },
  692. ]);
  693. });
  694. it('filter by price', async () => {
  695. const { productVariants } = await adminClient.query<
  696. GetProductVariantList.Query,
  697. GetProductVariantList.Variables
  698. >(GET_PRODUCT_VARIANT_LIST, {
  699. options: {
  700. take: 3,
  701. filter: {
  702. price: {
  703. between: {
  704. start: 1400,
  705. end: 1500,
  706. },
  707. },
  708. },
  709. },
  710. });
  711. expect(productVariants.items).toEqual([
  712. {
  713. id: 'T_20',
  714. name: 'Tripod',
  715. price: 1498,
  716. priceWithTax: 1798,
  717. sku: 'B00XI87KV8',
  718. },
  719. ]);
  720. });
  721. it('filter by priceWithTax', async () => {
  722. const { productVariants } = await adminClient.query<
  723. GetProductVariantList.Query,
  724. GetProductVariantList.Variables
  725. >(GET_PRODUCT_VARIANT_LIST, {
  726. options: {
  727. take: 3,
  728. filter: {
  729. priceWithTax: {
  730. between: {
  731. start: 1400,
  732. end: 1500,
  733. },
  734. },
  735. },
  736. },
  737. });
  738. // Note the results are incorrect. This is a design trade-off. See the
  739. // commend on the ProductVariant.priceWithTax annotation for explanation.
  740. expect(productVariants.items).toEqual([
  741. {
  742. id: 'T_20',
  743. name: 'Tripod',
  744. price: 1498,
  745. priceWithTax: 1798,
  746. sku: 'B00XI87KV8',
  747. },
  748. ]);
  749. });
  750. it('returns variants for particular product by id', async () => {
  751. const { productVariants } = await adminClient.query<
  752. GetProductVariantList.Query,
  753. GetProductVariantList.Variables
  754. >(GET_PRODUCT_VARIANT_LIST, {
  755. options: {
  756. take: 3,
  757. sort: {
  758. price: SortOrder.ASC,
  759. },
  760. },
  761. productId: 'T_1',
  762. });
  763. expect(productVariants.items).toEqual([
  764. {
  765. id: 'T_1',
  766. name: 'Laptop 13 inch 8GB',
  767. price: 129900,
  768. priceWithTax: 155880,
  769. sku: 'L2201308',
  770. },
  771. {
  772. id: 'T_2',
  773. name: 'Laptop 15 inch 8GB',
  774. price: 139900,
  775. priceWithTax: 167880,
  776. sku: 'L2201508',
  777. },
  778. {
  779. id: 'T_3',
  780. name: 'Laptop 13 inch 16GB',
  781. priceWithTax: 263880,
  782. price: 219900,
  783. sku: 'L2201316',
  784. },
  785. ]);
  786. });
  787. });
  788. describe('productVariant query', () => {
  789. it('by id', async () => {
  790. const { productVariant } = await adminClient.query<
  791. GetProductVariant.Query,
  792. GetProductVariant.Variables
  793. >(GET_PRODUCT_VARIANT, {
  794. id: 'T_1',
  795. });
  796. expect(productVariant?.id).toBe('T_1');
  797. expect(productVariant?.name).toBe('Laptop 13 inch 8GB');
  798. });
  799. it('returns null when id not found', async () => {
  800. const { productVariant } = await adminClient.query<
  801. GetProductVariant.Query,
  802. GetProductVariant.Variables
  803. >(GET_PRODUCT_VARIANT, {
  804. id: 'T_999',
  805. });
  806. expect(productVariant).toBeNull();
  807. });
  808. });
  809. describe('product mutation', () => {
  810. let newTranslatedProduct: ProductWithVariants.Fragment;
  811. let newProduct: ProductWithVariants.Fragment;
  812. let newProductWithAssets: ProductWithVariants.Fragment;
  813. it('createProduct creates a new Product', async () => {
  814. const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
  815. CREATE_PRODUCT,
  816. {
  817. input: {
  818. translations: [
  819. {
  820. languageCode: LanguageCode.en,
  821. name: 'en Baked Potato',
  822. slug: 'en Baked Potato',
  823. description: 'A baked potato',
  824. },
  825. {
  826. languageCode: LanguageCode.de,
  827. name: 'de Baked Potato',
  828. slug: 'de-baked-potato',
  829. description: 'Eine baked Erdapfel',
  830. },
  831. ],
  832. },
  833. },
  834. );
  835. expect(omit(result.createProduct, ['translations'])).toMatchSnapshot();
  836. expect(result.createProduct.translations.map(t => t.description).sort()).toEqual([
  837. 'A baked potato',
  838. 'Eine baked Erdapfel',
  839. ]);
  840. newTranslatedProduct = result.createProduct;
  841. });
  842. it('createProduct creates a new Product with assets', async () => {
  843. const assetsResult = await adminClient.query<GetAssetList.Query, GetAssetList.Variables>(
  844. GET_ASSET_LIST,
  845. );
  846. const assetIds = assetsResult.assets.items.slice(0, 2).map(a => a.id);
  847. const featuredAssetId = assetsResult.assets.items[0].id;
  848. const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
  849. CREATE_PRODUCT,
  850. {
  851. input: {
  852. assetIds,
  853. featuredAssetId,
  854. translations: [
  855. {
  856. languageCode: LanguageCode.en,
  857. name: 'en Has Assets',
  858. slug: 'en-has-assets',
  859. description: 'A product with assets',
  860. },
  861. ],
  862. },
  863. },
  864. );
  865. expect(result.createProduct.assets.map(a => a.id)).toEqual(assetIds);
  866. expect(result.createProduct.featuredAsset!.id).toBe(featuredAssetId);
  867. newProductWithAssets = result.createProduct;
  868. });
  869. it('createProduct creates a disabled Product', async () => {
  870. const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
  871. CREATE_PRODUCT,
  872. {
  873. input: {
  874. enabled: false,
  875. translations: [
  876. {
  877. languageCode: LanguageCode.en,
  878. name: 'en Small apple',
  879. slug: 'en-small-apple',
  880. description: 'A small apple',
  881. },
  882. ],
  883. },
  884. },
  885. );
  886. expect(result.createProduct.enabled).toBe(false);
  887. newProduct = result.createProduct;
  888. });
  889. it('updateProduct updates a Product', async () => {
  890. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  891. UPDATE_PRODUCT,
  892. {
  893. input: {
  894. id: newProduct.id,
  895. translations: [
  896. {
  897. languageCode: LanguageCode.en,
  898. name: 'en Mashed Potato',
  899. slug: 'en-mashed-potato',
  900. description: 'A blob of mashed potato',
  901. },
  902. {
  903. languageCode: LanguageCode.de,
  904. name: 'de Mashed Potato',
  905. slug: 'de-mashed-potato',
  906. description: 'Eine blob von gemashed Erdapfel',
  907. },
  908. ],
  909. },
  910. },
  911. );
  912. expect(result.updateProduct.translations.map(t => t.description).sort()).toEqual([
  913. 'A blob of mashed potato',
  914. 'Eine blob von gemashed Erdapfel',
  915. ]);
  916. });
  917. it('slug is normalized to be url-safe', async () => {
  918. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  919. UPDATE_PRODUCT,
  920. {
  921. input: {
  922. id: newProduct.id,
  923. translations: [
  924. {
  925. languageCode: LanguageCode.en,
  926. name: 'en Mashed Potato',
  927. slug: 'A (very) nice potato!!',
  928. description: 'A blob of mashed potato',
  929. },
  930. ],
  931. },
  932. },
  933. );
  934. expect(result.updateProduct.slug).toBe('a-very-nice-potato');
  935. });
  936. it('create with duplicate slug is renamed to be unique', async () => {
  937. const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
  938. CREATE_PRODUCT,
  939. {
  940. input: {
  941. translations: [
  942. {
  943. languageCode: LanguageCode.en,
  944. name: 'Another baked potato',
  945. slug: 'a-very-nice-potato',
  946. description: 'Another baked potato but a bit different',
  947. },
  948. ],
  949. },
  950. },
  951. );
  952. expect(result.createProduct.slug).toBe('a-very-nice-potato-2');
  953. });
  954. it('update with duplicate slug is renamed to be unique', async () => {
  955. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  956. UPDATE_PRODUCT,
  957. {
  958. input: {
  959. id: newProduct.id,
  960. translations: [
  961. {
  962. languageCode: LanguageCode.en,
  963. name: 'Yet another baked potato',
  964. slug: 'a-very-nice-potato-2',
  965. description: 'Possibly the final baked potato',
  966. },
  967. ],
  968. },
  969. },
  970. );
  971. expect(result.updateProduct.slug).toBe('a-very-nice-potato-3');
  972. });
  973. it('slug duplicate check does not include self', async () => {
  974. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  975. UPDATE_PRODUCT,
  976. {
  977. input: {
  978. id: newProduct.id,
  979. translations: [
  980. {
  981. languageCode: LanguageCode.en,
  982. slug: 'a-very-nice-potato-3',
  983. },
  984. ],
  985. },
  986. },
  987. );
  988. expect(result.updateProduct.slug).toBe('a-very-nice-potato-3');
  989. });
  990. it('updateProduct accepts partial input', async () => {
  991. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  992. UPDATE_PRODUCT,
  993. {
  994. input: {
  995. id: newProduct.id,
  996. translations: [
  997. {
  998. languageCode: LanguageCode.en,
  999. name: 'en Very Mashed Potato',
  1000. },
  1001. ],
  1002. },
  1003. },
  1004. );
  1005. expect(result.updateProduct.translations.length).toBe(2);
  1006. expect(
  1007. result.updateProduct.translations.find(t => t.languageCode === LanguageCode.de)!.name,
  1008. ).toBe('de Mashed Potato');
  1009. expect(
  1010. result.updateProduct.translations.find(t => t.languageCode === LanguageCode.en)!.name,
  1011. ).toBe('en Very Mashed Potato');
  1012. expect(
  1013. result.updateProduct.translations.find(t => t.languageCode === LanguageCode.en)!.description,
  1014. ).toBe('Possibly the final baked potato');
  1015. });
  1016. it('updateProduct adds Assets to a product and sets featured asset', async () => {
  1017. const assetsResult = await adminClient.query<GetAssetList.Query, GetAssetList.Variables>(
  1018. GET_ASSET_LIST,
  1019. );
  1020. const assetIds = assetsResult.assets.items.map(a => a.id);
  1021. const featuredAssetId = assetsResult.assets.items[2].id;
  1022. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  1023. UPDATE_PRODUCT,
  1024. {
  1025. input: {
  1026. id: newProduct.id,
  1027. assetIds,
  1028. featuredAssetId,
  1029. },
  1030. },
  1031. );
  1032. expect(result.updateProduct.assets.map(a => a.id)).toEqual(assetIds);
  1033. expect(result.updateProduct.featuredAsset!.id).toBe(featuredAssetId);
  1034. });
  1035. it('updateProduct sets a featured asset', async () => {
  1036. const productResult = await adminClient.query<
  1037. GetProductWithVariants.Query,
  1038. GetProductWithVariants.Variables
  1039. >(GET_PRODUCT_WITH_VARIANTS, {
  1040. id: newProduct.id,
  1041. });
  1042. const assets = productResult.product!.assets;
  1043. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  1044. UPDATE_PRODUCT,
  1045. {
  1046. input: {
  1047. id: newProduct.id,
  1048. featuredAssetId: assets[0].id,
  1049. },
  1050. },
  1051. );
  1052. expect(result.updateProduct.featuredAsset!.id).toBe(assets[0].id);
  1053. });
  1054. it('updateProduct updates assets', async () => {
  1055. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  1056. UPDATE_PRODUCT,
  1057. {
  1058. input: {
  1059. id: newProduct.id,
  1060. featuredAssetId: 'T_1',
  1061. assetIds: ['T_1', 'T_2'],
  1062. },
  1063. },
  1064. );
  1065. expect(result.updateProduct.assets.map(a => a.id)).toEqual(['T_1', 'T_2']);
  1066. });
  1067. it('updateProduct updates FacetValues', async () => {
  1068. const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
  1069. UPDATE_PRODUCT,
  1070. {
  1071. input: {
  1072. id: newProduct.id,
  1073. facetValueIds: ['T_1'],
  1074. },
  1075. },
  1076. );
  1077. expect(result.updateProduct.facetValues.length).toEqual(1);
  1078. });
  1079. it(
  1080. 'updateProduct errors with an invalid productId',
  1081. assertThrowsWithMessage(
  1082. () =>
  1083. adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  1084. input: {
  1085. id: '999',
  1086. translations: [
  1087. {
  1088. languageCode: LanguageCode.en,
  1089. name: 'en Mashed Potato',
  1090. slug: 'en-mashed-potato',
  1091. description: 'A blob of mashed potato',
  1092. },
  1093. {
  1094. languageCode: LanguageCode.de,
  1095. name: 'de Mashed Potato',
  1096. slug: 'de-mashed-potato',
  1097. description: 'Eine blob von gemashed Erdapfel',
  1098. },
  1099. ],
  1100. },
  1101. }),
  1102. `No Product with the id '999' could be found`,
  1103. ),
  1104. );
  1105. it('addOptionGroupToProduct adds an option group', async () => {
  1106. const result = await adminClient.query<
  1107. AddOptionGroupToProduct.Mutation,
  1108. AddOptionGroupToProduct.Variables
  1109. >(ADD_OPTION_GROUP_TO_PRODUCT, {
  1110. optionGroupId: 'T_2',
  1111. productId: newProduct.id,
  1112. });
  1113. expect(result.addOptionGroupToProduct.optionGroups.length).toBe(1);
  1114. expect(result.addOptionGroupToProduct.optionGroups[0].id).toBe('T_2');
  1115. });
  1116. it(
  1117. 'addOptionGroupToProduct errors with an invalid productId',
  1118. assertThrowsWithMessage(
  1119. () =>
  1120. adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
  1121. ADD_OPTION_GROUP_TO_PRODUCT,
  1122. {
  1123. optionGroupId: 'T_1',
  1124. productId: '999',
  1125. },
  1126. ),
  1127. `No Product with the id '999' could be found`,
  1128. ),
  1129. );
  1130. it(
  1131. 'addOptionGroupToProduct errors with an invalid optionGroupId',
  1132. assertThrowsWithMessage(
  1133. () =>
  1134. adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
  1135. ADD_OPTION_GROUP_TO_PRODUCT,
  1136. {
  1137. optionGroupId: '999',
  1138. productId: newProduct.id,
  1139. },
  1140. ),
  1141. `No ProductOptionGroup with the id '999' could be found`,
  1142. ),
  1143. );
  1144. it('removeOptionGroupFromProduct removes an option group', async () => {
  1145. const { addOptionGroupToProduct } = await adminClient.query<
  1146. AddOptionGroupToProduct.Mutation,
  1147. AddOptionGroupToProduct.Variables
  1148. >(ADD_OPTION_GROUP_TO_PRODUCT, {
  1149. optionGroupId: 'T_1',
  1150. productId: newProductWithAssets.id,
  1151. });
  1152. expect(addOptionGroupToProduct.optionGroups.length).toBe(1);
  1153. const { removeOptionGroupFromProduct } = await adminClient.query<
  1154. RemoveOptionGroupFromProduct.Mutation,
  1155. RemoveOptionGroupFromProduct.Variables
  1156. >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
  1157. optionGroupId: 'T_1',
  1158. productId: newProductWithAssets.id,
  1159. });
  1160. removeOptionGuard.assertSuccess(removeOptionGroupFromProduct);
  1161. expect(removeOptionGroupFromProduct.id).toBe(newProductWithAssets.id);
  1162. expect(removeOptionGroupFromProduct.optionGroups.length).toBe(0);
  1163. });
  1164. it('removeOptionGroupFromProduct return error result if the optionGroup is being used by variants', async () => {
  1165. const { removeOptionGroupFromProduct } = await adminClient.query<
  1166. RemoveOptionGroupFromProduct.Mutation,
  1167. RemoveOptionGroupFromProduct.Variables
  1168. >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
  1169. optionGroupId: 'T_3',
  1170. productId: 'T_2',
  1171. });
  1172. removeOptionGuard.assertErrorResult(removeOptionGroupFromProduct);
  1173. expect(removeOptionGroupFromProduct.message).toBe(
  1174. `Cannot remove ProductOptionGroup "curvy-monitor-monitor-size" as it is used by 2 ProductVariants`,
  1175. );
  1176. expect(removeOptionGroupFromProduct.errorCode).toBe(ErrorCode.PRODUCT_OPTION_IN_USE_ERROR);
  1177. expect(removeOptionGroupFromProduct.optionGroupCode).toBe('curvy-monitor-monitor-size');
  1178. expect(removeOptionGroupFromProduct.productVariantCount).toBe(2);
  1179. });
  1180. it(
  1181. 'removeOptionGroupFromProduct errors with an invalid productId',
  1182. assertThrowsWithMessage(
  1183. () =>
  1184. adminClient.query<
  1185. RemoveOptionGroupFromProduct.Mutation,
  1186. RemoveOptionGroupFromProduct.Variables
  1187. >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
  1188. optionGroupId: '1',
  1189. productId: '999',
  1190. }),
  1191. `No Product with the id '999' could be found`,
  1192. ),
  1193. );
  1194. it(
  1195. 'removeOptionGroupFromProduct errors with an invalid optionGroupId',
  1196. assertThrowsWithMessage(
  1197. () =>
  1198. adminClient.query<
  1199. RemoveOptionGroupFromProduct.Mutation,
  1200. RemoveOptionGroupFromProduct.Variables
  1201. >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
  1202. optionGroupId: '999',
  1203. productId: newProduct.id,
  1204. }),
  1205. `No ProductOptionGroup with the id '999' could be found`,
  1206. ),
  1207. );
  1208. describe('variants', () => {
  1209. let variants: CreateProductVariants.CreateProductVariants[];
  1210. let optionGroup2: GetOptionGroup.ProductOptionGroup;
  1211. let optionGroup3: GetOptionGroup.ProductOptionGroup;
  1212. beforeAll(async () => {
  1213. await adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
  1214. ADD_OPTION_GROUP_TO_PRODUCT,
  1215. {
  1216. optionGroupId: 'T_3',
  1217. productId: newProduct.id,
  1218. },
  1219. );
  1220. const result1 = await adminClient.query<GetOptionGroup.Query, GetOptionGroup.Variables>(
  1221. GET_OPTION_GROUP,
  1222. { id: 'T_2' },
  1223. );
  1224. const result2 = await adminClient.query<GetOptionGroup.Query, GetOptionGroup.Variables>(
  1225. GET_OPTION_GROUP,
  1226. { id: 'T_3' },
  1227. );
  1228. optionGroup2 = result1.productOptionGroup!;
  1229. optionGroup3 = result2.productOptionGroup!;
  1230. });
  1231. it(
  1232. 'createProductVariants throws if optionIds not compatible with product',
  1233. assertThrowsWithMessage(async () => {
  1234. await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
  1235. CREATE_PRODUCT_VARIANTS,
  1236. {
  1237. input: [
  1238. {
  1239. productId: newProduct.id,
  1240. sku: 'PV1',
  1241. optionIds: [],
  1242. translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
  1243. },
  1244. ],
  1245. },
  1246. );
  1247. }, 'ProductVariant optionIds must include one optionId from each of the groups: curvy-monitor-monitor-size, laptop-ram'),
  1248. );
  1249. it(
  1250. 'createProductVariants throws if optionIds are duplicated',
  1251. assertThrowsWithMessage(async () => {
  1252. await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
  1253. CREATE_PRODUCT_VARIANTS,
  1254. {
  1255. input: [
  1256. {
  1257. productId: newProduct.id,
  1258. sku: 'PV1',
  1259. optionIds: [optionGroup2.options[0].id, optionGroup2.options[1].id],
  1260. translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
  1261. },
  1262. ],
  1263. },
  1264. );
  1265. }, 'ProductVariant optionIds must include one optionId from each of the groups: curvy-monitor-monitor-size, laptop-ram'),
  1266. );
  1267. it('createProductVariants works', async () => {
  1268. const { createProductVariants } = await adminClient.query<
  1269. CreateProductVariants.Mutation,
  1270. CreateProductVariants.Variables
  1271. >(CREATE_PRODUCT_VARIANTS, {
  1272. input: [
  1273. {
  1274. productId: newProduct.id,
  1275. sku: 'PV1',
  1276. optionIds: [optionGroup2.options[0].id, optionGroup3.options[0].id],
  1277. translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
  1278. },
  1279. ],
  1280. });
  1281. expect(createProductVariants[0]!.name).toBe('Variant 1');
  1282. expect(createProductVariants[0]!.options.map(pick(['id']))).toContainEqual({
  1283. id: optionGroup2.options[0].id,
  1284. });
  1285. expect(createProductVariants[0]!.options.map(pick(['id']))).toContainEqual({
  1286. id: optionGroup3.options[0].id,
  1287. });
  1288. });
  1289. it('createProductVariants adds multiple variants at once', async () => {
  1290. const { createProductVariants } = await adminClient.query<
  1291. CreateProductVariants.Mutation,
  1292. CreateProductVariants.Variables
  1293. >(CREATE_PRODUCT_VARIANTS, {
  1294. input: [
  1295. {
  1296. productId: newProduct.id,
  1297. sku: 'PV2',
  1298. optionIds: [optionGroup2.options[1].id, optionGroup3.options[0].id],
  1299. translations: [{ languageCode: LanguageCode.en, name: 'Variant 2' }],
  1300. },
  1301. {
  1302. productId: newProduct.id,
  1303. sku: 'PV3',
  1304. optionIds: [optionGroup2.options[1].id, optionGroup3.options[1].id],
  1305. translations: [{ languageCode: LanguageCode.en, name: 'Variant 3' }],
  1306. },
  1307. ],
  1308. });
  1309. const variant2 = createProductVariants.find(v => v!.name === 'Variant 2')!;
  1310. const variant3 = createProductVariants.find(v => v!.name === 'Variant 3')!;
  1311. expect(variant2.options.map(pick(['id']))).toContainEqual({ id: optionGroup2.options[1].id });
  1312. expect(variant2.options.map(pick(['id']))).toContainEqual({ id: optionGroup3.options[0].id });
  1313. expect(variant3.options.map(pick(['id']))).toContainEqual({ id: optionGroup2.options[1].id });
  1314. expect(variant3.options.map(pick(['id']))).toContainEqual({ id: optionGroup3.options[1].id });
  1315. variants = createProductVariants.filter(notNullOrUndefined);
  1316. });
  1317. it(
  1318. 'createProductVariants throws if options combination already exists',
  1319. assertThrowsWithMessage(async () => {
  1320. await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
  1321. CREATE_PRODUCT_VARIANTS,
  1322. {
  1323. input: [
  1324. {
  1325. productId: newProduct.id,
  1326. sku: 'PV2',
  1327. optionIds: [optionGroup2.options[0].id, optionGroup3.options[0].id],
  1328. translations: [{ languageCode: LanguageCode.en, name: 'Variant 2' }],
  1329. },
  1330. ],
  1331. },
  1332. );
  1333. }, 'A ProductVariant with the selected options already exists: Variant 1'),
  1334. );
  1335. it('updateProductVariants updates variants', async () => {
  1336. const firstVariant = variants[0];
  1337. const { updateProductVariants } = await adminClient.query<
  1338. UpdateProductVariants.Mutation,
  1339. UpdateProductVariants.Variables
  1340. >(UPDATE_PRODUCT_VARIANTS, {
  1341. input: [
  1342. {
  1343. id: firstVariant.id,
  1344. translations: firstVariant.translations,
  1345. sku: 'ABC',
  1346. price: 432,
  1347. },
  1348. ],
  1349. });
  1350. const updatedVariant = updateProductVariants[0];
  1351. if (!updatedVariant) {
  1352. fail('no updated variant returned.');
  1353. return;
  1354. }
  1355. expect(updatedVariant.sku).toBe('ABC');
  1356. expect(updatedVariant.price).toBe(432);
  1357. });
  1358. // https://github.com/vendure-ecommerce/vendure/issues/1101
  1359. it('after update, the updatedAt should be modified', async () => {
  1360. // Pause for a second to ensure the updatedAt date is more than 1s
  1361. // later than the createdAt date, since sqlite does not seem to store
  1362. // down to millisecond resolution.
  1363. await new Promise(resolve => setTimeout(resolve, 1000));
  1364. const firstVariant = variants[0];
  1365. const { updateProductVariants } = await adminClient.query<
  1366. UpdateProductVariants.Mutation,
  1367. UpdateProductVariants.Variables
  1368. >(UPDATE_PRODUCT_VARIANTS, {
  1369. input: [
  1370. {
  1371. id: firstVariant.id,
  1372. translations: firstVariant.translations,
  1373. sku: 'ABCD',
  1374. price: 432,
  1375. },
  1376. ],
  1377. });
  1378. const updatedVariant = updateProductVariants.find(v => v?.id === variants[0].id);
  1379. expect(updatedVariant?.updatedAt).not.toBe(updatedVariant?.createdAt);
  1380. });
  1381. it('updateProductVariants updates assets', async () => {
  1382. const firstVariant = variants[0];
  1383. const result = await adminClient.query<
  1384. UpdateProductVariants.Mutation,
  1385. UpdateProductVariants.Variables
  1386. >(UPDATE_PRODUCT_VARIANTS, {
  1387. input: [
  1388. {
  1389. id: firstVariant.id,
  1390. assetIds: ['T_1', 'T_2'],
  1391. featuredAssetId: 'T_2',
  1392. },
  1393. ],
  1394. });
  1395. const updatedVariant = result.updateProductVariants[0];
  1396. if (!updatedVariant) {
  1397. fail('no updated variant returned.');
  1398. return;
  1399. }
  1400. expect(updatedVariant.assets.map(a => a.id)).toEqual(['T_1', 'T_2']);
  1401. expect(updatedVariant.featuredAsset!.id).toBe('T_2');
  1402. });
  1403. it('updateProductVariants updates assets again', async () => {
  1404. const firstVariant = variants[0];
  1405. const result = await adminClient.query<
  1406. UpdateProductVariants.Mutation,
  1407. UpdateProductVariants.Variables
  1408. >(UPDATE_PRODUCT_VARIANTS, {
  1409. input: [
  1410. {
  1411. id: firstVariant.id,
  1412. assetIds: ['T_4', 'T_3'],
  1413. featuredAssetId: 'T_4',
  1414. },
  1415. ],
  1416. });
  1417. const updatedVariant = result.updateProductVariants[0];
  1418. if (!updatedVariant) {
  1419. fail('no updated variant returned.');
  1420. return;
  1421. }
  1422. expect(updatedVariant.assets.map(a => a.id)).toEqual(['T_4', 'T_3']);
  1423. expect(updatedVariant.featuredAsset!.id).toBe('T_4');
  1424. });
  1425. it('updateProductVariants updates taxCategory and price', async () => {
  1426. const firstVariant = variants[0];
  1427. const result = await adminClient.query<
  1428. UpdateProductVariants.Mutation,
  1429. UpdateProductVariants.Variables
  1430. >(UPDATE_PRODUCT_VARIANTS, {
  1431. input: [
  1432. {
  1433. id: firstVariant.id,
  1434. price: 105,
  1435. taxCategoryId: 'T_2',
  1436. },
  1437. ],
  1438. });
  1439. const updatedVariant = result.updateProductVariants[0];
  1440. if (!updatedVariant) {
  1441. fail('no updated variant returned.');
  1442. return;
  1443. }
  1444. expect(updatedVariant.price).toBe(105);
  1445. expect(updatedVariant.taxCategory.id).toBe('T_2');
  1446. });
  1447. it('updateProductVariants updates facetValues', async () => {
  1448. const firstVariant = variants[0];
  1449. const result = await adminClient.query<
  1450. UpdateProductVariants.Mutation,
  1451. UpdateProductVariants.Variables
  1452. >(UPDATE_PRODUCT_VARIANTS, {
  1453. input: [
  1454. {
  1455. id: firstVariant.id,
  1456. facetValueIds: ['T_1'],
  1457. },
  1458. ],
  1459. });
  1460. const updatedVariant = result.updateProductVariants[0];
  1461. if (!updatedVariant) {
  1462. fail('no updated variant returned.');
  1463. return;
  1464. }
  1465. expect(updatedVariant.facetValues.length).toBe(1);
  1466. expect(updatedVariant.facetValues[0].id).toBe('T_1');
  1467. });
  1468. it(
  1469. 'updateProductVariants throws with an invalid variant id',
  1470. assertThrowsWithMessage(
  1471. () =>
  1472. adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1473. UPDATE_PRODUCT_VARIANTS,
  1474. {
  1475. input: [
  1476. {
  1477. id: 'T_999',
  1478. translations: variants[0].translations,
  1479. sku: 'ABC',
  1480. price: 432,
  1481. },
  1482. ],
  1483. },
  1484. ),
  1485. `No ProductVariant with the id '999' could be found`,
  1486. ),
  1487. );
  1488. let deletedVariant: ProductVariantFragment;
  1489. it('deleteProductVariant', async () => {
  1490. const result1 = await adminClient.query<
  1491. GetProductWithVariants.Query,
  1492. GetProductWithVariants.Variables
  1493. >(GET_PRODUCT_WITH_VARIANTS, {
  1494. id: newProduct.id,
  1495. });
  1496. const sortedVariantIds = result1.product!.variants.map(v => v.id).sort();
  1497. expect(sortedVariantIds).toEqual(['T_35', 'T_36', 'T_37']);
  1498. const { deleteProductVariant } = await adminClient.query<
  1499. DeleteProductVariant.Mutation,
  1500. DeleteProductVariant.Variables
  1501. >(DELETE_PRODUCT_VARIANT, {
  1502. id: sortedVariantIds[0],
  1503. });
  1504. expect(deleteProductVariant.result).toBe(DeletionResult.DELETED);
  1505. const result2 = await adminClient.query<
  1506. GetProductWithVariants.Query,
  1507. GetProductWithVariants.Variables
  1508. >(GET_PRODUCT_WITH_VARIANTS, {
  1509. id: newProduct.id,
  1510. });
  1511. expect(result2.product!.variants.map(v => v.id).sort()).toEqual(['T_36', 'T_37']);
  1512. deletedVariant = result1.product?.variants.find(v => v.id === 'T_35')!;
  1513. });
  1514. /** Testing https://github.com/vendure-ecommerce/vendure/issues/412 **/
  1515. it('createProductVariants ignores deleted variants when checking for existing combinations', async () => {
  1516. const { createProductVariants } = await adminClient.query<
  1517. CreateProductVariants.Mutation,
  1518. CreateProductVariants.Variables
  1519. >(CREATE_PRODUCT_VARIANTS, {
  1520. input: [
  1521. {
  1522. productId: newProduct.id,
  1523. sku: 'RE1',
  1524. optionIds: [deletedVariant.options[0].id, deletedVariant.options[1].id],
  1525. translations: [{ languageCode: LanguageCode.en, name: 'Re-created Variant' }],
  1526. },
  1527. ],
  1528. });
  1529. expect(createProductVariants.length).toBe(1);
  1530. expect(createProductVariants[0]!.options.map(o => o.code).sort()).toEqual(
  1531. deletedVariant.options.map(o => o.code).sort(),
  1532. );
  1533. });
  1534. // https://github.com/vendure-ecommerce/vendure/issues/980
  1535. it('creating variants in a non-default language', async () => {
  1536. const { createProduct } = await adminClient.query<
  1537. CreateProduct.Mutation,
  1538. CreateProduct.Variables
  1539. >(CREATE_PRODUCT, {
  1540. input: {
  1541. translations: [
  1542. {
  1543. languageCode: LanguageCode.de,
  1544. name: 'Ananas',
  1545. slug: 'ananas',
  1546. description: 'Yummy Ananas',
  1547. },
  1548. ],
  1549. },
  1550. });
  1551. const { createProductVariants } = await adminClient.query<
  1552. CreateProductVariants.Mutation,
  1553. CreateProductVariants.Variables
  1554. >(CREATE_PRODUCT_VARIANTS, {
  1555. input: [
  1556. {
  1557. productId: createProduct.id,
  1558. sku: 'AN1110111',
  1559. optionIds: [],
  1560. translations: [{ languageCode: LanguageCode.de, name: 'Ananas Klein' }],
  1561. },
  1562. ],
  1563. });
  1564. expect(createProductVariants.length).toBe(1);
  1565. expect(createProductVariants[0]?.name).toBe('Ananas Klein');
  1566. const { product } = await adminClient.query<
  1567. GetProductWithVariants.Query,
  1568. GetProductWithVariants.Variables
  1569. >(
  1570. GET_PRODUCT_WITH_VARIANTS,
  1571. {
  1572. id: createProduct.id,
  1573. },
  1574. { languageCode: LanguageCode.en },
  1575. );
  1576. expect(product?.variants.length).toBe(1);
  1577. });
  1578. });
  1579. });
  1580. describe('deletion', () => {
  1581. let allProducts: GetProductList.Items[];
  1582. let productToDelete: GetProductWithVariants.Product;
  1583. beforeAll(async () => {
  1584. const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
  1585. GET_PRODUCT_LIST,
  1586. {
  1587. options: {
  1588. sort: {
  1589. id: SortOrder.ASC,
  1590. },
  1591. },
  1592. },
  1593. );
  1594. allProducts = result.products.items;
  1595. });
  1596. it('deletes a product', async () => {
  1597. const { product } = await adminClient.query<
  1598. GetProductWithVariants.Query,
  1599. GetProductWithVariants.Variables
  1600. >(GET_PRODUCT_WITH_VARIANTS, {
  1601. id: allProducts[0].id,
  1602. });
  1603. const result = await adminClient.query<DeleteProduct.Mutation, DeleteProduct.Variables>(
  1604. DELETE_PRODUCT,
  1605. { id: product!.id },
  1606. );
  1607. expect(result.deleteProduct).toEqual({ result: DeletionResult.DELETED });
  1608. productToDelete = product!;
  1609. });
  1610. it('cannot get a deleted product', async () => {
  1611. const { product } = await adminClient.query<
  1612. GetProductWithVariants.Query,
  1613. GetProductWithVariants.Variables
  1614. >(GET_PRODUCT_WITH_VARIANTS, {
  1615. id: productToDelete.id,
  1616. });
  1617. expect(product).toBe(null);
  1618. });
  1619. // https://github.com/vendure-ecommerce/vendure/issues/1096
  1620. it('variants of deleted product are also deleted', async () => {
  1621. for (const variant of productToDelete.variants) {
  1622. const { productVariant } = await adminClient.query<
  1623. GetProductVariant.Query,
  1624. GetProductVariant.Variables
  1625. >(GET_PRODUCT_VARIANT, {
  1626. id: variant.id,
  1627. });
  1628. expect(productVariant).toBe(null);
  1629. }
  1630. });
  1631. it('deleted product omitted from list', async () => {
  1632. const result = await adminClient.query<GetProductList.Query>(GET_PRODUCT_LIST);
  1633. expect(result.products.items.length).toBe(allProducts.length - 1);
  1634. expect(result.products.items.map(c => c.id).includes(productToDelete.id)).toBe(false);
  1635. });
  1636. it(
  1637. 'updateProduct throws for deleted product',
  1638. assertThrowsWithMessage(
  1639. () =>
  1640. adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  1641. input: {
  1642. id: productToDelete.id,
  1643. facetValueIds: ['T_1'],
  1644. },
  1645. }),
  1646. `No Product with the id '1' could be found`,
  1647. ),
  1648. );
  1649. it(
  1650. 'addOptionGroupToProduct throws for deleted product',
  1651. assertThrowsWithMessage(
  1652. () =>
  1653. adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
  1654. ADD_OPTION_GROUP_TO_PRODUCT,
  1655. {
  1656. optionGroupId: 'T_1',
  1657. productId: productToDelete.id,
  1658. },
  1659. ),
  1660. `No Product with the id '1' could be found`,
  1661. ),
  1662. );
  1663. it(
  1664. 'removeOptionGroupToProduct throws for deleted product',
  1665. assertThrowsWithMessage(
  1666. () =>
  1667. adminClient.query<
  1668. RemoveOptionGroupFromProduct.Mutation,
  1669. RemoveOptionGroupFromProduct.Variables
  1670. >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
  1671. optionGroupId: 'T_1',
  1672. productId: productToDelete.id,
  1673. }),
  1674. `No Product with the id '1' could be found`,
  1675. ),
  1676. );
  1677. // https://github.com/vendure-ecommerce/vendure/issues/558
  1678. it('slug of a deleted product can be re-used', async () => {
  1679. const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
  1680. CREATE_PRODUCT,
  1681. {
  1682. input: {
  1683. translations: [
  1684. {
  1685. languageCode: LanguageCode.en,
  1686. name: 'Product reusing deleted slug',
  1687. slug: productToDelete.slug,
  1688. description: 'stuff',
  1689. },
  1690. ],
  1691. },
  1692. },
  1693. );
  1694. expect(result.createProduct.slug).toBe(productToDelete.slug);
  1695. });
  1696. // https://github.com/vendure-ecommerce/vendure/issues/800
  1697. it('product can be fetched by slug of a deleted product', async () => {
  1698. const { product } = await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
  1699. GET_PRODUCT_SIMPLE,
  1700. { slug: productToDelete.slug },
  1701. );
  1702. if (!product) {
  1703. fail('Product not found');
  1704. return;
  1705. }
  1706. expect(product.slug).toBe(productToDelete.slug);
  1707. });
  1708. });
  1709. });
  1710. export const REMOVE_OPTION_GROUP_FROM_PRODUCT = gql`
  1711. mutation RemoveOptionGroupFromProduct($productId: ID!, $optionGroupId: ID!) {
  1712. removeOptionGroupFromProduct(productId: $productId, optionGroupId: $optionGroupId) {
  1713. ...ProductWithOptions
  1714. ... on ProductOptionInUseError {
  1715. errorCode
  1716. message
  1717. optionGroupCode
  1718. productVariantCount
  1719. }
  1720. }
  1721. }
  1722. ${PRODUCT_WITH_OPTIONS_FRAGMENT}
  1723. `;
  1724. export const GET_OPTION_GROUP = gql`
  1725. query GetOptionGroup($id: ID!) {
  1726. productOptionGroup(id: $id) {
  1727. id
  1728. code
  1729. options {
  1730. id
  1731. code
  1732. }
  1733. }
  1734. }
  1735. `;
  1736. export const GET_PRODUCT_VARIANT = gql`
  1737. query GetProductVariant($id: ID!) {
  1738. productVariant(id: $id) {
  1739. id
  1740. name
  1741. }
  1742. }
  1743. `;
  1744. export const GET_PRODUCT_VARIANT_LIST = gql`
  1745. query GetProductVariantLIST($options: ProductVariantListOptions, $productId: ID) {
  1746. productVariants(options: $options, productId: $productId) {
  1747. items {
  1748. id
  1749. name
  1750. sku
  1751. price
  1752. priceWithTax
  1753. }
  1754. totalItems
  1755. }
  1756. }
  1757. `;
  1758. export const GET_PRODUCT_WITH_VARIANT_LIST = gql`
  1759. query GetProductWithVariantList($id: ID, $variantListOptions: ProductVariantListOptions) {
  1760. product(id: $id) {
  1761. id
  1762. variantList(options: $variantListOptions) {
  1763. items {
  1764. ...ProductVariant
  1765. }
  1766. totalItems
  1767. }
  1768. }
  1769. }
  1770. ${PRODUCT_VARIANT_FRAGMENT}
  1771. `;