collection.e2e-spec.ts 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. /* tslint:disable:no-non-null-assertion */
  2. import { ROOT_COLLECTION_NAME } from '@vendure/common/lib/shared-constants';
  3. import {
  4. DefaultJobQueuePlugin,
  5. facetValueCollectionFilter,
  6. variantNameCollectionFilter,
  7. } from '@vendure/core';
  8. import { createTestEnvironment } from '@vendure/testing';
  9. import gql from 'graphql-tag';
  10. import path from 'path';
  11. import { initialData } from '../../../e2e-common/e2e-initial-data';
  12. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  13. import { pick } from '../../common/lib/pick';
  14. import { COLLECTION_FRAGMENT, FACET_VALUE_FRAGMENT } from './graphql/fragments';
  15. import {
  16. Collection,
  17. CreateCollection,
  18. CreateCollectionInput,
  19. CreateCollectionSelectVariants,
  20. DeleteCollection,
  21. DeleteProduct,
  22. DeletionResult,
  23. FacetValueFragment,
  24. GetAssetList,
  25. GetCollection,
  26. GetCollectionBreadcrumbs,
  27. GetCollectionNestedParents,
  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, 5000);
  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, 5000);
  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, 5000);
  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. // https://github.com/vendure-ecommerce/vendure/issues/981
  554. it('nested parent field in shop API', async () => {
  555. const { collections } = await shopClient.query<GetCollectionNestedParents.Query>(
  556. GET_COLLECTION_NESTED_PARENTS,
  557. );
  558. expect(collections.items[0].parent?.name).toBe(ROOT_COLLECTION_NAME);
  559. });
  560. it('children field', async () => {
  561. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  562. GET_COLLECTION,
  563. {
  564. id: electronicsCollection.id,
  565. },
  566. );
  567. if (!result.collection) {
  568. fail(`did not return the collection`);
  569. return;
  570. }
  571. expect(result.collection.children!.length).toBe(1);
  572. expect(result.collection.children![0].name).toBe('Computers');
  573. });
  574. it('breadcrumbs', async () => {
  575. const result = await adminClient.query<
  576. GetCollectionBreadcrumbs.Query,
  577. GetCollectionBreadcrumbs.Variables
  578. >(GET_COLLECTION_BREADCRUMBS, {
  579. id: pearCollection.id,
  580. });
  581. if (!result.collection) {
  582. fail(`did not return the collection`);
  583. return;
  584. }
  585. expect(result.collection.breadcrumbs).toEqual([
  586. { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
  587. {
  588. id: electronicsCollection.id,
  589. name: electronicsCollection.name,
  590. slug: electronicsCollection.slug,
  591. },
  592. {
  593. id: computersCollection.id,
  594. name: computersCollection.name,
  595. slug: computersCollection.slug,
  596. },
  597. { id: pearCollection.id, name: pearCollection.name, slug: pearCollection.slug },
  598. ]);
  599. });
  600. it('breadcrumbs for root collection', async () => {
  601. const result = await adminClient.query<
  602. GetCollectionBreadcrumbs.Query,
  603. GetCollectionBreadcrumbs.Variables
  604. >(GET_COLLECTION_BREADCRUMBS, {
  605. id: 'T_1',
  606. });
  607. if (!result.collection) {
  608. fail(`did not return the collection`);
  609. return;
  610. }
  611. expect(result.collection.breadcrumbs).toEqual([
  612. { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
  613. ]);
  614. });
  615. it('collections.assets', async () => {
  616. const { collections } = await adminClient.query<GetCollectionsWithAssets.Query>(gql`
  617. query GetCollectionsWithAssets {
  618. collections {
  619. items {
  620. assets {
  621. name
  622. }
  623. }
  624. }
  625. }
  626. `);
  627. expect(collections.items[0].assets).toBeDefined();
  628. });
  629. // https://github.com/vendure-ecommerce/vendure/issues/642
  630. it('sorting on Collection.productVariants.price', async () => {
  631. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  632. GET_COLLECTION,
  633. {
  634. id: computersCollection.id,
  635. variantListOptions: {
  636. sort: {
  637. price: SortOrder.ASC,
  638. },
  639. },
  640. },
  641. );
  642. expect(collection!.productVariants.items.map(i => i.price)).toEqual([
  643. 3799,
  644. 5374,
  645. 6900,
  646. 7489,
  647. 7896,
  648. 9299,
  649. 13435,
  650. 14374,
  651. 16994,
  652. 93120,
  653. 94920,
  654. 108720,
  655. 109995,
  656. 129900,
  657. 139900,
  658. 219900,
  659. 229900,
  660. ]);
  661. });
  662. });
  663. describe('moveCollection', () => {
  664. it('moves a collection to a new parent', async () => {
  665. const result = await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(
  666. MOVE_COLLECTION,
  667. {
  668. input: {
  669. collectionId: pearCollection.id,
  670. parentId: electronicsCollection.id,
  671. index: 0,
  672. },
  673. },
  674. );
  675. expect(result.moveCollection.parent!.id).toBe(electronicsCollection.id);
  676. const positions = await getChildrenOf(electronicsCollection.id);
  677. expect(positions.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  678. });
  679. it('re-evaluates Collection contents on move', async () => {
  680. await awaitRunningJobs(adminClient, 5000);
  681. const result = await adminClient.query<
  682. GetCollectionProducts.Query,
  683. GetCollectionProducts.Variables
  684. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  685. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  686. 'Laptop 13 inch 8GB',
  687. 'Laptop 15 inch 8GB',
  688. 'Laptop 13 inch 16GB',
  689. 'Laptop 15 inch 16GB',
  690. 'Instant Camera',
  691. ]);
  692. });
  693. it('moves a 1st level collection to a new parent to check breadcrumbs', async () => {
  694. const result = await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(
  695. MOVE_COLLECTION,
  696. {
  697. input: {
  698. collectionId: computersBreadcrumbsCollection.id,
  699. parentId: electronicsBreadcrumbsCollection.id,
  700. index: 0,
  701. },
  702. },
  703. );
  704. expect(result.moveCollection.parent!.id).toBe(electronicsBreadcrumbsCollection.id);
  705. const positions = await getChildrenOf(electronicsBreadcrumbsCollection.id);
  706. expect(positions.map(i => i.id)).toEqual([computersBreadcrumbsCollection.id]);
  707. });
  708. it('breadcrumbs for collection with ids out of order', async () => {
  709. const result = await adminClient.query<
  710. GetCollectionBreadcrumbs.Query,
  711. GetCollectionBreadcrumbs.Variables
  712. >(GET_COLLECTION_BREADCRUMBS, {
  713. id: pearBreadcrumbsCollection.id,
  714. });
  715. if (!result.collection) {
  716. fail(`did not return the collection`);
  717. return;
  718. }
  719. expect(result.collection.breadcrumbs).toEqual([
  720. { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
  721. {
  722. id: electronicsBreadcrumbsCollection.id,
  723. name: electronicsBreadcrumbsCollection.name,
  724. slug: electronicsBreadcrumbsCollection.slug,
  725. },
  726. {
  727. id: computersBreadcrumbsCollection.id,
  728. name: computersBreadcrumbsCollection.name,
  729. slug: computersBreadcrumbsCollection.slug,
  730. },
  731. {
  732. id: pearBreadcrumbsCollection.id,
  733. name: pearBreadcrumbsCollection.name,
  734. slug: pearBreadcrumbsCollection.slug,
  735. },
  736. ]);
  737. });
  738. it('alters the position in the current parent 1', async () => {
  739. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  740. input: {
  741. collectionId: computersCollection.id,
  742. parentId: electronicsCollection.id,
  743. index: 0,
  744. },
  745. });
  746. const afterResult = await getChildrenOf(electronicsCollection.id);
  747. expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
  748. });
  749. it('alters the position in the current parent 2', async () => {
  750. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  751. input: {
  752. collectionId: pearCollection.id,
  753. parentId: electronicsCollection.id,
  754. index: 0,
  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 negative index value', async () => {
  761. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  762. input: {
  763. collectionId: pearCollection.id,
  764. parentId: electronicsCollection.id,
  765. index: -3,
  766. },
  767. });
  768. const afterResult = await getChildrenOf(electronicsCollection.id);
  769. expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  770. });
  771. it('corrects an out-of-bounds positive index value', async () => {
  772. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  773. input: {
  774. collectionId: pearCollection.id,
  775. parentId: electronicsCollection.id,
  776. index: 10,
  777. },
  778. });
  779. const afterResult = await getChildrenOf(electronicsCollection.id);
  780. expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
  781. });
  782. it(
  783. 'throws if attempting to move into self',
  784. assertThrowsWithMessage(
  785. () =>
  786. adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  787. input: {
  788. collectionId: pearCollection.id,
  789. parentId: pearCollection.id,
  790. index: 0,
  791. },
  792. }),
  793. `Cannot move a Collection into itself`,
  794. ),
  795. );
  796. it(
  797. 'throws if attempting to move into a descendant of self',
  798. assertThrowsWithMessage(
  799. () =>
  800. adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  801. input: {
  802. collectionId: pearCollection.id,
  803. parentId: pearCollection.id,
  804. index: 0,
  805. },
  806. }),
  807. `Cannot move a Collection into itself`,
  808. ),
  809. );
  810. async function getChildrenOf(parentId: string): Promise<Array<{ name: string; id: string }>> {
  811. const result = await adminClient.query<GetCollections.Query>(GET_COLLECTIONS);
  812. return result.collections.items.filter(i => i.parent!.id === parentId);
  813. }
  814. });
  815. describe('deleteCollection', () => {
  816. let collectionToDeleteParent: CreateCollection.CreateCollection;
  817. let collectionToDeleteChild: CreateCollection.CreateCollection;
  818. let laptopProductId: string;
  819. beforeAll(async () => {
  820. const result1 = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  821. CREATE_COLLECTION,
  822. {
  823. input: {
  824. filters: [
  825. {
  826. code: variantNameCollectionFilter.code,
  827. arguments: [
  828. {
  829. name: 'operator',
  830. value: 'contains',
  831. },
  832. {
  833. name: 'term',
  834. value: 'laptop',
  835. },
  836. ],
  837. },
  838. ],
  839. translations: [
  840. {
  841. languageCode: LanguageCode.en,
  842. name: 'Delete Me Parent',
  843. description: '',
  844. slug: 'delete-me-parent',
  845. },
  846. ],
  847. assetIds: ['T_1'],
  848. },
  849. },
  850. );
  851. collectionToDeleteParent = result1.createCollection;
  852. const result2 = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  853. CREATE_COLLECTION,
  854. {
  855. input: {
  856. filters: [],
  857. translations: [
  858. {
  859. languageCode: LanguageCode.en,
  860. name: 'Delete Me Child',
  861. description: '',
  862. slug: 'delete-me-child',
  863. },
  864. ],
  865. parentId: collectionToDeleteParent.id,
  866. assetIds: ['T_2'],
  867. },
  868. },
  869. );
  870. collectionToDeleteChild = result2.createCollection;
  871. await awaitRunningJobs(adminClient, 5000);
  872. });
  873. it(
  874. 'throws for invalid collection id',
  875. assertThrowsWithMessage(async () => {
  876. await adminClient.query<DeleteCollection.Mutation, DeleteCollection.Variables>(
  877. DELETE_COLLECTION,
  878. {
  879. id: 'T_999',
  880. },
  881. );
  882. }, "No Collection with the id '999' could be found"),
  883. );
  884. it('collection and product related prior to deletion', async () => {
  885. const { collection } = await adminClient.query<
  886. GetCollectionProducts.Query,
  887. GetCollectionProducts.Variables
  888. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  889. id: collectionToDeleteParent.id,
  890. });
  891. expect(collection!.productVariants.items.map(pick(['name']))).toEqual([
  892. { name: 'Laptop 13 inch 8GB' },
  893. { name: 'Laptop 15 inch 8GB' },
  894. { name: 'Laptop 13 inch 16GB' },
  895. { name: 'Laptop 15 inch 16GB' },
  896. ]);
  897. laptopProductId = collection!.productVariants.items[0].productId;
  898. const { product } = await adminClient.query<
  899. GetProductCollections.Query,
  900. GetProductCollections.Variables
  901. >(GET_PRODUCT_COLLECTIONS, {
  902. id: laptopProductId,
  903. });
  904. expect(product!.collections).toEqual([
  905. {
  906. id: 'T_3',
  907. name: 'Electronics',
  908. },
  909. {
  910. id: 'T_4',
  911. name: 'Computers',
  912. },
  913. {
  914. id: 'T_5',
  915. name: 'Pear',
  916. },
  917. {
  918. id: 'T_8',
  919. name: 'Computers Breadcrumbs',
  920. },
  921. {
  922. id: 'T_9',
  923. name: 'Electronics Breadcrumbs',
  924. },
  925. {
  926. id: 'T_10',
  927. name: 'Pear Breadcrumbs',
  928. },
  929. {
  930. id: 'T_11',
  931. name: 'Delete Me Parent',
  932. },
  933. {
  934. id: 'T_12',
  935. name: 'Delete Me Child',
  936. },
  937. ]);
  938. });
  939. it('deleteCollection works', async () => {
  940. const { deleteCollection } = await adminClient.query<
  941. DeleteCollection.Mutation,
  942. DeleteCollection.Variables
  943. >(DELETE_COLLECTION, {
  944. id: collectionToDeleteParent.id,
  945. });
  946. expect(deleteCollection.result).toBe(DeletionResult.DELETED);
  947. });
  948. it('deleted parent collection is null', async () => {
  949. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  950. GET_COLLECTION,
  951. {
  952. id: collectionToDeleteParent.id,
  953. },
  954. );
  955. expect(collection).toBeNull();
  956. });
  957. it('deleted child collection is null', async () => {
  958. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  959. GET_COLLECTION,
  960. {
  961. id: collectionToDeleteChild.id,
  962. },
  963. );
  964. expect(collection).toBeNull();
  965. });
  966. it('product no longer lists collection', async () => {
  967. const { product } = await adminClient.query<
  968. GetProductCollections.Query,
  969. GetProductCollections.Variables
  970. >(GET_PRODUCT_COLLECTIONS, {
  971. id: laptopProductId,
  972. });
  973. expect(product!.collections).toEqual([
  974. { id: 'T_3', name: 'Electronics' },
  975. { id: 'T_4', name: 'Computers' },
  976. { id: 'T_5', name: 'Pear' },
  977. {
  978. id: 'T_8',
  979. name: 'Computers Breadcrumbs',
  980. },
  981. {
  982. id: 'T_9',
  983. name: 'Electronics Breadcrumbs',
  984. },
  985. {
  986. id: 'T_10',
  987. name: 'Pear Breadcrumbs',
  988. },
  989. ]);
  990. });
  991. });
  992. describe('filters', () => {
  993. it('Collection with no filters has no productVariants', async () => {
  994. const result = await adminClient.query<
  995. CreateCollectionSelectVariants.Mutation,
  996. CreateCollectionSelectVariants.Variables
  997. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  998. input: {
  999. translations: [
  1000. { languageCode: LanguageCode.en, name: 'Empty', description: '', slug: 'empty' },
  1001. ],
  1002. filters: [],
  1003. } as CreateCollectionInput,
  1004. });
  1005. expect(result.createCollection.productVariants.totalItems).toBe(0);
  1006. });
  1007. describe('facetValue filter', () => {
  1008. it('electronics', async () => {
  1009. const result = await adminClient.query<
  1010. GetCollectionProducts.Query,
  1011. GetCollectionProducts.Variables
  1012. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1013. id: electronicsCollection.id,
  1014. });
  1015. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1016. 'Laptop 13 inch 8GB',
  1017. 'Laptop 15 inch 8GB',
  1018. 'Laptop 13 inch 16GB',
  1019. 'Laptop 15 inch 16GB',
  1020. 'Curvy Monitor 24 inch',
  1021. 'Curvy Monitor 27 inch',
  1022. 'Gaming PC i7-8700 240GB SSD',
  1023. 'Gaming PC R7-2700 240GB SSD',
  1024. 'Gaming PC i7-8700 120GB SSD',
  1025. 'Gaming PC R7-2700 120GB SSD',
  1026. 'Hard Drive 1TB',
  1027. 'Hard Drive 2TB',
  1028. 'Hard Drive 3TB',
  1029. 'Hard Drive 4TB',
  1030. 'Hard Drive 6TB',
  1031. 'Clacky Keyboard',
  1032. 'USB Cable',
  1033. 'Instant Camera',
  1034. 'Camera Lens',
  1035. 'Tripod',
  1036. 'SLR Camera',
  1037. ]);
  1038. });
  1039. it('computers', async () => {
  1040. const result = await adminClient.query<
  1041. GetCollectionProducts.Query,
  1042. GetCollectionProducts.Variables
  1043. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1044. id: computersCollection.id,
  1045. });
  1046. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1047. 'Laptop 13 inch 8GB',
  1048. 'Laptop 15 inch 8GB',
  1049. 'Laptop 13 inch 16GB',
  1050. 'Laptop 15 inch 16GB',
  1051. 'Curvy Monitor 24 inch',
  1052. 'Curvy Monitor 27 inch',
  1053. 'Gaming PC i7-8700 240GB SSD',
  1054. 'Gaming PC R7-2700 240GB SSD',
  1055. 'Gaming PC i7-8700 120GB SSD',
  1056. 'Gaming PC R7-2700 120GB SSD',
  1057. 'Hard Drive 1TB',
  1058. 'Hard Drive 2TB',
  1059. 'Hard Drive 3TB',
  1060. 'Hard Drive 4TB',
  1061. 'Hard Drive 6TB',
  1062. 'Clacky Keyboard',
  1063. 'USB Cable',
  1064. ]);
  1065. });
  1066. it('photo AND pear', async () => {
  1067. const result = await adminClient.query<
  1068. CreateCollectionSelectVariants.Mutation,
  1069. CreateCollectionSelectVariants.Variables
  1070. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  1071. input: {
  1072. translations: [
  1073. {
  1074. languageCode: LanguageCode.en,
  1075. name: 'Photo AND Pear',
  1076. description: '',
  1077. slug: 'photo-and-pear',
  1078. },
  1079. ],
  1080. filters: [
  1081. {
  1082. code: facetValueCollectionFilter.code,
  1083. arguments: [
  1084. {
  1085. name: 'facetValueIds',
  1086. value: `["${getFacetValueId('pear')}", "${getFacetValueId(
  1087. 'photo',
  1088. )}"]`,
  1089. },
  1090. {
  1091. name: 'containsAny',
  1092. value: `false`,
  1093. },
  1094. ],
  1095. },
  1096. ],
  1097. } as CreateCollectionInput,
  1098. });
  1099. await awaitRunningJobs(adminClient, 5000);
  1100. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  1101. GET_COLLECTION,
  1102. {
  1103. id: result.createCollection.id,
  1104. },
  1105. );
  1106. expect(collection!.productVariants.items.map(i => i.name)).toEqual(['Instant Camera']);
  1107. });
  1108. it('photo OR pear', async () => {
  1109. const result = await adminClient.query<
  1110. CreateCollectionSelectVariants.Mutation,
  1111. CreateCollectionSelectVariants.Variables
  1112. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  1113. input: {
  1114. translations: [
  1115. {
  1116. languageCode: LanguageCode.en,
  1117. name: 'Photo OR Pear',
  1118. description: '',
  1119. slug: 'photo-or-pear',
  1120. },
  1121. ],
  1122. filters: [
  1123. {
  1124. code: facetValueCollectionFilter.code,
  1125. arguments: [
  1126. {
  1127. name: 'facetValueIds',
  1128. value: `["${getFacetValueId('pear')}", "${getFacetValueId(
  1129. 'photo',
  1130. )}"]`,
  1131. },
  1132. {
  1133. name: 'containsAny',
  1134. value: `true`,
  1135. },
  1136. ],
  1137. },
  1138. ],
  1139. } as CreateCollectionInput,
  1140. });
  1141. await awaitRunningJobs(adminClient, 5000);
  1142. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  1143. GET_COLLECTION,
  1144. {
  1145. id: result.createCollection.id,
  1146. },
  1147. );
  1148. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  1149. 'Laptop 13 inch 8GB',
  1150. 'Laptop 15 inch 8GB',
  1151. 'Laptop 13 inch 16GB',
  1152. 'Laptop 15 inch 16GB',
  1153. 'Instant Camera',
  1154. 'Camera Lens',
  1155. 'Tripod',
  1156. 'SLR Camera',
  1157. 'Hat',
  1158. ]);
  1159. });
  1160. it('bell OR pear in computers', async () => {
  1161. const result = await adminClient.query<
  1162. CreateCollectionSelectVariants.Mutation,
  1163. CreateCollectionSelectVariants.Variables
  1164. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  1165. input: {
  1166. parentId: computersCollection.id,
  1167. translations: [
  1168. {
  1169. languageCode: LanguageCode.en,
  1170. name: 'Bell OR Pear Computers',
  1171. description: '',
  1172. slug: 'bell-or-pear',
  1173. },
  1174. ],
  1175. filters: [
  1176. {
  1177. code: facetValueCollectionFilter.code,
  1178. arguments: [
  1179. {
  1180. name: 'facetValueIds',
  1181. value: `["${getFacetValueId('pear')}", "${getFacetValueId('bell')}"]`,
  1182. },
  1183. {
  1184. name: 'containsAny',
  1185. value: `true`,
  1186. },
  1187. ],
  1188. },
  1189. ],
  1190. } as CreateCollectionInput,
  1191. });
  1192. await awaitRunningJobs(adminClient, 5000);
  1193. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  1194. GET_COLLECTION,
  1195. {
  1196. id: result.createCollection.id,
  1197. },
  1198. );
  1199. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  1200. 'Laptop 13 inch 8GB',
  1201. 'Laptop 15 inch 8GB',
  1202. 'Laptop 13 inch 16GB',
  1203. 'Laptop 15 inch 16GB',
  1204. 'Curvy Monitor 24 inch',
  1205. 'Curvy Monitor 27 inch',
  1206. ]);
  1207. });
  1208. });
  1209. describe('variantName filter', () => {
  1210. async function createVariantNameFilteredCollection(
  1211. operator: string,
  1212. term: string,
  1213. parentId?: string,
  1214. ): Promise<Collection.Fragment> {
  1215. const { createCollection } = await adminClient.query<
  1216. CreateCollection.Mutation,
  1217. CreateCollection.Variables
  1218. >(CREATE_COLLECTION, {
  1219. input: {
  1220. parentId,
  1221. translations: [
  1222. {
  1223. languageCode: LanguageCode.en,
  1224. name: `${operator} ${term}`,
  1225. description: '',
  1226. slug: `${operator} ${term}`,
  1227. },
  1228. ],
  1229. filters: [
  1230. {
  1231. code: variantNameCollectionFilter.code,
  1232. arguments: [
  1233. {
  1234. name: 'operator',
  1235. value: operator,
  1236. },
  1237. {
  1238. name: 'term',
  1239. value: term,
  1240. },
  1241. ],
  1242. },
  1243. ],
  1244. },
  1245. });
  1246. await awaitRunningJobs(adminClient, 5000);
  1247. return createCollection;
  1248. }
  1249. it('contains operator', async () => {
  1250. const collection = await createVariantNameFilteredCollection('contains', 'camera');
  1251. const result = await adminClient.query<
  1252. GetCollectionProducts.Query,
  1253. GetCollectionProducts.Variables
  1254. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1255. id: collection.id,
  1256. });
  1257. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1258. 'Instant Camera',
  1259. 'Camera Lens',
  1260. 'SLR Camera',
  1261. ]);
  1262. });
  1263. it('startsWith operator', async () => {
  1264. const collection = await createVariantNameFilteredCollection('startsWith', 'camera');
  1265. const result = await adminClient.query<
  1266. GetCollectionProducts.Query,
  1267. GetCollectionProducts.Variables
  1268. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1269. id: collection.id,
  1270. });
  1271. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual(['Camera Lens']);
  1272. });
  1273. it('endsWith operator', async () => {
  1274. const collection = await createVariantNameFilteredCollection('endsWith', 'camera');
  1275. const result = await adminClient.query<
  1276. GetCollectionProducts.Query,
  1277. GetCollectionProducts.Variables
  1278. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1279. id: collection.id,
  1280. });
  1281. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1282. 'Instant Camera',
  1283. 'SLR Camera',
  1284. ]);
  1285. });
  1286. it('doesNotContain operator', async () => {
  1287. const collection = await createVariantNameFilteredCollection('doesNotContain', 'camera');
  1288. const result = await adminClient.query<
  1289. GetCollectionProducts.Query,
  1290. GetCollectionProducts.Variables
  1291. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1292. id: collection.id,
  1293. });
  1294. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1295. 'Laptop 13 inch 8GB',
  1296. 'Laptop 15 inch 8GB',
  1297. 'Laptop 13 inch 16GB',
  1298. 'Laptop 15 inch 16GB',
  1299. 'Curvy Monitor 24 inch',
  1300. 'Curvy Monitor 27 inch',
  1301. 'Gaming PC i7-8700 240GB SSD',
  1302. 'Gaming PC R7-2700 240GB SSD',
  1303. 'Gaming PC i7-8700 120GB SSD',
  1304. 'Gaming PC R7-2700 120GB SSD',
  1305. 'Hard Drive 1TB',
  1306. 'Hard Drive 2TB',
  1307. 'Hard Drive 3TB',
  1308. 'Hard Drive 4TB',
  1309. 'Hard Drive 6TB',
  1310. 'Clacky Keyboard',
  1311. 'USB Cable',
  1312. 'Tripod',
  1313. 'Hat',
  1314. ]);
  1315. });
  1316. // https://github.com/vendure-ecommerce/vendure/issues/927
  1317. it('nested variantName filter', async () => {
  1318. const parent = await createVariantNameFilteredCollection('contains', 'lap');
  1319. const parentResult = await adminClient.query<
  1320. GetCollectionProducts.Query,
  1321. GetCollectionProducts.Variables
  1322. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1323. id: parent.id,
  1324. });
  1325. expect(parentResult.collection?.productVariants.items.map(i => i.name)).toEqual([
  1326. 'Laptop 13 inch 8GB',
  1327. 'Laptop 15 inch 8GB',
  1328. 'Laptop 13 inch 16GB',
  1329. 'Laptop 15 inch 16GB',
  1330. ]);
  1331. const child = await createVariantNameFilteredCollection('contains', 'GB', parent.id);
  1332. const childResult = await adminClient.query<
  1333. GetCollectionProducts.Query,
  1334. GetCollectionProducts.Variables
  1335. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1336. id: child.id,
  1337. });
  1338. expect(childResult.collection?.productVariants.items.map(i => i.name)).toEqual([
  1339. 'Laptop 13 inch 8GB',
  1340. 'Laptop 15 inch 8GB',
  1341. 'Laptop 13 inch 16GB',
  1342. 'Laptop 15 inch 16GB',
  1343. ]);
  1344. });
  1345. });
  1346. describe('re-evaluation of contents on changes', () => {
  1347. let products: GetProductsWithVariantIds.Items[];
  1348. beforeAll(async () => {
  1349. const result = await adminClient.query<GetProductsWithVariantIds.Query>(gql`
  1350. query GetProductsWithVariantIds {
  1351. products(options: { sort: { id: ASC } }) {
  1352. items {
  1353. id
  1354. name
  1355. variants {
  1356. id
  1357. name
  1358. }
  1359. }
  1360. }
  1361. }
  1362. `);
  1363. products = result.products.items;
  1364. });
  1365. it('updates contents when Product is updated', async () => {
  1366. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  1367. input: {
  1368. id: products[1].id,
  1369. facetValueIds: [
  1370. getFacetValueId('electronics'),
  1371. getFacetValueId('computers'),
  1372. getFacetValueId('pear'),
  1373. ],
  1374. },
  1375. });
  1376. await awaitRunningJobs(adminClient, 5000);
  1377. const result = await adminClient.query<
  1378. GetCollectionProducts.Query,
  1379. GetCollectionProducts.Variables
  1380. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  1381. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1382. 'Laptop 13 inch 8GB',
  1383. 'Laptop 15 inch 8GB',
  1384. 'Laptop 13 inch 16GB',
  1385. 'Laptop 15 inch 16GB',
  1386. 'Curvy Monitor 24 inch',
  1387. 'Curvy Monitor 27 inch',
  1388. 'Instant Camera',
  1389. ]);
  1390. });
  1391. it('updates contents when ProductVariant is updated', async () => {
  1392. const gamingPc240GB = products
  1393. .find(p => p.name === 'Gaming PC')!
  1394. .variants.find(v => v.name.includes('240GB'))!;
  1395. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1396. UPDATE_PRODUCT_VARIANTS,
  1397. {
  1398. input: [
  1399. {
  1400. id: gamingPc240GB.id,
  1401. facetValueIds: [getFacetValueId('pear')],
  1402. },
  1403. ],
  1404. },
  1405. );
  1406. await awaitRunningJobs(adminClient, 5000);
  1407. const result = await adminClient.query<
  1408. GetCollectionProducts.Query,
  1409. GetCollectionProducts.Variables
  1410. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  1411. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1412. 'Laptop 13 inch 8GB',
  1413. 'Laptop 15 inch 8GB',
  1414. 'Laptop 13 inch 16GB',
  1415. 'Laptop 15 inch 16GB',
  1416. 'Curvy Monitor 24 inch',
  1417. 'Curvy Monitor 27 inch',
  1418. 'Gaming PC i7-8700 240GB SSD',
  1419. 'Instant Camera',
  1420. ]);
  1421. });
  1422. it('correctly filters when ProductVariant and Product both have matching FacetValue', async () => {
  1423. const gamingPc240GB = products
  1424. .find(p => p.name === 'Gaming PC')!
  1425. .variants.find(v => v.name.includes('240GB'))!;
  1426. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1427. UPDATE_PRODUCT_VARIANTS,
  1428. {
  1429. input: [
  1430. {
  1431. id: gamingPc240GB.id,
  1432. facetValueIds: [getFacetValueId('electronics'), getFacetValueId('pear')],
  1433. },
  1434. ],
  1435. },
  1436. );
  1437. await awaitRunningJobs(adminClient, 5000);
  1438. const result = await adminClient.query<
  1439. GetCollectionProducts.Query,
  1440. GetCollectionProducts.Variables
  1441. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  1442. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1443. 'Laptop 13 inch 8GB',
  1444. 'Laptop 15 inch 8GB',
  1445. 'Laptop 13 inch 16GB',
  1446. 'Laptop 15 inch 16GB',
  1447. 'Curvy Monitor 24 inch',
  1448. 'Curvy Monitor 27 inch',
  1449. 'Gaming PC i7-8700 240GB SSD',
  1450. 'Instant Camera',
  1451. ]);
  1452. });
  1453. });
  1454. it('filter inheritance of nested collections (issue #158)', async () => {
  1455. const a = 1;
  1456. const { createCollection: pearElectronics } = await adminClient.query<
  1457. CreateCollectionSelectVariants.Mutation,
  1458. CreateCollectionSelectVariants.Variables
  1459. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  1460. input: {
  1461. parentId: electronicsCollection.id,
  1462. translations: [
  1463. {
  1464. languageCode: LanguageCode.en,
  1465. name: 'pear electronics',
  1466. description: '',
  1467. slug: 'pear-electronics',
  1468. },
  1469. ],
  1470. filters: [
  1471. {
  1472. code: facetValueCollectionFilter.code,
  1473. arguments: [
  1474. {
  1475. name: 'facetValueIds',
  1476. value: `["${getFacetValueId('pear')}"]`,
  1477. },
  1478. {
  1479. name: 'containsAny',
  1480. value: `false`,
  1481. },
  1482. ],
  1483. },
  1484. ],
  1485. } as CreateCollectionInput,
  1486. });
  1487. await awaitRunningJobs(adminClient, 5000);
  1488. const result = await adminClient.query<
  1489. GetCollectionProducts.Query,
  1490. GetCollectionProducts.Variables
  1491. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearElectronics.id });
  1492. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1493. 'Laptop 13 inch 8GB',
  1494. 'Laptop 15 inch 8GB',
  1495. 'Laptop 13 inch 16GB',
  1496. 'Laptop 15 inch 16GB',
  1497. 'Curvy Monitor 24 inch',
  1498. 'Curvy Monitor 27 inch',
  1499. 'Gaming PC i7-8700 240GB SSD',
  1500. 'Instant Camera',
  1501. // no "Hat"
  1502. ]);
  1503. });
  1504. });
  1505. describe('Product collections property', () => {
  1506. it('returns all collections to which the Product belongs', async () => {
  1507. const result = await adminClient.query<
  1508. GetCollectionsForProducts.Query,
  1509. GetCollectionsForProducts.Variables
  1510. >(GET_COLLECTIONS_FOR_PRODUCTS, { term: 'camera' });
  1511. expect(result.products.items[0].collections).toEqual([
  1512. {
  1513. id: 'T_3',
  1514. name: 'Electronics',
  1515. },
  1516. {
  1517. id: 'T_5',
  1518. name: 'Pear',
  1519. },
  1520. {
  1521. id: 'T_9',
  1522. name: 'Electronics Breadcrumbs',
  1523. },
  1524. {
  1525. id: 'T_14',
  1526. name: 'Photo AND Pear',
  1527. },
  1528. {
  1529. id: 'T_15',
  1530. name: 'Photo OR Pear',
  1531. },
  1532. {
  1533. id: 'T_17',
  1534. name: 'contains camera',
  1535. },
  1536. {
  1537. id: 'T_19',
  1538. name: 'endsWith camera',
  1539. },
  1540. {
  1541. id: 'T_23',
  1542. name: 'pear electronics',
  1543. },
  1544. ]);
  1545. });
  1546. });
  1547. describe('productVariants list', () => {
  1548. it('does not list variants from deleted products', async () => {
  1549. await adminClient.query<DeleteProduct.Mutation, DeleteProduct.Variables>(DELETE_PRODUCT, {
  1550. id: 'T_2', // curvy monitor
  1551. });
  1552. const { collection } = await adminClient.query<
  1553. GetCollectionProducts.Query,
  1554. GetCollectionProducts.Variables
  1555. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1556. id: pearCollection.id,
  1557. });
  1558. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  1559. 'Laptop 13 inch 8GB',
  1560. 'Laptop 15 inch 8GB',
  1561. 'Laptop 13 inch 16GB',
  1562. 'Laptop 15 inch 16GB',
  1563. 'Gaming PC i7-8700 240GB SSD',
  1564. 'Instant Camera',
  1565. ]);
  1566. });
  1567. it('does not list disabled variants in Shop API', async () => {
  1568. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1569. UPDATE_PRODUCT_VARIANTS,
  1570. {
  1571. input: [{ id: 'T_1', enabled: false }],
  1572. },
  1573. );
  1574. await awaitRunningJobs(adminClient, 5000);
  1575. const { collection } = await shopClient.query<
  1576. GetCollectionProducts.Query,
  1577. GetCollectionProducts.Variables
  1578. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1579. id: pearCollection.id,
  1580. });
  1581. expect(collection!.productVariants.items.map(i => i.id).includes('T_1')).toBe(false);
  1582. });
  1583. it('handles other languages', async () => {
  1584. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1585. UPDATE_PRODUCT_VARIANTS,
  1586. {
  1587. input: [
  1588. {
  1589. id: 'T_2',
  1590. translations: [{ languageCode: LanguageCode.de, name: 'Taschenrechner 15 Zoll' }],
  1591. },
  1592. ],
  1593. },
  1594. );
  1595. const { collection } = await shopClient.query<
  1596. GetCollectionProducts.Query,
  1597. GetCollectionProducts.Variables
  1598. >(
  1599. GET_COLLECTION_PRODUCT_VARIANTS,
  1600. {
  1601. id: pearCollection.id,
  1602. },
  1603. { languageCode: LanguageCode.de },
  1604. );
  1605. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  1606. 'Taschenrechner 15 Zoll',
  1607. 'Laptop 13 inch 16GB',
  1608. 'Laptop 15 inch 16GB',
  1609. 'Gaming PC i7-8700 240GB SSD',
  1610. 'Instant Camera',
  1611. ]);
  1612. });
  1613. });
  1614. function getFacetValueId(code: string): string {
  1615. const match = facetValues.find(fv => fv.code === code);
  1616. if (!match) {
  1617. throw new Error(`Could not find a FacetValue with the code "${code}"`);
  1618. }
  1619. return match.id;
  1620. }
  1621. });
  1622. export const GET_COLLECTION = gql`
  1623. query GetCollection($id: ID, $slug: String, $variantListOptions: ProductVariantListOptions) {
  1624. collection(id: $id, slug: $slug) {
  1625. ...Collection
  1626. productVariants(options: $variantListOptions) {
  1627. items {
  1628. id
  1629. name
  1630. price
  1631. }
  1632. }
  1633. }
  1634. }
  1635. ${COLLECTION_FRAGMENT}
  1636. `;
  1637. export const MOVE_COLLECTION = gql`
  1638. mutation MoveCollection($input: MoveCollectionInput!) {
  1639. moveCollection(input: $input) {
  1640. ...Collection
  1641. }
  1642. }
  1643. ${COLLECTION_FRAGMENT}
  1644. `;
  1645. const GET_FACET_VALUES = gql`
  1646. query GetFacetValues {
  1647. facets {
  1648. items {
  1649. values {
  1650. ...FacetValue
  1651. }
  1652. }
  1653. }
  1654. }
  1655. ${FACET_VALUE_FRAGMENT}
  1656. `;
  1657. const GET_COLLECTIONS = gql`
  1658. query GetCollections {
  1659. collections {
  1660. items {
  1661. id
  1662. name
  1663. position
  1664. parent {
  1665. id
  1666. name
  1667. }
  1668. }
  1669. }
  1670. }
  1671. `;
  1672. const GET_COLLECTION_PRODUCT_VARIANTS = gql`
  1673. query GetCollectionProducts($id: ID!) {
  1674. collection(id: $id) {
  1675. productVariants(options: { sort: { id: ASC } }) {
  1676. items {
  1677. id
  1678. name
  1679. facetValues {
  1680. code
  1681. }
  1682. productId
  1683. }
  1684. }
  1685. }
  1686. }
  1687. `;
  1688. const CREATE_COLLECTION_SELECT_VARIANTS = gql`
  1689. mutation CreateCollectionSelectVariants($input: CreateCollectionInput!) {
  1690. createCollection(input: $input) {
  1691. id
  1692. productVariants {
  1693. items {
  1694. name
  1695. }
  1696. totalItems
  1697. }
  1698. }
  1699. }
  1700. `;
  1701. const GET_COLLECTION_BREADCRUMBS = gql`
  1702. query GetCollectionBreadcrumbs($id: ID!) {
  1703. collection(id: $id) {
  1704. breadcrumbs {
  1705. id
  1706. name
  1707. slug
  1708. }
  1709. }
  1710. }
  1711. `;
  1712. const GET_COLLECTIONS_FOR_PRODUCTS = gql`
  1713. query GetCollectionsForProducts($term: String!) {
  1714. products(options: { filter: { name: { contains: $term } } }) {
  1715. items {
  1716. id
  1717. name
  1718. collections {
  1719. id
  1720. name
  1721. }
  1722. }
  1723. }
  1724. }
  1725. `;
  1726. const DELETE_COLLECTION = gql`
  1727. mutation DeleteCollection($id: ID!) {
  1728. deleteCollection(id: $id) {
  1729. result
  1730. message
  1731. }
  1732. }
  1733. `;
  1734. const GET_PRODUCT_COLLECTIONS = gql`
  1735. query GetProductCollections($id: ID!) {
  1736. product(id: $id) {
  1737. id
  1738. collections {
  1739. id
  1740. name
  1741. }
  1742. }
  1743. }
  1744. `;
  1745. const GET_PRODUCT_COLLECTIONS_WITH_PARENT = gql`
  1746. query GetProductCollectionsWithParent($id: ID!) {
  1747. product(id: $id) {
  1748. id
  1749. collections {
  1750. id
  1751. name
  1752. parent {
  1753. id
  1754. name
  1755. }
  1756. }
  1757. }
  1758. }
  1759. `;
  1760. const GET_COLLECTION_NESTED_PARENTS = gql`
  1761. query GetCollectionNestedParents {
  1762. collections {
  1763. items {
  1764. id
  1765. name
  1766. parent {
  1767. name
  1768. parent {
  1769. name
  1770. parent {
  1771. name
  1772. }
  1773. }
  1774. }
  1775. }
  1776. }
  1777. }
  1778. `;