collection.e2e-spec.ts 84 KB

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