collection.e2e-spec.ts 100 KB

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