collection.e2e-spec.ts 73 KB

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