collection.e2e-spec.ts 71 KB

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