collection.e2e-spec.ts 73 KB

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