product.e2e-spec.ts 91 KB

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