collection.e2e-spec.ts 86 KB

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