collection.e2e-spec.ts 70 KB

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