collection.e2e-spec.ts 91 KB

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