collection.e2e-spec.ts 90 KB

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