collection.e2e-spec.ts 101 KB

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