product.e2e-spec.ts 84 KB

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