1
0

collection.e2e-spec.ts 101 KB

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