collection.e2e-spec.ts 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  1. /* tslint:disable:no-non-null-assertion */
  2. import { ROOT_COLLECTION_NAME } from '@vendure/common/lib/shared-constants';
  3. import {
  4. DefaultJobQueuePlugin,
  5. facetValueCollectionFilter,
  6. variantNameCollectionFilter,
  7. } from '@vendure/core';
  8. import { createTestEnvironment } from '@vendure/testing';
  9. import gql from 'graphql-tag';
  10. import path from 'path';
  11. import { initialData } from '../../../e2e-common/e2e-initial-data';
  12. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  13. import { pick } from '../../common/lib/pick';
  14. import { COLLECTION_FRAGMENT, FACET_VALUE_FRAGMENT } from './graphql/fragments';
  15. import {
  16. Collection,
  17. CreateCollection,
  18. CreateCollectionInput,
  19. CreateCollectionSelectVariants,
  20. DeleteCollection,
  21. DeleteProduct,
  22. DeleteProductVariant,
  23. DeletionResult,
  24. FacetValueFragment,
  25. GetAssetList,
  26. GetCollection,
  27. GetCollectionBreadcrumbs,
  28. GetCollectionNestedParents,
  29. GetCollectionProducts,
  30. GetCollections,
  31. GetCollectionsForProducts,
  32. GetCollectionsWithAssets,
  33. GetFacetValues,
  34. GetProductCollections,
  35. GetProductCollectionsWithParent,
  36. GetProductsWithVariantIds,
  37. LanguageCode,
  38. MoveCollection,
  39. PreviewCollectionVariantsQuery,
  40. PreviewCollectionVariantsQueryVariables,
  41. SortOrder,
  42. UpdateCollection,
  43. UpdateProduct,
  44. UpdateProductVariants,
  45. } from './graphql/generated-e2e-admin-types';
  46. import {
  47. CREATE_COLLECTION,
  48. DELETE_PRODUCT,
  49. DELETE_PRODUCT_VARIANT,
  50. GET_ASSET_LIST,
  51. GET_COLLECTIONS,
  52. UPDATE_COLLECTION,
  53. UPDATE_PRODUCT,
  54. UPDATE_PRODUCT_VARIANTS,
  55. } from './graphql/shared-definitions';
  56. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  57. import { awaitRunningJobs } from './utils/await-running-jobs';
  58. import { sortById } from './utils/test-order-utils';
  59. describe('Collection resolver', () => {
  60. const { server, adminClient, shopClient } = createTestEnvironment({
  61. ...testConfig(),
  62. plugins: [DefaultJobQueuePlugin],
  63. });
  64. let assets: GetAssetList.Items[];
  65. let facetValues: FacetValueFragment[];
  66. let electronicsCollection: Collection.Fragment;
  67. let computersCollection: Collection.Fragment;
  68. let pearCollection: Collection.Fragment;
  69. let electronicsBreadcrumbsCollection: Collection.Fragment;
  70. let computersBreadcrumbsCollection: Collection.Fragment;
  71. let pearBreadcrumbsCollection: Collection.Fragment;
  72. beforeAll(async () => {
  73. await server.init({
  74. initialData,
  75. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-collections.csv'),
  76. customerCount: 1,
  77. });
  78. await adminClient.asSuperAdmin();
  79. const assetsResult = await adminClient.query<GetAssetList.Query, GetAssetList.Variables>(
  80. GET_ASSET_LIST,
  81. {
  82. options: {
  83. sort: {
  84. name: SortOrder.ASC,
  85. },
  86. },
  87. },
  88. );
  89. assets = assetsResult.assets.items;
  90. const facetValuesResult = await adminClient.query<GetFacetValues.Query>(GET_FACET_VALUES);
  91. facetValues = facetValuesResult.facets.items.reduce(
  92. (values, facet) => [...values, ...facet.values],
  93. [] as FacetValueFragment[],
  94. );
  95. }, TEST_SETUP_TIMEOUT_MS);
  96. afterAll(async () => {
  97. await server.destroy();
  98. });
  99. /**
  100. * Test case for https://github.com/vendure-ecommerce/vendure/issues/97
  101. */
  102. it('collection breadcrumbs works after bootstrap', async () => {
  103. const result = await adminClient.query<GetCollectionBreadcrumbs.Query>(GET_COLLECTION_BREADCRUMBS, {
  104. id: 'T_1',
  105. });
  106. expect(result.collection!.breadcrumbs[0].name).toBe(ROOT_COLLECTION_NAME);
  107. });
  108. describe('createCollection', () => {
  109. it('creates a root collection', async () => {
  110. const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  111. CREATE_COLLECTION,
  112. {
  113. input: {
  114. assetIds: [assets[0].id, assets[1].id],
  115. featuredAssetId: assets[1].id,
  116. filters: [
  117. {
  118. code: facetValueCollectionFilter.code,
  119. arguments: [
  120. {
  121. name: 'facetValueIds',
  122. value: `["${getFacetValueId('electronics')}"]`,
  123. },
  124. {
  125. name: 'containsAny',
  126. value: `false`,
  127. },
  128. ],
  129. },
  130. ],
  131. translations: [
  132. {
  133. languageCode: LanguageCode.en,
  134. name: 'Electronics',
  135. description: '',
  136. slug: 'electronics',
  137. },
  138. ],
  139. },
  140. },
  141. );
  142. electronicsCollection = result.createCollection;
  143. expect(electronicsCollection).toMatchSnapshot();
  144. expect(electronicsCollection.parent!.name).toBe(ROOT_COLLECTION_NAME);
  145. });
  146. it('creates a nested collection', async () => {
  147. const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  148. CREATE_COLLECTION,
  149. {
  150. input: {
  151. parentId: electronicsCollection.id,
  152. translations: [
  153. {
  154. languageCode: LanguageCode.en,
  155. name: 'Computers',
  156. description: '',
  157. slug: 'computers',
  158. },
  159. ],
  160. filters: [
  161. {
  162. code: facetValueCollectionFilter.code,
  163. arguments: [
  164. {
  165. name: 'facetValueIds',
  166. value: `["${getFacetValueId('computers')}"]`,
  167. },
  168. {
  169. name: 'containsAny',
  170. value: `false`,
  171. },
  172. ],
  173. },
  174. ],
  175. },
  176. },
  177. );
  178. computersCollection = result.createCollection;
  179. expect(computersCollection.parent!.name).toBe(electronicsCollection.name);
  180. });
  181. it('creates a 2nd level nested collection', async () => {
  182. const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  183. CREATE_COLLECTION,
  184. {
  185. input: {
  186. parentId: computersCollection.id,
  187. translations: [
  188. { languageCode: LanguageCode.en, name: 'Pear', description: '', slug: 'pear' },
  189. ],
  190. filters: [
  191. {
  192. code: facetValueCollectionFilter.code,
  193. arguments: [
  194. {
  195. name: 'facetValueIds',
  196. value: `["${getFacetValueId('pear')}"]`,
  197. },
  198. {
  199. name: 'containsAny',
  200. value: `false`,
  201. },
  202. ],
  203. },
  204. ],
  205. },
  206. },
  207. );
  208. pearCollection = result.createCollection;
  209. expect(pearCollection.parent!.name).toBe(computersCollection.name);
  210. });
  211. it('slug is normalized to be url-safe', async () => {
  212. const { createCollection } = await adminClient.query<
  213. CreateCollection.Mutation,
  214. CreateCollection.Variables
  215. >(CREATE_COLLECTION, {
  216. input: {
  217. translations: [
  218. {
  219. languageCode: LanguageCode.en,
  220. name: 'Accessories',
  221. description: '',
  222. slug: 'Accessories!',
  223. },
  224. {
  225. languageCode: LanguageCode.de,
  226. name: 'Zubehör',
  227. description: '',
  228. slug: 'Zubehör!',
  229. },
  230. ],
  231. filters: [],
  232. },
  233. });
  234. expect(createCollection.slug).toBe('accessories');
  235. expect(createCollection.translations.find(t => t.languageCode === LanguageCode.en)?.slug).toBe(
  236. 'accessories',
  237. );
  238. expect(createCollection.translations.find(t => t.languageCode === LanguageCode.de)?.slug).toBe(
  239. 'zubehor',
  240. );
  241. });
  242. it('create with duplicate slug is renamed to be unique', async () => {
  243. const { createCollection } = await adminClient.query<
  244. CreateCollection.Mutation,
  245. CreateCollection.Variables
  246. >(CREATE_COLLECTION, {
  247. input: {
  248. translations: [
  249. {
  250. languageCode: LanguageCode.en,
  251. name: 'Accessories',
  252. description: '',
  253. slug: 'Accessories',
  254. },
  255. {
  256. languageCode: LanguageCode.de,
  257. name: 'Zubehör',
  258. description: '',
  259. slug: 'Zubehör',
  260. },
  261. ],
  262. filters: [],
  263. },
  264. });
  265. expect(createCollection.translations.find(t => t.languageCode === LanguageCode.en)?.slug).toBe(
  266. 'accessories-2',
  267. );
  268. expect(createCollection.translations.find(t => t.languageCode === LanguageCode.de)?.slug).toBe(
  269. 'zubehor-2',
  270. );
  271. });
  272. it('creates a root collection to became a 1st level collection later #779', async () => {
  273. const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  274. CREATE_COLLECTION,
  275. {
  276. input: {
  277. assetIds: [assets[0].id, assets[1].id],
  278. featuredAssetId: assets[1].id,
  279. filters: [
  280. {
  281. code: facetValueCollectionFilter.code,
  282. arguments: [
  283. {
  284. name: 'facetValueIds',
  285. value: `["${getFacetValueId('computers')}"]`,
  286. },
  287. {
  288. name: 'containsAny',
  289. value: `false`,
  290. },
  291. ],
  292. },
  293. ],
  294. translations: [
  295. {
  296. languageCode: LanguageCode.en,
  297. name: 'Computers Breadcrumbs',
  298. description: '',
  299. slug: 'computers_breadcrumbs',
  300. },
  301. ],
  302. },
  303. },
  304. );
  305. computersBreadcrumbsCollection = result.createCollection;
  306. expect(computersBreadcrumbsCollection.parent!.name).toBe(ROOT_COLLECTION_NAME);
  307. });
  308. it('creates a root collection to be a parent collection for 1st level collection with id greater than child collection #779', async () => {
  309. const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  310. CREATE_COLLECTION,
  311. {
  312. input: {
  313. assetIds: [assets[0].id, assets[1].id],
  314. featuredAssetId: assets[1].id,
  315. filters: [
  316. {
  317. code: facetValueCollectionFilter.code,
  318. arguments: [
  319. {
  320. name: 'facetValueIds',
  321. value: `["${getFacetValueId('electronics')}"]`,
  322. },
  323. {
  324. name: 'containsAny',
  325. value: `false`,
  326. },
  327. ],
  328. },
  329. ],
  330. translations: [
  331. {
  332. languageCode: LanguageCode.en,
  333. name: 'Electronics Breadcrumbs',
  334. description: '',
  335. slug: 'electronics_breadcrumbs',
  336. },
  337. ],
  338. },
  339. },
  340. );
  341. electronicsBreadcrumbsCollection = result.createCollection;
  342. expect(electronicsBreadcrumbsCollection.parent!.name).toBe(ROOT_COLLECTION_NAME);
  343. });
  344. it('creates a 2nd level nested collection #779', async () => {
  345. const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  346. CREATE_COLLECTION,
  347. {
  348. input: {
  349. parentId: computersBreadcrumbsCollection.id,
  350. translations: [
  351. {
  352. languageCode: LanguageCode.en,
  353. name: 'Pear Breadcrumbs',
  354. description: '',
  355. slug: 'pear_breadcrumbs',
  356. },
  357. ],
  358. filters: [
  359. {
  360. code: facetValueCollectionFilter.code,
  361. arguments: [
  362. {
  363. name: 'facetValueIds',
  364. value: `["${getFacetValueId('pear')}"]`,
  365. },
  366. {
  367. name: 'containsAny',
  368. value: `false`,
  369. },
  370. ],
  371. },
  372. ],
  373. },
  374. },
  375. );
  376. pearBreadcrumbsCollection = result.createCollection;
  377. expect(pearBreadcrumbsCollection.parent!.name).toBe(computersBreadcrumbsCollection.name);
  378. });
  379. });
  380. describe('updateCollection', () => {
  381. it('updates with assets', async () => {
  382. const { updateCollection } = await adminClient.query<
  383. UpdateCollection.Mutation,
  384. UpdateCollection.Variables
  385. >(UPDATE_COLLECTION, {
  386. input: {
  387. id: pearCollection.id,
  388. assetIds: [assets[1].id, assets[2].id],
  389. featuredAssetId: assets[1].id,
  390. translations: [
  391. { languageCode: LanguageCode.en, description: 'Apple stuff ', slug: 'apple-stuff' },
  392. ],
  393. },
  394. });
  395. await awaitRunningJobs(adminClient, 5000);
  396. expect(updateCollection).toMatchSnapshot();
  397. pearCollection = updateCollection;
  398. });
  399. it('updating existing assets', async () => {
  400. const { updateCollection } = await adminClient.query<
  401. UpdateCollection.Mutation,
  402. UpdateCollection.Variables
  403. >(UPDATE_COLLECTION, {
  404. input: {
  405. id: pearCollection.id,
  406. assetIds: [assets[3].id, assets[0].id],
  407. featuredAssetId: assets[3].id,
  408. },
  409. });
  410. await awaitRunningJobs(adminClient, 5000);
  411. expect(updateCollection.assets.map(a => a.id)).toEqual([assets[3].id, assets[0].id]);
  412. });
  413. it('removes all assets', async () => {
  414. const { updateCollection } = await adminClient.query<
  415. UpdateCollection.Mutation,
  416. UpdateCollection.Variables
  417. >(UPDATE_COLLECTION, {
  418. input: {
  419. id: pearCollection.id,
  420. assetIds: [],
  421. },
  422. });
  423. await awaitRunningJobs(adminClient, 5000);
  424. expect(updateCollection.assets).toEqual([]);
  425. expect(updateCollection.featuredAsset).toBeNull();
  426. });
  427. });
  428. describe('querying', () => {
  429. it('collection by id', async () => {
  430. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  431. GET_COLLECTION,
  432. {
  433. id: computersCollection.id,
  434. },
  435. );
  436. if (!result.collection) {
  437. fail(`did not return the collection`);
  438. return;
  439. }
  440. expect(result.collection.id).toBe(computersCollection.id);
  441. });
  442. it('collection by slug', async () => {
  443. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  444. GET_COLLECTION,
  445. {
  446. slug: computersCollection.slug,
  447. },
  448. );
  449. if (!result.collection) {
  450. fail(`did not return the collection`);
  451. return;
  452. }
  453. expect(result.collection.id).toBe(computersCollection.id);
  454. });
  455. // https://github.com/vendure-ecommerce/vendure/issues/538
  456. it('falls back to default language slug', async () => {
  457. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  458. GET_COLLECTION,
  459. {
  460. slug: computersCollection.slug,
  461. },
  462. { languageCode: LanguageCode.de },
  463. );
  464. if (!result.collection) {
  465. fail(`did not return the collection`);
  466. return;
  467. }
  468. expect(result.collection.id).toBe(computersCollection.id);
  469. });
  470. it(
  471. 'throws if neither id nor slug provided',
  472. assertThrowsWithMessage(async () => {
  473. await adminClient.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {});
  474. }, 'Either the Collection id or slug must be provided'),
  475. );
  476. it(
  477. 'throws if id and slug do not refer to the same Product',
  478. assertThrowsWithMessage(async () => {
  479. await adminClient.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
  480. id: computersCollection.id,
  481. slug: pearCollection.slug,
  482. });
  483. }, 'The provided id and slug refer to different Collections'),
  484. );
  485. it('parent field', async () => {
  486. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  487. GET_COLLECTION,
  488. {
  489. id: computersCollection.id,
  490. },
  491. );
  492. if (!result.collection) {
  493. fail(`did not return the collection`);
  494. return;
  495. }
  496. expect(result.collection.parent!.name).toBe('Electronics');
  497. });
  498. // Tests fix for https://github.com/vendure-ecommerce/vendure/issues/361
  499. it('parent field resolved by CollectionEntityResolver', async () => {
  500. const { product } = await adminClient.query<
  501. GetProductCollectionsWithParent.Query,
  502. GetProductCollectionsWithParent.Variables
  503. >(GET_PRODUCT_COLLECTIONS_WITH_PARENT, {
  504. id: 'T_1',
  505. });
  506. expect(product?.collections.length).toBe(6);
  507. expect(product?.collections.sort(sortById)).toEqual([
  508. {
  509. id: 'T_10',
  510. name: 'Pear Breadcrumbs',
  511. parent: {
  512. id: 'T_8',
  513. name: 'Computers Breadcrumbs',
  514. },
  515. },
  516. {
  517. id: 'T_3',
  518. name: 'Electronics',
  519. parent: {
  520. id: 'T_1',
  521. name: '__root_collection__',
  522. },
  523. },
  524. {
  525. id: 'T_4',
  526. name: 'Computers',
  527. parent: {
  528. id: 'T_3',
  529. name: 'Electronics',
  530. },
  531. },
  532. {
  533. id: 'T_5',
  534. name: 'Pear',
  535. parent: {
  536. id: 'T_4',
  537. name: 'Computers',
  538. },
  539. },
  540. {
  541. id: 'T_8',
  542. name: 'Computers Breadcrumbs',
  543. parent: {
  544. id: 'T_1',
  545. name: '__root_collection__',
  546. },
  547. },
  548. {
  549. id: 'T_9',
  550. name: 'Electronics Breadcrumbs',
  551. parent: {
  552. id: 'T_1',
  553. name: '__root_collection__',
  554. },
  555. },
  556. ]);
  557. });
  558. // https://github.com/vendure-ecommerce/vendure/issues/981
  559. it('nested parent field in shop API', async () => {
  560. const { collections } = await shopClient.query<GetCollectionNestedParents.Query>(
  561. GET_COLLECTION_NESTED_PARENTS,
  562. );
  563. expect(collections.items[0].parent?.name).toBe(ROOT_COLLECTION_NAME);
  564. });
  565. it('children field', async () => {
  566. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  567. GET_COLLECTION,
  568. {
  569. id: electronicsCollection.id,
  570. },
  571. );
  572. if (!result.collection) {
  573. fail(`did not return the collection`);
  574. return;
  575. }
  576. expect(result.collection.children!.length).toBe(1);
  577. expect(result.collection.children![0].name).toBe('Computers');
  578. });
  579. it('breadcrumbs', async () => {
  580. const result = await adminClient.query<
  581. GetCollectionBreadcrumbs.Query,
  582. GetCollectionBreadcrumbs.Variables
  583. >(GET_COLLECTION_BREADCRUMBS, {
  584. id: pearCollection.id,
  585. });
  586. if (!result.collection) {
  587. fail(`did not return the collection`);
  588. return;
  589. }
  590. expect(result.collection.breadcrumbs).toEqual([
  591. { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
  592. {
  593. id: electronicsCollection.id,
  594. name: electronicsCollection.name,
  595. slug: electronicsCollection.slug,
  596. },
  597. {
  598. id: computersCollection.id,
  599. name: computersCollection.name,
  600. slug: computersCollection.slug,
  601. },
  602. { id: pearCollection.id, name: pearCollection.name, slug: pearCollection.slug },
  603. ]);
  604. });
  605. it('breadcrumbs for root collection', async () => {
  606. const result = await adminClient.query<
  607. GetCollectionBreadcrumbs.Query,
  608. GetCollectionBreadcrumbs.Variables
  609. >(GET_COLLECTION_BREADCRUMBS, {
  610. id: 'T_1',
  611. });
  612. if (!result.collection) {
  613. fail(`did not return the collection`);
  614. return;
  615. }
  616. expect(result.collection.breadcrumbs).toEqual([
  617. { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
  618. ]);
  619. });
  620. it('collections.assets', async () => {
  621. const { collections } = await adminClient.query<GetCollectionsWithAssets.Query>(gql`
  622. query GetCollectionsWithAssets {
  623. collections {
  624. items {
  625. assets {
  626. name
  627. }
  628. }
  629. }
  630. }
  631. `);
  632. expect(collections.items[0].assets).toBeDefined();
  633. });
  634. // https://github.com/vendure-ecommerce/vendure/issues/642
  635. it('sorting on Collection.productVariants.price', async () => {
  636. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  637. GET_COLLECTION,
  638. {
  639. id: computersCollection.id,
  640. variantListOptions: {
  641. sort: {
  642. price: SortOrder.ASC,
  643. },
  644. },
  645. },
  646. );
  647. expect(collection!.productVariants.items.map(i => i.price)).toEqual([
  648. 3799, 5374, 6900, 7489, 7896, 9299, 13435, 14374, 16994, 93120, 94920, 108720, 109995, 129900,
  649. 139900, 219900, 229900,
  650. ]);
  651. });
  652. });
  653. describe('moveCollection', () => {
  654. it('moves a collection to a new parent', async () => {
  655. const result = await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(
  656. MOVE_COLLECTION,
  657. {
  658. input: {
  659. collectionId: pearCollection.id,
  660. parentId: electronicsCollection.id,
  661. index: 0,
  662. },
  663. },
  664. );
  665. expect(result.moveCollection.parent!.id).toBe(electronicsCollection.id);
  666. const positions = await getChildrenOf(electronicsCollection.id);
  667. expect(positions.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  668. });
  669. it('re-evaluates Collection contents on move', async () => {
  670. await awaitRunningJobs(adminClient, 5000);
  671. const result = await adminClient.query<
  672. GetCollectionProducts.Query,
  673. GetCollectionProducts.Variables
  674. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  675. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  676. 'Laptop 13 inch 8GB',
  677. 'Laptop 15 inch 8GB',
  678. 'Laptop 13 inch 16GB',
  679. 'Laptop 15 inch 16GB',
  680. 'Instant Camera',
  681. ]);
  682. });
  683. it('moves a 1st level collection to a new parent to check breadcrumbs', async () => {
  684. const result = await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(
  685. MOVE_COLLECTION,
  686. {
  687. input: {
  688. collectionId: computersBreadcrumbsCollection.id,
  689. parentId: electronicsBreadcrumbsCollection.id,
  690. index: 0,
  691. },
  692. },
  693. );
  694. expect(result.moveCollection.parent!.id).toBe(electronicsBreadcrumbsCollection.id);
  695. const positions = await getChildrenOf(electronicsBreadcrumbsCollection.id);
  696. expect(positions.map(i => i.id)).toEqual([computersBreadcrumbsCollection.id]);
  697. });
  698. it('breadcrumbs for collection with ids out of order', async () => {
  699. const result = await adminClient.query<
  700. GetCollectionBreadcrumbs.Query,
  701. GetCollectionBreadcrumbs.Variables
  702. >(GET_COLLECTION_BREADCRUMBS, {
  703. id: pearBreadcrumbsCollection.id,
  704. });
  705. if (!result.collection) {
  706. fail(`did not return the collection`);
  707. return;
  708. }
  709. expect(result.collection.breadcrumbs).toEqual([
  710. { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
  711. {
  712. id: electronicsBreadcrumbsCollection.id,
  713. name: electronicsBreadcrumbsCollection.name,
  714. slug: electronicsBreadcrumbsCollection.slug,
  715. },
  716. {
  717. id: computersBreadcrumbsCollection.id,
  718. name: computersBreadcrumbsCollection.name,
  719. slug: computersBreadcrumbsCollection.slug,
  720. },
  721. {
  722. id: pearBreadcrumbsCollection.id,
  723. name: pearBreadcrumbsCollection.name,
  724. slug: pearBreadcrumbsCollection.slug,
  725. },
  726. ]);
  727. });
  728. it('alters the position in the current parent 1', async () => {
  729. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  730. input: {
  731. collectionId: computersCollection.id,
  732. parentId: electronicsCollection.id,
  733. index: 0,
  734. },
  735. });
  736. const afterResult = await getChildrenOf(electronicsCollection.id);
  737. expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
  738. });
  739. it('alters the position in the current parent 2', async () => {
  740. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  741. input: {
  742. collectionId: pearCollection.id,
  743. parentId: electronicsCollection.id,
  744. index: 0,
  745. },
  746. });
  747. const afterResult = await getChildrenOf(electronicsCollection.id);
  748. expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  749. });
  750. it('corrects an out-of-bounds negative index value', async () => {
  751. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  752. input: {
  753. collectionId: pearCollection.id,
  754. parentId: electronicsCollection.id,
  755. index: -3,
  756. },
  757. });
  758. const afterResult = await getChildrenOf(electronicsCollection.id);
  759. expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  760. });
  761. it('corrects an out-of-bounds positive index value', async () => {
  762. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  763. input: {
  764. collectionId: pearCollection.id,
  765. parentId: electronicsCollection.id,
  766. index: 10,
  767. },
  768. });
  769. const afterResult = await getChildrenOf(electronicsCollection.id);
  770. expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
  771. });
  772. it(
  773. 'throws if attempting to move into self',
  774. assertThrowsWithMessage(
  775. () =>
  776. adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  777. input: {
  778. collectionId: pearCollection.id,
  779. parentId: pearCollection.id,
  780. index: 0,
  781. },
  782. }),
  783. `Cannot move a Collection into itself`,
  784. ),
  785. );
  786. it(
  787. 'throws if attempting to move into a descendant of self',
  788. assertThrowsWithMessage(
  789. () =>
  790. adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  791. input: {
  792. collectionId: pearCollection.id,
  793. parentId: pearCollection.id,
  794. index: 0,
  795. },
  796. }),
  797. `Cannot move a Collection into itself`,
  798. ),
  799. );
  800. async function getChildrenOf(parentId: string): Promise<Array<{ name: string; id: string }>> {
  801. const result = await adminClient.query<GetCollections.Query>(GET_COLLECTIONS);
  802. return result.collections.items.filter(i => i.parent!.id === parentId);
  803. }
  804. });
  805. describe('deleteCollection', () => {
  806. let collectionToDeleteParent: CreateCollection.CreateCollection;
  807. let collectionToDeleteChild: CreateCollection.CreateCollection;
  808. let laptopProductId: string;
  809. beforeAll(async () => {
  810. const result1 = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  811. CREATE_COLLECTION,
  812. {
  813. input: {
  814. filters: [
  815. {
  816. code: variantNameCollectionFilter.code,
  817. arguments: [
  818. {
  819. name: 'operator',
  820. value: 'contains',
  821. },
  822. {
  823. name: 'term',
  824. value: 'laptop',
  825. },
  826. ],
  827. },
  828. ],
  829. translations: [
  830. {
  831. languageCode: LanguageCode.en,
  832. name: 'Delete Me Parent',
  833. description: '',
  834. slug: 'delete-me-parent',
  835. },
  836. ],
  837. assetIds: ['T_1'],
  838. },
  839. },
  840. );
  841. collectionToDeleteParent = result1.createCollection;
  842. const result2 = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  843. CREATE_COLLECTION,
  844. {
  845. input: {
  846. filters: [],
  847. translations: [
  848. {
  849. languageCode: LanguageCode.en,
  850. name: 'Delete Me Child',
  851. description: '',
  852. slug: 'delete-me-child',
  853. },
  854. ],
  855. parentId: collectionToDeleteParent.id,
  856. assetIds: ['T_2'],
  857. },
  858. },
  859. );
  860. collectionToDeleteChild = result2.createCollection;
  861. await awaitRunningJobs(adminClient, 5000);
  862. });
  863. it(
  864. 'throws for invalid collection id',
  865. assertThrowsWithMessage(async () => {
  866. await adminClient.query<DeleteCollection.Mutation, DeleteCollection.Variables>(
  867. DELETE_COLLECTION,
  868. {
  869. id: 'T_999',
  870. },
  871. );
  872. }, "No Collection with the id '999' could be found"),
  873. );
  874. it('collection and product related prior to deletion', async () => {
  875. const { collection } = await adminClient.query<
  876. GetCollectionProducts.Query,
  877. GetCollectionProducts.Variables
  878. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  879. id: collectionToDeleteParent.id,
  880. });
  881. expect(collection!.productVariants.items.map(pick(['name']))).toEqual([
  882. { name: 'Laptop 13 inch 8GB' },
  883. { name: 'Laptop 15 inch 8GB' },
  884. { name: 'Laptop 13 inch 16GB' },
  885. { name: 'Laptop 15 inch 16GB' },
  886. ]);
  887. laptopProductId = collection!.productVariants.items[0].productId;
  888. const { product } = await adminClient.query<
  889. GetProductCollections.Query,
  890. GetProductCollections.Variables
  891. >(GET_PRODUCT_COLLECTIONS, {
  892. id: laptopProductId,
  893. });
  894. expect(product!.collections).toEqual([
  895. {
  896. id: 'T_3',
  897. name: 'Electronics',
  898. },
  899. {
  900. id: 'T_4',
  901. name: 'Computers',
  902. },
  903. {
  904. id: 'T_5',
  905. name: 'Pear',
  906. },
  907. {
  908. id: 'T_8',
  909. name: 'Computers Breadcrumbs',
  910. },
  911. {
  912. id: 'T_9',
  913. name: 'Electronics Breadcrumbs',
  914. },
  915. {
  916. id: 'T_10',
  917. name: 'Pear Breadcrumbs',
  918. },
  919. {
  920. id: 'T_11',
  921. name: 'Delete Me Parent',
  922. },
  923. {
  924. id: 'T_12',
  925. name: 'Delete Me Child',
  926. },
  927. ]);
  928. });
  929. it('deleteCollection works', async () => {
  930. const { deleteCollection } = await adminClient.query<
  931. DeleteCollection.Mutation,
  932. DeleteCollection.Variables
  933. >(DELETE_COLLECTION, {
  934. id: collectionToDeleteParent.id,
  935. });
  936. expect(deleteCollection.result).toBe(DeletionResult.DELETED);
  937. });
  938. it('deleted parent collection is null', async () => {
  939. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  940. GET_COLLECTION,
  941. {
  942. id: collectionToDeleteParent.id,
  943. },
  944. );
  945. expect(collection).toBeNull();
  946. });
  947. it('deleted child collection is null', async () => {
  948. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  949. GET_COLLECTION,
  950. {
  951. id: collectionToDeleteChild.id,
  952. },
  953. );
  954. expect(collection).toBeNull();
  955. });
  956. it('product no longer lists collection', async () => {
  957. const { product } = await adminClient.query<
  958. GetProductCollections.Query,
  959. GetProductCollections.Variables
  960. >(GET_PRODUCT_COLLECTIONS, {
  961. id: laptopProductId,
  962. });
  963. expect(product!.collections).toEqual([
  964. { id: 'T_3', name: 'Electronics' },
  965. { id: 'T_4', name: 'Computers' },
  966. { id: 'T_5', name: 'Pear' },
  967. {
  968. id: 'T_8',
  969. name: 'Computers Breadcrumbs',
  970. },
  971. {
  972. id: 'T_9',
  973. name: 'Electronics Breadcrumbs',
  974. },
  975. {
  976. id: 'T_10',
  977. name: 'Pear Breadcrumbs',
  978. },
  979. ]);
  980. });
  981. });
  982. describe('filters', () => {
  983. it('Collection with no filters has no productVariants', async () => {
  984. const result = await adminClient.query<
  985. CreateCollectionSelectVariants.Mutation,
  986. CreateCollectionSelectVariants.Variables
  987. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  988. input: {
  989. translations: [
  990. { languageCode: LanguageCode.en, name: 'Empty', description: '', slug: 'empty' },
  991. ],
  992. filters: [],
  993. } as CreateCollectionInput,
  994. });
  995. expect(result.createCollection.productVariants.totalItems).toBe(0);
  996. });
  997. describe('facetValue filter', () => {
  998. it('electronics', async () => {
  999. const result = await adminClient.query<
  1000. GetCollectionProducts.Query,
  1001. GetCollectionProducts.Variables
  1002. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1003. id: electronicsCollection.id,
  1004. });
  1005. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1006. 'Laptop 13 inch 8GB',
  1007. 'Laptop 15 inch 8GB',
  1008. 'Laptop 13 inch 16GB',
  1009. 'Laptop 15 inch 16GB',
  1010. 'Curvy Monitor 24 inch',
  1011. 'Curvy Monitor 27 inch',
  1012. 'Gaming PC i7-8700 240GB SSD',
  1013. 'Gaming PC R7-2700 240GB SSD',
  1014. 'Gaming PC i7-8700 120GB SSD',
  1015. 'Gaming PC R7-2700 120GB SSD',
  1016. 'Hard Drive 1TB',
  1017. 'Hard Drive 2TB',
  1018. 'Hard Drive 3TB',
  1019. 'Hard Drive 4TB',
  1020. 'Hard Drive 6TB',
  1021. 'Clacky Keyboard',
  1022. 'USB Cable',
  1023. 'Instant Camera',
  1024. 'Camera Lens',
  1025. 'Tripod',
  1026. 'SLR Camera',
  1027. ]);
  1028. });
  1029. it('computers', async () => {
  1030. const result = await adminClient.query<
  1031. GetCollectionProducts.Query,
  1032. GetCollectionProducts.Variables
  1033. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1034. id: computersCollection.id,
  1035. });
  1036. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1037. 'Laptop 13 inch 8GB',
  1038. 'Laptop 15 inch 8GB',
  1039. 'Laptop 13 inch 16GB',
  1040. 'Laptop 15 inch 16GB',
  1041. 'Curvy Monitor 24 inch',
  1042. 'Curvy Monitor 27 inch',
  1043. 'Gaming PC i7-8700 240GB SSD',
  1044. 'Gaming PC R7-2700 240GB SSD',
  1045. 'Gaming PC i7-8700 120GB SSD',
  1046. 'Gaming PC R7-2700 120GB SSD',
  1047. 'Hard Drive 1TB',
  1048. 'Hard Drive 2TB',
  1049. 'Hard Drive 3TB',
  1050. 'Hard Drive 4TB',
  1051. 'Hard Drive 6TB',
  1052. 'Clacky Keyboard',
  1053. 'USB Cable',
  1054. ]);
  1055. });
  1056. it('photo AND pear', async () => {
  1057. const result = await adminClient.query<
  1058. CreateCollectionSelectVariants.Mutation,
  1059. CreateCollectionSelectVariants.Variables
  1060. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  1061. input: {
  1062. translations: [
  1063. {
  1064. languageCode: LanguageCode.en,
  1065. name: 'Photo AND Pear',
  1066. description: '',
  1067. slug: 'photo-and-pear',
  1068. },
  1069. ],
  1070. filters: [
  1071. {
  1072. code: facetValueCollectionFilter.code,
  1073. arguments: [
  1074. {
  1075. name: 'facetValueIds',
  1076. value: `["${getFacetValueId('pear')}", "${getFacetValueId(
  1077. 'photo',
  1078. )}"]`,
  1079. },
  1080. {
  1081. name: 'containsAny',
  1082. value: `false`,
  1083. },
  1084. ],
  1085. },
  1086. ],
  1087. } as CreateCollectionInput,
  1088. });
  1089. await awaitRunningJobs(adminClient, 5000);
  1090. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  1091. GET_COLLECTION,
  1092. {
  1093. id: result.createCollection.id,
  1094. },
  1095. );
  1096. expect(collection!.productVariants.items.map(i => i.name)).toEqual(['Instant Camera']);
  1097. });
  1098. it('photo OR pear', async () => {
  1099. const result = await adminClient.query<
  1100. CreateCollectionSelectVariants.Mutation,
  1101. CreateCollectionSelectVariants.Variables
  1102. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  1103. input: {
  1104. translations: [
  1105. {
  1106. languageCode: LanguageCode.en,
  1107. name: 'Photo OR Pear',
  1108. description: '',
  1109. slug: 'photo-or-pear',
  1110. },
  1111. ],
  1112. filters: [
  1113. {
  1114. code: facetValueCollectionFilter.code,
  1115. arguments: [
  1116. {
  1117. name: 'facetValueIds',
  1118. value: `["${getFacetValueId('pear')}", "${getFacetValueId(
  1119. 'photo',
  1120. )}"]`,
  1121. },
  1122. {
  1123. name: 'containsAny',
  1124. value: `true`,
  1125. },
  1126. ],
  1127. },
  1128. ],
  1129. } as CreateCollectionInput,
  1130. });
  1131. await awaitRunningJobs(adminClient, 5000);
  1132. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  1133. GET_COLLECTION,
  1134. {
  1135. id: result.createCollection.id,
  1136. },
  1137. );
  1138. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  1139. 'Laptop 13 inch 8GB',
  1140. 'Laptop 15 inch 8GB',
  1141. 'Laptop 13 inch 16GB',
  1142. 'Laptop 15 inch 16GB',
  1143. 'Instant Camera',
  1144. 'Camera Lens',
  1145. 'Tripod',
  1146. 'SLR Camera',
  1147. 'Hat',
  1148. ]);
  1149. });
  1150. it('bell OR pear in computers', async () => {
  1151. const result = await adminClient.query<
  1152. CreateCollectionSelectVariants.Mutation,
  1153. CreateCollectionSelectVariants.Variables
  1154. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  1155. input: {
  1156. parentId: computersCollection.id,
  1157. translations: [
  1158. {
  1159. languageCode: LanguageCode.en,
  1160. name: 'Bell OR Pear Computers',
  1161. description: '',
  1162. slug: 'bell-or-pear',
  1163. },
  1164. ],
  1165. filters: [
  1166. {
  1167. code: facetValueCollectionFilter.code,
  1168. arguments: [
  1169. {
  1170. name: 'facetValueIds',
  1171. value: `["${getFacetValueId('pear')}", "${getFacetValueId('bell')}"]`,
  1172. },
  1173. {
  1174. name: 'containsAny',
  1175. value: `true`,
  1176. },
  1177. ],
  1178. },
  1179. ],
  1180. } as CreateCollectionInput,
  1181. });
  1182. await awaitRunningJobs(adminClient, 5000);
  1183. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  1184. GET_COLLECTION,
  1185. {
  1186. id: result.createCollection.id,
  1187. },
  1188. );
  1189. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  1190. 'Laptop 13 inch 8GB',
  1191. 'Laptop 15 inch 8GB',
  1192. 'Laptop 13 inch 16GB',
  1193. 'Laptop 15 inch 16GB',
  1194. 'Curvy Monitor 24 inch',
  1195. 'Curvy Monitor 27 inch',
  1196. ]);
  1197. });
  1198. });
  1199. describe('variantName filter', () => {
  1200. async function createVariantNameFilteredCollection(
  1201. operator: string,
  1202. term: string,
  1203. parentId?: string,
  1204. ): Promise<Collection.Fragment> {
  1205. const { createCollection } = await adminClient.query<
  1206. CreateCollection.Mutation,
  1207. CreateCollection.Variables
  1208. >(CREATE_COLLECTION, {
  1209. input: {
  1210. parentId,
  1211. translations: [
  1212. {
  1213. languageCode: LanguageCode.en,
  1214. name: `${operator} ${term}`,
  1215. description: '',
  1216. slug: `${operator} ${term}`,
  1217. },
  1218. ],
  1219. filters: [
  1220. {
  1221. code: variantNameCollectionFilter.code,
  1222. arguments: [
  1223. {
  1224. name: 'operator',
  1225. value: operator,
  1226. },
  1227. {
  1228. name: 'term',
  1229. value: term,
  1230. },
  1231. ],
  1232. },
  1233. ],
  1234. },
  1235. });
  1236. await awaitRunningJobs(adminClient, 5000);
  1237. return createCollection;
  1238. }
  1239. it('contains operator', async () => {
  1240. const collection = await createVariantNameFilteredCollection('contains', 'camera');
  1241. const result = await adminClient.query<
  1242. GetCollectionProducts.Query,
  1243. GetCollectionProducts.Variables
  1244. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1245. id: collection.id,
  1246. });
  1247. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1248. 'Instant Camera',
  1249. 'Camera Lens',
  1250. 'SLR Camera',
  1251. ]);
  1252. });
  1253. it('startsWith operator', async () => {
  1254. const collection = await createVariantNameFilteredCollection('startsWith', 'camera');
  1255. const result = await adminClient.query<
  1256. GetCollectionProducts.Query,
  1257. GetCollectionProducts.Variables
  1258. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1259. id: collection.id,
  1260. });
  1261. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual(['Camera Lens']);
  1262. });
  1263. it('endsWith operator', async () => {
  1264. const collection = await createVariantNameFilteredCollection('endsWith', 'camera');
  1265. const result = await adminClient.query<
  1266. GetCollectionProducts.Query,
  1267. GetCollectionProducts.Variables
  1268. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1269. id: collection.id,
  1270. });
  1271. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1272. 'Instant Camera',
  1273. 'SLR Camera',
  1274. ]);
  1275. });
  1276. it('doesNotContain operator', async () => {
  1277. const collection = await createVariantNameFilteredCollection('doesNotContain', 'camera');
  1278. const result = await adminClient.query<
  1279. GetCollectionProducts.Query,
  1280. GetCollectionProducts.Variables
  1281. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1282. id: collection.id,
  1283. });
  1284. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1285. 'Laptop 13 inch 8GB',
  1286. 'Laptop 15 inch 8GB',
  1287. 'Laptop 13 inch 16GB',
  1288. 'Laptop 15 inch 16GB',
  1289. 'Curvy Monitor 24 inch',
  1290. 'Curvy Monitor 27 inch',
  1291. 'Gaming PC i7-8700 240GB SSD',
  1292. 'Gaming PC R7-2700 240GB SSD',
  1293. 'Gaming PC i7-8700 120GB SSD',
  1294. 'Gaming PC R7-2700 120GB SSD',
  1295. 'Hard Drive 1TB',
  1296. 'Hard Drive 2TB',
  1297. 'Hard Drive 3TB',
  1298. 'Hard Drive 4TB',
  1299. 'Hard Drive 6TB',
  1300. 'Clacky Keyboard',
  1301. 'USB Cable',
  1302. 'Tripod',
  1303. 'Hat',
  1304. ]);
  1305. });
  1306. // https://github.com/vendure-ecommerce/vendure/issues/927
  1307. it('nested variantName filter', async () => {
  1308. const parent = await createVariantNameFilteredCollection('contains', 'lap');
  1309. const parentResult = await adminClient.query<
  1310. GetCollectionProducts.Query,
  1311. GetCollectionProducts.Variables
  1312. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1313. id: parent.id,
  1314. });
  1315. expect(parentResult.collection?.productVariants.items.map(i => i.name)).toEqual([
  1316. 'Laptop 13 inch 8GB',
  1317. 'Laptop 15 inch 8GB',
  1318. 'Laptop 13 inch 16GB',
  1319. 'Laptop 15 inch 16GB',
  1320. ]);
  1321. const child = await createVariantNameFilteredCollection('contains', 'GB', parent.id);
  1322. const childResult = await adminClient.query<
  1323. GetCollectionProducts.Query,
  1324. GetCollectionProducts.Variables
  1325. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1326. id: child.id,
  1327. });
  1328. expect(childResult.collection?.productVariants.items.map(i => i.name)).toEqual([
  1329. 'Laptop 13 inch 8GB',
  1330. 'Laptop 15 inch 8GB',
  1331. 'Laptop 13 inch 16GB',
  1332. 'Laptop 15 inch 16GB',
  1333. ]);
  1334. });
  1335. });
  1336. describe('re-evaluation of contents on changes', () => {
  1337. let products: GetProductsWithVariantIds.Items[];
  1338. beforeAll(async () => {
  1339. const result = await adminClient.query<GetProductsWithVariantIds.Query>(gql`
  1340. query GetProductsWithVariantIds {
  1341. products(options: { sort: { id: ASC } }) {
  1342. items {
  1343. id
  1344. name
  1345. variants {
  1346. id
  1347. name
  1348. }
  1349. }
  1350. }
  1351. }
  1352. `);
  1353. products = result.products.items;
  1354. });
  1355. it('updates contents when Product is updated', async () => {
  1356. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  1357. input: {
  1358. id: products[1].id,
  1359. facetValueIds: [
  1360. getFacetValueId('electronics'),
  1361. getFacetValueId('computers'),
  1362. getFacetValueId('pear'),
  1363. ],
  1364. },
  1365. });
  1366. await awaitRunningJobs(adminClient, 5000);
  1367. const result = await adminClient.query<
  1368. GetCollectionProducts.Query,
  1369. GetCollectionProducts.Variables
  1370. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  1371. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1372. 'Laptop 13 inch 8GB',
  1373. 'Laptop 15 inch 8GB',
  1374. 'Laptop 13 inch 16GB',
  1375. 'Laptop 15 inch 16GB',
  1376. 'Curvy Monitor 24 inch',
  1377. 'Curvy Monitor 27 inch',
  1378. 'Instant Camera',
  1379. ]);
  1380. });
  1381. it('updates contents when ProductVariant is updated', async () => {
  1382. const gamingPc240GB = products
  1383. .find(p => p.name === 'Gaming PC')!
  1384. .variants.find(v => v.name.includes('240GB'))!;
  1385. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1386. UPDATE_PRODUCT_VARIANTS,
  1387. {
  1388. input: [
  1389. {
  1390. id: gamingPc240GB.id,
  1391. facetValueIds: [getFacetValueId('pear')],
  1392. },
  1393. ],
  1394. },
  1395. );
  1396. await awaitRunningJobs(adminClient, 5000);
  1397. const result = await adminClient.query<
  1398. GetCollectionProducts.Query,
  1399. GetCollectionProducts.Variables
  1400. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  1401. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1402. 'Laptop 13 inch 8GB',
  1403. 'Laptop 15 inch 8GB',
  1404. 'Laptop 13 inch 16GB',
  1405. 'Laptop 15 inch 16GB',
  1406. 'Curvy Monitor 24 inch',
  1407. 'Curvy Monitor 27 inch',
  1408. 'Gaming PC i7-8700 240GB SSD',
  1409. 'Instant Camera',
  1410. ]);
  1411. });
  1412. it('correctly filters when ProductVariant and Product both have matching FacetValue', async () => {
  1413. const gamingPc240GB = products
  1414. .find(p => p.name === 'Gaming PC')!
  1415. .variants.find(v => v.name.includes('240GB'))!;
  1416. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1417. UPDATE_PRODUCT_VARIANTS,
  1418. {
  1419. input: [
  1420. {
  1421. id: gamingPc240GB.id,
  1422. facetValueIds: [getFacetValueId('electronics'), getFacetValueId('pear')],
  1423. },
  1424. ],
  1425. },
  1426. );
  1427. await awaitRunningJobs(adminClient, 5000);
  1428. const result = await adminClient.query<
  1429. GetCollectionProducts.Query,
  1430. GetCollectionProducts.Variables
  1431. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  1432. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1433. 'Laptop 13 inch 8GB',
  1434. 'Laptop 15 inch 8GB',
  1435. 'Laptop 13 inch 16GB',
  1436. 'Laptop 15 inch 16GB',
  1437. 'Curvy Monitor 24 inch',
  1438. 'Curvy Monitor 27 inch',
  1439. 'Gaming PC i7-8700 240GB SSD',
  1440. 'Instant Camera',
  1441. ]);
  1442. });
  1443. });
  1444. it('filter inheritance of nested collections (issue #158)', async () => {
  1445. const { createCollection: pearElectronics } = await adminClient.query<
  1446. CreateCollectionSelectVariants.Mutation,
  1447. CreateCollectionSelectVariants.Variables
  1448. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  1449. input: {
  1450. parentId: electronicsCollection.id,
  1451. translations: [
  1452. {
  1453. languageCode: LanguageCode.en,
  1454. name: 'pear electronics',
  1455. description: '',
  1456. slug: 'pear-electronics',
  1457. },
  1458. ],
  1459. filters: [
  1460. {
  1461. code: facetValueCollectionFilter.code,
  1462. arguments: [
  1463. {
  1464. name: 'facetValueIds',
  1465. value: `["${getFacetValueId('pear')}"]`,
  1466. },
  1467. {
  1468. name: 'containsAny',
  1469. value: `false`,
  1470. },
  1471. ],
  1472. },
  1473. ],
  1474. } as CreateCollectionInput,
  1475. });
  1476. await awaitRunningJobs(adminClient, 5000);
  1477. const result = await adminClient.query<
  1478. GetCollectionProducts.Query,
  1479. GetCollectionProducts.Variables
  1480. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearElectronics.id });
  1481. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1482. 'Laptop 13 inch 8GB',
  1483. 'Laptop 15 inch 8GB',
  1484. 'Laptop 13 inch 16GB',
  1485. 'Laptop 15 inch 16GB',
  1486. 'Curvy Monitor 24 inch',
  1487. 'Curvy Monitor 27 inch',
  1488. 'Gaming PC i7-8700 240GB SSD',
  1489. 'Instant Camera',
  1490. // no "Hat"
  1491. ]);
  1492. });
  1493. describe('previewCollectionVariants', () => {
  1494. it('returns correct contents', async () => {
  1495. const { previewCollectionVariants } = await adminClient.query<
  1496. PreviewCollectionVariantsQuery,
  1497. PreviewCollectionVariantsQueryVariables
  1498. >(PREVIEW_COLLECTION_VARIANTS, {
  1499. input: {
  1500. parentId: electronicsCollection.parent?.id,
  1501. filters: [
  1502. {
  1503. code: facetValueCollectionFilter.code,
  1504. arguments: [
  1505. {
  1506. name: 'facetValueIds',
  1507. value: `["${getFacetValueId('electronics')}","${getFacetValueId(
  1508. 'pear',
  1509. )}"]`,
  1510. },
  1511. {
  1512. name: 'containsAny',
  1513. value: `false`,
  1514. },
  1515. ],
  1516. },
  1517. ],
  1518. },
  1519. });
  1520. expect(previewCollectionVariants.items.map(i => i.name).sort()).toEqual([
  1521. 'Curvy Monitor 24 inch',
  1522. 'Curvy Monitor 27 inch',
  1523. 'Gaming PC i7-8700 240GB SSD',
  1524. 'Instant Camera',
  1525. 'Laptop 13 inch 16GB',
  1526. 'Laptop 13 inch 8GB',
  1527. 'Laptop 15 inch 16GB',
  1528. 'Laptop 15 inch 8GB',
  1529. ]);
  1530. });
  1531. it('works with list options', async () => {
  1532. const { previewCollectionVariants } = await adminClient.query<
  1533. PreviewCollectionVariantsQuery,
  1534. PreviewCollectionVariantsQueryVariables
  1535. >(PREVIEW_COLLECTION_VARIANTS, {
  1536. input: {
  1537. parentId: electronicsCollection.parent?.id,
  1538. filters: [
  1539. {
  1540. code: facetValueCollectionFilter.code,
  1541. arguments: [
  1542. {
  1543. name: 'facetValueIds',
  1544. value: `["${getFacetValueId('electronics')}"]`,
  1545. },
  1546. {
  1547. name: 'containsAny',
  1548. value: `false`,
  1549. },
  1550. ],
  1551. },
  1552. ],
  1553. },
  1554. options: {
  1555. sort: {
  1556. name: SortOrder.ASC,
  1557. },
  1558. filter: {
  1559. name: {
  1560. contains: 'mon',
  1561. },
  1562. },
  1563. take: 5,
  1564. },
  1565. });
  1566. expect(previewCollectionVariants.items).toEqual([
  1567. { id: 'T_5', name: 'Curvy Monitor 24 inch' },
  1568. { id: 'T_6', name: 'Curvy Monitor 27 inch' },
  1569. ]);
  1570. });
  1571. it('takes parent filters into account', async () => {
  1572. const { previewCollectionVariants } = await adminClient.query<
  1573. PreviewCollectionVariantsQuery,
  1574. PreviewCollectionVariantsQueryVariables
  1575. >(PREVIEW_COLLECTION_VARIANTS, {
  1576. input: {
  1577. parentId: electronicsCollection.id,
  1578. filters: [
  1579. {
  1580. code: variantNameCollectionFilter.code,
  1581. arguments: [
  1582. {
  1583. name: 'operator',
  1584. value: 'startsWith',
  1585. },
  1586. {
  1587. name: 'term',
  1588. value: 'h',
  1589. },
  1590. ],
  1591. },
  1592. ],
  1593. },
  1594. });
  1595. expect(previewCollectionVariants.items.map(i => i.name).sort()).toEqual([
  1596. 'Hard Drive 1TB',
  1597. 'Hard Drive 2TB',
  1598. 'Hard Drive 3TB',
  1599. 'Hard Drive 4TB',
  1600. 'Hard Drive 6TB',
  1601. ]);
  1602. });
  1603. it('with no parentId, operates at the root level', async () => {
  1604. const { previewCollectionVariants } = await adminClient.query<
  1605. PreviewCollectionVariantsQuery,
  1606. PreviewCollectionVariantsQueryVariables
  1607. >(PREVIEW_COLLECTION_VARIANTS, {
  1608. input: {
  1609. filters: [
  1610. {
  1611. code: variantNameCollectionFilter.code,
  1612. arguments: [
  1613. {
  1614. name: 'operator',
  1615. value: 'startsWith',
  1616. },
  1617. {
  1618. name: 'term',
  1619. value: 'h',
  1620. },
  1621. ],
  1622. },
  1623. ],
  1624. },
  1625. });
  1626. expect(previewCollectionVariants.items.map(i => i.name).sort()).toEqual([
  1627. 'Hard Drive 1TB',
  1628. 'Hard Drive 2TB',
  1629. 'Hard Drive 3TB',
  1630. 'Hard Drive 4TB',
  1631. 'Hard Drive 6TB',
  1632. 'Hat',
  1633. ]);
  1634. });
  1635. });
  1636. });
  1637. describe('Product collections property', () => {
  1638. it('returns all collections to which the Product belongs', async () => {
  1639. const result = await adminClient.query<
  1640. GetCollectionsForProducts.Query,
  1641. GetCollectionsForProducts.Variables
  1642. >(GET_COLLECTIONS_FOR_PRODUCTS, { term: 'camera' });
  1643. expect(result.products.items[0].collections).toEqual([
  1644. {
  1645. id: 'T_3',
  1646. name: 'Electronics',
  1647. },
  1648. {
  1649. id: 'T_5',
  1650. name: 'Pear',
  1651. },
  1652. {
  1653. id: 'T_9',
  1654. name: 'Electronics Breadcrumbs',
  1655. },
  1656. {
  1657. id: 'T_14',
  1658. name: 'Photo AND Pear',
  1659. },
  1660. {
  1661. id: 'T_15',
  1662. name: 'Photo OR Pear',
  1663. },
  1664. {
  1665. id: 'T_17',
  1666. name: 'contains camera',
  1667. },
  1668. {
  1669. id: 'T_19',
  1670. name: 'endsWith camera',
  1671. },
  1672. {
  1673. id: 'T_23',
  1674. name: 'pear electronics',
  1675. },
  1676. ]);
  1677. });
  1678. });
  1679. describe('productVariants list', () => {
  1680. it('does not list variants from deleted products', async () => {
  1681. await adminClient.query<DeleteProduct.Mutation, DeleteProduct.Variables>(DELETE_PRODUCT, {
  1682. id: 'T_2', // curvy monitor
  1683. });
  1684. await awaitRunningJobs(adminClient, 5000);
  1685. const { collection } = await adminClient.query<
  1686. GetCollectionProducts.Query,
  1687. GetCollectionProducts.Variables
  1688. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1689. id: pearCollection.id,
  1690. });
  1691. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  1692. 'Laptop 13 inch 8GB',
  1693. 'Laptop 15 inch 8GB',
  1694. 'Laptop 13 inch 16GB',
  1695. 'Laptop 15 inch 16GB',
  1696. 'Gaming PC i7-8700 240GB SSD',
  1697. 'Instant Camera',
  1698. ]);
  1699. });
  1700. // https://github.com/vendure-ecommerce/vendure/issues/1213
  1701. it('does not list deleted variants', async () => {
  1702. await adminClient.query<DeleteProductVariant.Mutation, DeleteProductVariant.Variables>(
  1703. DELETE_PRODUCT_VARIANT,
  1704. {
  1705. id: 'T_18', // Instant Camera
  1706. },
  1707. );
  1708. await awaitRunningJobs(adminClient, 5000);
  1709. const { collection } = await adminClient.query<
  1710. GetCollectionProducts.Query,
  1711. GetCollectionProducts.Variables
  1712. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1713. id: pearCollection.id,
  1714. });
  1715. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  1716. 'Laptop 13 inch 8GB',
  1717. 'Laptop 15 inch 8GB',
  1718. 'Laptop 13 inch 16GB',
  1719. 'Laptop 15 inch 16GB',
  1720. 'Gaming PC i7-8700 240GB SSD',
  1721. // 'Instant Camera',
  1722. ]);
  1723. });
  1724. it('does not list disabled variants in Shop API', async () => {
  1725. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1726. UPDATE_PRODUCT_VARIANTS,
  1727. {
  1728. input: [{ id: 'T_1', enabled: false }],
  1729. },
  1730. );
  1731. await awaitRunningJobs(adminClient, 5000);
  1732. const { collection } = await shopClient.query<
  1733. GetCollectionProducts.Query,
  1734. GetCollectionProducts.Variables
  1735. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1736. id: pearCollection.id,
  1737. });
  1738. expect(collection!.productVariants.items.map(i => i.id).includes('T_1')).toBe(false);
  1739. });
  1740. it('does not list variants of disabled products in Shop API', async () => {
  1741. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  1742. input: { id: 'T_1', enabled: false },
  1743. });
  1744. await awaitRunningJobs(adminClient, 5000);
  1745. const { collection } = await shopClient.query<
  1746. GetCollectionProducts.Query,
  1747. GetCollectionProducts.Variables
  1748. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1749. id: pearCollection.id,
  1750. });
  1751. expect(collection!.productVariants.items.map(i => i.id).includes('T_1')).toBe(false);
  1752. expect(collection!.productVariants.items.map(i => i.id).includes('T_2')).toBe(false);
  1753. expect(collection!.productVariants.items.map(i => i.id).includes('T_3')).toBe(false);
  1754. expect(collection!.productVariants.items.map(i => i.id).includes('T_4')).toBe(false);
  1755. });
  1756. it('handles other languages', async () => {
  1757. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  1758. input: { id: 'T_1', enabled: true },
  1759. });
  1760. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1761. UPDATE_PRODUCT_VARIANTS,
  1762. {
  1763. input: [
  1764. {
  1765. id: 'T_2',
  1766. translations: [{ languageCode: LanguageCode.de, name: 'Taschenrechner 15 Zoll' }],
  1767. },
  1768. ],
  1769. },
  1770. );
  1771. const { collection } = await shopClient.query<
  1772. GetCollectionProducts.Query,
  1773. GetCollectionProducts.Variables
  1774. >(
  1775. GET_COLLECTION_PRODUCT_VARIANTS,
  1776. {
  1777. id: pearCollection.id,
  1778. },
  1779. { languageCode: LanguageCode.de },
  1780. );
  1781. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  1782. 'Taschenrechner 15 Zoll',
  1783. 'Laptop 13 inch 16GB',
  1784. 'Laptop 15 inch 16GB',
  1785. 'Gaming PC i7-8700 240GB SSD',
  1786. ]);
  1787. });
  1788. });
  1789. function getFacetValueId(code: string): string {
  1790. const match = facetValues.find(fv => fv.code === code);
  1791. if (!match) {
  1792. throw new Error(`Could not find a FacetValue with the code "${code}"`);
  1793. }
  1794. return match.id;
  1795. }
  1796. });
  1797. export const GET_COLLECTION = gql`
  1798. query GetCollection($id: ID, $slug: String, $variantListOptions: ProductVariantListOptions) {
  1799. collection(id: $id, slug: $slug) {
  1800. ...Collection
  1801. productVariants(options: $variantListOptions) {
  1802. items {
  1803. id
  1804. name
  1805. price
  1806. }
  1807. }
  1808. }
  1809. }
  1810. ${COLLECTION_FRAGMENT}
  1811. `;
  1812. export const MOVE_COLLECTION = gql`
  1813. mutation MoveCollection($input: MoveCollectionInput!) {
  1814. moveCollection(input: $input) {
  1815. ...Collection
  1816. }
  1817. }
  1818. ${COLLECTION_FRAGMENT}
  1819. `;
  1820. const GET_FACET_VALUES = gql`
  1821. query GetFacetValues {
  1822. facets {
  1823. items {
  1824. values {
  1825. ...FacetValue
  1826. }
  1827. }
  1828. }
  1829. }
  1830. ${FACET_VALUE_FRAGMENT}
  1831. `;
  1832. const GET_COLLECTION_PRODUCT_VARIANTS = gql`
  1833. query GetCollectionProducts($id: ID!) {
  1834. collection(id: $id) {
  1835. productVariants(options: { sort: { id: ASC } }) {
  1836. items {
  1837. id
  1838. name
  1839. facetValues {
  1840. code
  1841. }
  1842. productId
  1843. }
  1844. }
  1845. }
  1846. }
  1847. `;
  1848. const CREATE_COLLECTION_SELECT_VARIANTS = gql`
  1849. mutation CreateCollectionSelectVariants($input: CreateCollectionInput!) {
  1850. createCollection(input: $input) {
  1851. id
  1852. productVariants {
  1853. items {
  1854. name
  1855. }
  1856. totalItems
  1857. }
  1858. }
  1859. }
  1860. `;
  1861. const GET_COLLECTION_BREADCRUMBS = gql`
  1862. query GetCollectionBreadcrumbs($id: ID!) {
  1863. collection(id: $id) {
  1864. breadcrumbs {
  1865. id
  1866. name
  1867. slug
  1868. }
  1869. }
  1870. }
  1871. `;
  1872. const GET_COLLECTIONS_FOR_PRODUCTS = gql`
  1873. query GetCollectionsForProducts($term: String!) {
  1874. products(options: { filter: { name: { contains: $term } } }) {
  1875. items {
  1876. id
  1877. name
  1878. collections {
  1879. id
  1880. name
  1881. }
  1882. }
  1883. }
  1884. }
  1885. `;
  1886. const DELETE_COLLECTION = gql`
  1887. mutation DeleteCollection($id: ID!) {
  1888. deleteCollection(id: $id) {
  1889. result
  1890. message
  1891. }
  1892. }
  1893. `;
  1894. const GET_PRODUCT_COLLECTIONS = gql`
  1895. query GetProductCollections($id: ID!) {
  1896. product(id: $id) {
  1897. id
  1898. collections {
  1899. id
  1900. name
  1901. }
  1902. }
  1903. }
  1904. `;
  1905. const GET_PRODUCT_COLLECTIONS_WITH_PARENT = gql`
  1906. query GetProductCollectionsWithParent($id: ID!) {
  1907. product(id: $id) {
  1908. id
  1909. collections {
  1910. id
  1911. name
  1912. parent {
  1913. id
  1914. name
  1915. }
  1916. }
  1917. }
  1918. }
  1919. `;
  1920. const GET_COLLECTION_NESTED_PARENTS = gql`
  1921. query GetCollectionNestedParents {
  1922. collections {
  1923. items {
  1924. id
  1925. name
  1926. parent {
  1927. name
  1928. parent {
  1929. name
  1930. parent {
  1931. name
  1932. }
  1933. }
  1934. }
  1935. }
  1936. }
  1937. }
  1938. `;
  1939. const PREVIEW_COLLECTION_VARIANTS = gql`
  1940. query PreviewCollectionVariants(
  1941. $input: PreviewCollectionVariantsInput!
  1942. $options: ProductVariantListOptions
  1943. ) {
  1944. previewCollectionVariants(input: $input, options: $options) {
  1945. items {
  1946. id
  1947. name
  1948. }
  1949. totalItems
  1950. }
  1951. }
  1952. `;