collection.e2e-spec.ts 100 KB

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