collection.e2e-spec.ts 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  1. /* tslint:disable:no-non-null-assertion */
  2. import { ROOT_COLLECTION_NAME } from '@vendure/common/lib/shared-constants';
  3. import {
  4. DefaultJobQueuePlugin,
  5. facetValueCollectionFilter,
  6. variantNameCollectionFilter,
  7. } from '@vendure/core';
  8. import { createTestEnvironment } from '@vendure/testing';
  9. import gql from 'graphql-tag';
  10. import path from 'path';
  11. import { initialData } from '../../../e2e-common/e2e-initial-data';
  12. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  13. import { pick } from '../../common/lib/pick';
  14. import { COLLECTION_FRAGMENT, FACET_VALUE_FRAGMENT } from './graphql/fragments';
  15. import {
  16. Collection,
  17. CreateCollection,
  18. CreateCollectionInput,
  19. CreateCollectionSelectVariants,
  20. DeleteCollection,
  21. DeleteProduct,
  22. DeletionResult,
  23. FacetValueFragment,
  24. GetAssetList,
  25. GetCollection,
  26. GetCollectionBreadcrumbs,
  27. GetCollectionProducts,
  28. GetCollections,
  29. GetCollectionsForProducts,
  30. GetCollectionsWithAssets,
  31. GetFacetValues,
  32. GetProductCollections,
  33. GetProductCollectionsWithParent,
  34. GetProductsWithVariantIds,
  35. LanguageCode,
  36. MoveCollection,
  37. SortOrder,
  38. UpdateCollection,
  39. UpdateProduct,
  40. UpdateProductVariants,
  41. } from './graphql/generated-e2e-admin-types';
  42. import {
  43. CREATE_COLLECTION,
  44. DELETE_PRODUCT,
  45. GET_ASSET_LIST,
  46. UPDATE_COLLECTION,
  47. UPDATE_PRODUCT,
  48. UPDATE_PRODUCT_VARIANTS,
  49. } from './graphql/shared-definitions';
  50. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  51. import { awaitRunningJobs } from './utils/await-running-jobs';
  52. import { sortById } from './utils/test-order-utils';
  53. describe('Collection resolver', () => {
  54. const { server, adminClient } = createTestEnvironment({
  55. ...testConfig,
  56. plugins: [DefaultJobQueuePlugin],
  57. });
  58. let assets: GetAssetList.Items[];
  59. let facetValues: FacetValueFragment[];
  60. let electronicsCollection: Collection.Fragment;
  61. let computersCollection: Collection.Fragment;
  62. let pearCollection: Collection.Fragment;
  63. beforeAll(async () => {
  64. await server.init({
  65. initialData,
  66. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-collections.csv'),
  67. customerCount: 1,
  68. });
  69. await adminClient.asSuperAdmin();
  70. const assetsResult = await adminClient.query<GetAssetList.Query, GetAssetList.Variables>(
  71. GET_ASSET_LIST,
  72. {
  73. options: {
  74. sort: {
  75. name: SortOrder.ASC,
  76. },
  77. },
  78. },
  79. );
  80. assets = assetsResult.assets.items;
  81. const facetValuesResult = await adminClient.query<GetFacetValues.Query>(GET_FACET_VALUES);
  82. facetValues = facetValuesResult.facets.items.reduce(
  83. (values, facet) => [...values, ...facet.values],
  84. [] as FacetValueFragment[],
  85. );
  86. }, TEST_SETUP_TIMEOUT_MS);
  87. afterAll(async () => {
  88. await server.destroy();
  89. });
  90. /**
  91. * Test case for https://github.com/vendure-ecommerce/vendure/issues/97
  92. */
  93. it('collection breadcrumbs works after bootstrap', async () => {
  94. const result = await adminClient.query<GetCollectionBreadcrumbs.Query>(GET_COLLECTION_BREADCRUMBS, {
  95. id: 'T_1',
  96. });
  97. expect(result.collection!.breadcrumbs[0].name).toBe(ROOT_COLLECTION_NAME);
  98. });
  99. describe('createCollection', () => {
  100. it('creates a root collection', async () => {
  101. const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  102. CREATE_COLLECTION,
  103. {
  104. input: {
  105. assetIds: [assets[0].id, assets[1].id],
  106. featuredAssetId: assets[1].id,
  107. filters: [
  108. {
  109. code: facetValueCollectionFilter.code,
  110. arguments: [
  111. {
  112. name: 'facetValueIds',
  113. value: `["${getFacetValueId('electronics')}"]`,
  114. },
  115. {
  116. name: 'containsAny',
  117. value: `false`,
  118. },
  119. ],
  120. },
  121. ],
  122. translations: [
  123. {
  124. languageCode: LanguageCode.en,
  125. name: 'Electronics',
  126. description: '',
  127. slug: 'electronics',
  128. },
  129. ],
  130. },
  131. },
  132. );
  133. electronicsCollection = result.createCollection;
  134. expect(electronicsCollection).toMatchSnapshot();
  135. expect(electronicsCollection.parent!.name).toBe(ROOT_COLLECTION_NAME);
  136. });
  137. it('creates a nested collection', async () => {
  138. const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  139. CREATE_COLLECTION,
  140. {
  141. input: {
  142. parentId: electronicsCollection.id,
  143. translations: [
  144. {
  145. languageCode: LanguageCode.en,
  146. name: 'Computers',
  147. description: '',
  148. slug: 'computers',
  149. },
  150. ],
  151. filters: [
  152. {
  153. code: facetValueCollectionFilter.code,
  154. arguments: [
  155. {
  156. name: 'facetValueIds',
  157. value: `["${getFacetValueId('computers')}"]`,
  158. },
  159. {
  160. name: 'containsAny',
  161. value: `false`,
  162. },
  163. ],
  164. },
  165. ],
  166. },
  167. },
  168. );
  169. computersCollection = result.createCollection;
  170. expect(computersCollection.parent!.name).toBe(electronicsCollection.name);
  171. });
  172. it('creates a 2nd level nested collection', async () => {
  173. const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  174. CREATE_COLLECTION,
  175. {
  176. input: {
  177. parentId: computersCollection.id,
  178. translations: [
  179. { languageCode: LanguageCode.en, name: 'Pear', description: '', slug: 'pear' },
  180. ],
  181. filters: [
  182. {
  183. code: facetValueCollectionFilter.code,
  184. arguments: [
  185. {
  186. name: 'facetValueIds',
  187. value: `["${getFacetValueId('pear')}"]`,
  188. },
  189. {
  190. name: 'containsAny',
  191. value: `false`,
  192. },
  193. ],
  194. },
  195. ],
  196. },
  197. },
  198. );
  199. pearCollection = result.createCollection;
  200. expect(pearCollection.parent!.name).toBe(computersCollection.name);
  201. });
  202. it('slug is normalized to be url-safe', async () => {
  203. const { createCollection } = await adminClient.query<
  204. CreateCollection.Mutation,
  205. CreateCollection.Variables
  206. >(CREATE_COLLECTION, {
  207. input: {
  208. translations: [
  209. {
  210. languageCode: LanguageCode.en,
  211. name: 'Accessories',
  212. description: '',
  213. slug: 'Accessories!',
  214. },
  215. {
  216. languageCode: LanguageCode.de,
  217. name: 'Zubehör',
  218. description: '',
  219. slug: 'Zubehör!',
  220. },
  221. ],
  222. filters: [],
  223. },
  224. });
  225. expect(createCollection.slug).toBe('accessories');
  226. expect(createCollection.translations.find(t => t.languageCode === LanguageCode.en)?.slug).toBe(
  227. 'accessories',
  228. );
  229. expect(createCollection.translations.find(t => t.languageCode === LanguageCode.de)?.slug).toBe(
  230. 'zubehor',
  231. );
  232. });
  233. it('create with duplicate slug is renamed to be unique', async () => {
  234. const { createCollection } = await adminClient.query<
  235. CreateCollection.Mutation,
  236. CreateCollection.Variables
  237. >(CREATE_COLLECTION, {
  238. input: {
  239. translations: [
  240. {
  241. languageCode: LanguageCode.en,
  242. name: 'Accessories',
  243. description: '',
  244. slug: 'Accessories',
  245. },
  246. {
  247. languageCode: LanguageCode.de,
  248. name: 'Zubehör',
  249. description: '',
  250. slug: 'Zubehör',
  251. },
  252. ],
  253. filters: [],
  254. },
  255. });
  256. expect(createCollection.translations.find(t => t.languageCode === LanguageCode.en)?.slug).toBe(
  257. 'accessories-2',
  258. );
  259. expect(createCollection.translations.find(t => t.languageCode === LanguageCode.de)?.slug).toBe(
  260. 'zubehor-2',
  261. );
  262. });
  263. });
  264. describe('updateCollection', () => {
  265. it('updates with assets', async () => {
  266. const { updateCollection } = await adminClient.query<
  267. UpdateCollection.Mutation,
  268. UpdateCollection.Variables
  269. >(UPDATE_COLLECTION, {
  270. input: {
  271. id: pearCollection.id,
  272. assetIds: [assets[1].id, assets[2].id],
  273. featuredAssetId: assets[1].id,
  274. translations: [
  275. { languageCode: LanguageCode.en, description: 'Apple stuff ', slug: 'apple-stuff' },
  276. ],
  277. },
  278. });
  279. await awaitRunningJobs(adminClient);
  280. expect(updateCollection).toMatchSnapshot();
  281. pearCollection = updateCollection;
  282. });
  283. it('updating existing assets', async () => {
  284. const { updateCollection } = await adminClient.query<
  285. UpdateCollection.Mutation,
  286. UpdateCollection.Variables
  287. >(UPDATE_COLLECTION, {
  288. input: {
  289. id: pearCollection.id,
  290. assetIds: [assets[3].id, assets[0].id],
  291. featuredAssetId: assets[3].id,
  292. },
  293. });
  294. await awaitRunningJobs(adminClient);
  295. expect(updateCollection.assets.map(a => a.id)).toEqual([assets[3].id, assets[0].id]);
  296. });
  297. it('removes all assets', async () => {
  298. const { updateCollection } = await adminClient.query<
  299. UpdateCollection.Mutation,
  300. UpdateCollection.Variables
  301. >(UPDATE_COLLECTION, {
  302. input: {
  303. id: pearCollection.id,
  304. assetIds: [],
  305. },
  306. });
  307. await awaitRunningJobs(adminClient);
  308. expect(updateCollection.assets).toEqual([]);
  309. expect(updateCollection.featuredAsset).toBeNull();
  310. });
  311. });
  312. describe('querying', () => {
  313. it('collection by id', async () => {
  314. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  315. GET_COLLECTION,
  316. {
  317. id: computersCollection.id,
  318. },
  319. );
  320. if (!result.collection) {
  321. fail(`did not return the collection`);
  322. return;
  323. }
  324. expect(result.collection.id).toBe(computersCollection.id);
  325. });
  326. it('collection by slug', async () => {
  327. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  328. GET_COLLECTION,
  329. {
  330. slug: computersCollection.slug,
  331. },
  332. );
  333. if (!result.collection) {
  334. fail(`did not return the collection`);
  335. return;
  336. }
  337. expect(result.collection.id).toBe(computersCollection.id);
  338. });
  339. it(
  340. 'throws if neither id nor slug provided',
  341. assertThrowsWithMessage(async () => {
  342. await adminClient.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {});
  343. }, 'Either the Collection id or slug must be provided'),
  344. );
  345. it(
  346. 'throws if id and slug do not refer to the same Product',
  347. assertThrowsWithMessage(async () => {
  348. await adminClient.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
  349. id: computersCollection.id,
  350. slug: pearCollection.slug,
  351. });
  352. }, 'The provided id and slug refer to different Collections'),
  353. );
  354. it('parent field', async () => {
  355. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  356. GET_COLLECTION,
  357. {
  358. id: computersCollection.id,
  359. },
  360. );
  361. if (!result.collection) {
  362. fail(`did not return the collection`);
  363. return;
  364. }
  365. expect(result.collection.parent!.name).toBe('Electronics');
  366. });
  367. // Tests fix for https://github.com/vendure-ecommerce/vendure/issues/361
  368. it('parent field resolved by CollectionEntityResolver', async () => {
  369. const { product } = await adminClient.query<
  370. GetProductCollectionsWithParent.Query,
  371. GetProductCollectionsWithParent.Variables
  372. >(GET_PRODUCT_COLLECTIONS_WITH_PARENT, {
  373. id: 'T_1',
  374. });
  375. expect(product?.collections.length).toBe(3);
  376. expect(product?.collections.sort(sortById)).toEqual([
  377. {
  378. id: 'T_3',
  379. name: 'Electronics',
  380. parent: {
  381. id: 'T_1',
  382. name: '__root_collection__',
  383. },
  384. },
  385. {
  386. id: 'T_4',
  387. name: 'Computers',
  388. parent: {
  389. id: 'T_3',
  390. name: 'Electronics',
  391. },
  392. },
  393. {
  394. id: 'T_5',
  395. name: 'Pear',
  396. parent: {
  397. id: 'T_4',
  398. name: 'Computers',
  399. },
  400. },
  401. ]);
  402. });
  403. it('children field', async () => {
  404. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  405. GET_COLLECTION,
  406. {
  407. id: electronicsCollection.id,
  408. },
  409. );
  410. if (!result.collection) {
  411. fail(`did not return the collection`);
  412. return;
  413. }
  414. expect(result.collection.children!.length).toBe(1);
  415. expect(result.collection.children![0].name).toBe('Computers');
  416. });
  417. it('breadcrumbs', async () => {
  418. const result = await adminClient.query<
  419. GetCollectionBreadcrumbs.Query,
  420. GetCollectionBreadcrumbs.Variables
  421. >(GET_COLLECTION_BREADCRUMBS, {
  422. id: pearCollection.id,
  423. });
  424. if (!result.collection) {
  425. fail(`did not return the collection`);
  426. return;
  427. }
  428. expect(result.collection.breadcrumbs).toEqual([
  429. { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
  430. {
  431. id: electronicsCollection.id,
  432. name: electronicsCollection.name,
  433. slug: electronicsCollection.slug,
  434. },
  435. {
  436. id: computersCollection.id,
  437. name: computersCollection.name,
  438. slug: computersCollection.slug,
  439. },
  440. { id: pearCollection.id, name: pearCollection.name, slug: pearCollection.slug },
  441. ]);
  442. });
  443. it('breadcrumbs for root collection', async () => {
  444. const result = await adminClient.query<
  445. GetCollectionBreadcrumbs.Query,
  446. GetCollectionBreadcrumbs.Variables
  447. >(GET_COLLECTION_BREADCRUMBS, {
  448. id: 'T_1',
  449. });
  450. if (!result.collection) {
  451. fail(`did not return the collection`);
  452. return;
  453. }
  454. expect(result.collection.breadcrumbs).toEqual([
  455. { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
  456. ]);
  457. });
  458. it('collections.assets', async () => {
  459. const { collections } = await adminClient.query<GetCollectionsWithAssets.Query>(gql`
  460. query GetCollectionsWithAssets {
  461. collections {
  462. items {
  463. assets {
  464. name
  465. }
  466. }
  467. }
  468. }
  469. `);
  470. expect(collections.items[0].assets).toBeDefined();
  471. });
  472. // https://github.com/vendure-ecommerce/vendure/issues/642
  473. it('sorting on Collection.productVariants.price', async () => {
  474. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  475. GET_COLLECTION,
  476. {
  477. id: computersCollection.id,
  478. variantListOptions: {
  479. sort: {
  480. price: SortOrder.ASC,
  481. },
  482. },
  483. },
  484. );
  485. expect(collection!.productVariants.items.map(i => i.price)).toEqual([
  486. 3799,
  487. 5374,
  488. 6900,
  489. 7489,
  490. 7896,
  491. 9299,
  492. 13435,
  493. 14374,
  494. 16994,
  495. 93120,
  496. 94920,
  497. 108720,
  498. 109995,
  499. 129900,
  500. 139900,
  501. 219900,
  502. 229900,
  503. ]);
  504. });
  505. });
  506. describe('moveCollection', () => {
  507. it('moves a collection to a new parent', async () => {
  508. const result = await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(
  509. MOVE_COLLECTION,
  510. {
  511. input: {
  512. collectionId: pearCollection.id,
  513. parentId: electronicsCollection.id,
  514. index: 0,
  515. },
  516. },
  517. );
  518. expect(result.moveCollection.parent!.id).toBe(electronicsCollection.id);
  519. const positions = await getChildrenOf(electronicsCollection.id);
  520. expect(positions.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  521. });
  522. it('re-evaluates Collection contents on move', async () => {
  523. await awaitRunningJobs(adminClient);
  524. const result = await adminClient.query<
  525. GetCollectionProducts.Query,
  526. GetCollectionProducts.Variables
  527. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  528. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  529. 'Laptop 13 inch 8GB',
  530. 'Laptop 15 inch 8GB',
  531. 'Laptop 13 inch 16GB',
  532. 'Laptop 15 inch 16GB',
  533. 'Instant Camera',
  534. ]);
  535. });
  536. it('alters the position in the current parent 1', async () => {
  537. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  538. input: {
  539. collectionId: computersCollection.id,
  540. parentId: electronicsCollection.id,
  541. index: 0,
  542. },
  543. });
  544. const afterResult = await getChildrenOf(electronicsCollection.id);
  545. expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
  546. });
  547. it('alters the position in the current parent 2', async () => {
  548. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  549. input: {
  550. collectionId: pearCollection.id,
  551. parentId: electronicsCollection.id,
  552. index: 0,
  553. },
  554. });
  555. const afterResult = await getChildrenOf(electronicsCollection.id);
  556. expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  557. });
  558. it('corrects an out-of-bounds negative index value', async () => {
  559. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  560. input: {
  561. collectionId: pearCollection.id,
  562. parentId: electronicsCollection.id,
  563. index: -3,
  564. },
  565. });
  566. const afterResult = await getChildrenOf(electronicsCollection.id);
  567. expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  568. });
  569. it('corrects an out-of-bounds positive index value', async () => {
  570. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  571. input: {
  572. collectionId: pearCollection.id,
  573. parentId: electronicsCollection.id,
  574. index: 10,
  575. },
  576. });
  577. const afterResult = await getChildrenOf(electronicsCollection.id);
  578. expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
  579. });
  580. it(
  581. 'throws if attempting to move into self',
  582. assertThrowsWithMessage(
  583. () =>
  584. adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  585. input: {
  586. collectionId: pearCollection.id,
  587. parentId: pearCollection.id,
  588. index: 0,
  589. },
  590. }),
  591. `Cannot move a Collection into itself`,
  592. ),
  593. );
  594. it(
  595. 'throws if attempting to move into a decendant of self',
  596. assertThrowsWithMessage(
  597. () =>
  598. adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  599. input: {
  600. collectionId: pearCollection.id,
  601. parentId: pearCollection.id,
  602. index: 0,
  603. },
  604. }),
  605. `Cannot move a Collection into itself`,
  606. ),
  607. );
  608. async function getChildrenOf(parentId: string): Promise<Array<{ name: string; id: string }>> {
  609. const result = await adminClient.query<GetCollections.Query>(GET_COLLECTIONS);
  610. return result.collections.items.filter(i => i.parent!.id === parentId);
  611. }
  612. });
  613. describe('deleteCollection', () => {
  614. let collectionToDeleteParent: CreateCollection.CreateCollection;
  615. let collectionToDeleteChild: CreateCollection.CreateCollection;
  616. let laptopProductId: string;
  617. beforeAll(async () => {
  618. const result1 = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  619. CREATE_COLLECTION,
  620. {
  621. input: {
  622. filters: [
  623. {
  624. code: variantNameCollectionFilter.code,
  625. arguments: [
  626. {
  627. name: 'operator',
  628. value: 'contains',
  629. },
  630. {
  631. name: 'term',
  632. value: 'laptop',
  633. },
  634. ],
  635. },
  636. ],
  637. translations: [
  638. {
  639. languageCode: LanguageCode.en,
  640. name: 'Delete Me Parent',
  641. description: '',
  642. slug: 'delete-me-parent',
  643. },
  644. ],
  645. assetIds: ['T_1'],
  646. },
  647. },
  648. );
  649. collectionToDeleteParent = result1.createCollection;
  650. const result2 = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  651. CREATE_COLLECTION,
  652. {
  653. input: {
  654. filters: [],
  655. translations: [
  656. {
  657. languageCode: LanguageCode.en,
  658. name: 'Delete Me Child',
  659. description: '',
  660. slug: 'delete-me-child',
  661. },
  662. ],
  663. parentId: collectionToDeleteParent.id,
  664. assetIds: ['T_2'],
  665. },
  666. },
  667. );
  668. collectionToDeleteChild = result2.createCollection;
  669. await awaitRunningJobs(adminClient);
  670. });
  671. it(
  672. 'throws for invalid collection id',
  673. assertThrowsWithMessage(async () => {
  674. await adminClient.query<DeleteCollection.Mutation, DeleteCollection.Variables>(
  675. DELETE_COLLECTION,
  676. {
  677. id: 'T_999',
  678. },
  679. );
  680. }, "No Collection with the id '999' could be found"),
  681. );
  682. it('collection and product related prior to deletion', async () => {
  683. const { collection } = await adminClient.query<
  684. GetCollectionProducts.Query,
  685. GetCollectionProducts.Variables
  686. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  687. id: collectionToDeleteParent.id,
  688. });
  689. expect(collection!.productVariants.items.map(pick(['name']))).toEqual([
  690. { name: 'Laptop 13 inch 8GB' },
  691. { name: 'Laptop 15 inch 8GB' },
  692. { name: 'Laptop 13 inch 16GB' },
  693. { name: 'Laptop 15 inch 16GB' },
  694. ]);
  695. laptopProductId = collection!.productVariants.items[0].productId;
  696. const { product } = await adminClient.query<
  697. GetProductCollections.Query,
  698. GetProductCollections.Variables
  699. >(GET_PRODUCT_COLLECTIONS, {
  700. id: laptopProductId,
  701. });
  702. expect(product!.collections).toEqual([
  703. { id: 'T_3', name: 'Electronics' },
  704. { id: 'T_4', name: 'Computers' },
  705. { id: 'T_5', name: 'Pear' },
  706. { id: 'T_8', name: 'Delete Me Parent' },
  707. { id: 'T_9', name: 'Delete Me Child' },
  708. ]);
  709. });
  710. it('deleteCollection works', async () => {
  711. const { deleteCollection } = await adminClient.query<
  712. DeleteCollection.Mutation,
  713. DeleteCollection.Variables
  714. >(DELETE_COLLECTION, {
  715. id: collectionToDeleteParent.id,
  716. });
  717. expect(deleteCollection.result).toBe(DeletionResult.DELETED);
  718. });
  719. it('deleted parent collection is null', async () => {
  720. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  721. GET_COLLECTION,
  722. {
  723. id: collectionToDeleteParent.id,
  724. },
  725. );
  726. expect(collection).toBeNull();
  727. });
  728. it('deleted child collection is null', async () => {
  729. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  730. GET_COLLECTION,
  731. {
  732. id: collectionToDeleteChild.id,
  733. },
  734. );
  735. expect(collection).toBeNull();
  736. });
  737. it('product no longer lists collection', async () => {
  738. const { product } = await adminClient.query<
  739. GetProductCollections.Query,
  740. GetProductCollections.Variables
  741. >(GET_PRODUCT_COLLECTIONS, {
  742. id: laptopProductId,
  743. });
  744. expect(product!.collections).toEqual([
  745. { id: 'T_3', name: 'Electronics' },
  746. { id: 'T_4', name: 'Computers' },
  747. { id: 'T_5', name: 'Pear' },
  748. ]);
  749. });
  750. });
  751. describe('filters', () => {
  752. it('Collection with no filters has no productVariants', async () => {
  753. const result = await adminClient.query<
  754. CreateCollectionSelectVariants.Mutation,
  755. CreateCollectionSelectVariants.Variables
  756. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  757. input: {
  758. translations: [
  759. { languageCode: LanguageCode.en, name: 'Empty', description: '', slug: 'empty' },
  760. ],
  761. filters: [],
  762. } as CreateCollectionInput,
  763. });
  764. expect(result.createCollection.productVariants.totalItems).toBe(0);
  765. });
  766. describe('facetValue filter', () => {
  767. it('electronics', async () => {
  768. const result = await adminClient.query<
  769. GetCollectionProducts.Query,
  770. GetCollectionProducts.Variables
  771. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  772. id: electronicsCollection.id,
  773. });
  774. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  775. 'Laptop 13 inch 8GB',
  776. 'Laptop 15 inch 8GB',
  777. 'Laptop 13 inch 16GB',
  778. 'Laptop 15 inch 16GB',
  779. 'Curvy Monitor 24 inch',
  780. 'Curvy Monitor 27 inch',
  781. 'Gaming PC i7-8700 240GB SSD',
  782. 'Gaming PC R7-2700 240GB SSD',
  783. 'Gaming PC i7-8700 120GB SSD',
  784. 'Gaming PC R7-2700 120GB SSD',
  785. 'Hard Drive 1TB',
  786. 'Hard Drive 2TB',
  787. 'Hard Drive 3TB',
  788. 'Hard Drive 4TB',
  789. 'Hard Drive 6TB',
  790. 'Clacky Keyboard',
  791. 'USB Cable',
  792. 'Instant Camera',
  793. 'Camera Lens',
  794. 'Tripod',
  795. 'SLR Camera',
  796. ]);
  797. });
  798. it('computers', async () => {
  799. const result = await adminClient.query<
  800. GetCollectionProducts.Query,
  801. GetCollectionProducts.Variables
  802. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  803. id: computersCollection.id,
  804. });
  805. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  806. 'Laptop 13 inch 8GB',
  807. 'Laptop 15 inch 8GB',
  808. 'Laptop 13 inch 16GB',
  809. 'Laptop 15 inch 16GB',
  810. 'Curvy Monitor 24 inch',
  811. 'Curvy Monitor 27 inch',
  812. 'Gaming PC i7-8700 240GB SSD',
  813. 'Gaming PC R7-2700 240GB SSD',
  814. 'Gaming PC i7-8700 120GB SSD',
  815. 'Gaming PC R7-2700 120GB SSD',
  816. 'Hard Drive 1TB',
  817. 'Hard Drive 2TB',
  818. 'Hard Drive 3TB',
  819. 'Hard Drive 4TB',
  820. 'Hard Drive 6TB',
  821. 'Clacky Keyboard',
  822. 'USB Cable',
  823. ]);
  824. });
  825. it('photo AND pear', async () => {
  826. const result = await adminClient.query<
  827. CreateCollectionSelectVariants.Mutation,
  828. CreateCollectionSelectVariants.Variables
  829. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  830. input: {
  831. translations: [
  832. {
  833. languageCode: LanguageCode.en,
  834. name: 'Photo AND Pear',
  835. description: '',
  836. slug: 'photo-and-pear',
  837. },
  838. ],
  839. filters: [
  840. {
  841. code: facetValueCollectionFilter.code,
  842. arguments: [
  843. {
  844. name: 'facetValueIds',
  845. value: `["${getFacetValueId('pear')}", "${getFacetValueId(
  846. 'photo',
  847. )}"]`,
  848. },
  849. {
  850. name: 'containsAny',
  851. value: `false`,
  852. },
  853. ],
  854. },
  855. ],
  856. } as CreateCollectionInput,
  857. });
  858. await awaitRunningJobs(adminClient);
  859. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  860. GET_COLLECTION,
  861. {
  862. id: result.createCollection.id,
  863. },
  864. );
  865. expect(collection!.productVariants.items.map(i => i.name)).toEqual(['Instant Camera']);
  866. });
  867. it('photo OR pear', async () => {
  868. const result = await adminClient.query<
  869. CreateCollectionSelectVariants.Mutation,
  870. CreateCollectionSelectVariants.Variables
  871. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  872. input: {
  873. translations: [
  874. {
  875. languageCode: LanguageCode.en,
  876. name: 'Photo OR Pear',
  877. description: '',
  878. slug: 'photo-or-pear',
  879. },
  880. ],
  881. filters: [
  882. {
  883. code: facetValueCollectionFilter.code,
  884. arguments: [
  885. {
  886. name: 'facetValueIds',
  887. value: `["${getFacetValueId('pear')}", "${getFacetValueId(
  888. 'photo',
  889. )}"]`,
  890. },
  891. {
  892. name: 'containsAny',
  893. value: `true`,
  894. },
  895. ],
  896. },
  897. ],
  898. } as CreateCollectionInput,
  899. });
  900. await awaitRunningJobs(adminClient);
  901. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  902. GET_COLLECTION,
  903. {
  904. id: result.createCollection.id,
  905. },
  906. );
  907. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  908. 'Laptop 13 inch 8GB',
  909. 'Laptop 15 inch 8GB',
  910. 'Laptop 13 inch 16GB',
  911. 'Laptop 15 inch 16GB',
  912. 'Instant Camera',
  913. 'Camera Lens',
  914. 'Tripod',
  915. 'SLR Camera',
  916. 'Hat',
  917. ]);
  918. });
  919. it('bell OR pear in computers', async () => {
  920. const result = await adminClient.query<
  921. CreateCollectionSelectVariants.Mutation,
  922. CreateCollectionSelectVariants.Variables
  923. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  924. input: {
  925. parentId: computersCollection.id,
  926. translations: [
  927. {
  928. languageCode: LanguageCode.en,
  929. name: 'Bell OR Pear Computers',
  930. description: '',
  931. slug: 'bell-or-pear',
  932. },
  933. ],
  934. filters: [
  935. {
  936. code: facetValueCollectionFilter.code,
  937. arguments: [
  938. {
  939. name: 'facetValueIds',
  940. value: `["${getFacetValueId('pear')}", "${getFacetValueId('bell')}"]`,
  941. },
  942. {
  943. name: 'containsAny',
  944. value: `true`,
  945. },
  946. ],
  947. },
  948. ],
  949. } as CreateCollectionInput,
  950. });
  951. await awaitRunningJobs(adminClient);
  952. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  953. GET_COLLECTION,
  954. {
  955. id: result.createCollection.id,
  956. },
  957. );
  958. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  959. 'Laptop 13 inch 8GB',
  960. 'Laptop 15 inch 8GB',
  961. 'Laptop 13 inch 16GB',
  962. 'Laptop 15 inch 16GB',
  963. 'Curvy Monitor 24 inch',
  964. 'Curvy Monitor 27 inch',
  965. ]);
  966. });
  967. });
  968. describe('variantName filter', () => {
  969. async function createVariantNameFilteredCollection(
  970. operator: string,
  971. term: string,
  972. ): Promise<Collection.Fragment> {
  973. const { createCollection } = await adminClient.query<
  974. CreateCollection.Mutation,
  975. CreateCollection.Variables
  976. >(CREATE_COLLECTION, {
  977. input: {
  978. translations: [
  979. {
  980. languageCode: LanguageCode.en,
  981. name: `${operator} ${term}`,
  982. description: '',
  983. slug: `${operator} ${term}`,
  984. },
  985. ],
  986. filters: [
  987. {
  988. code: variantNameCollectionFilter.code,
  989. arguments: [
  990. {
  991. name: 'operator',
  992. value: operator,
  993. },
  994. {
  995. name: 'term',
  996. value: term,
  997. },
  998. ],
  999. },
  1000. ],
  1001. },
  1002. });
  1003. await awaitRunningJobs(adminClient);
  1004. return createCollection;
  1005. }
  1006. it('contains operator', async () => {
  1007. const collection = await createVariantNameFilteredCollection('contains', 'camera');
  1008. const result = await adminClient.query<
  1009. GetCollectionProducts.Query,
  1010. GetCollectionProducts.Variables
  1011. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1012. id: collection.id,
  1013. });
  1014. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1015. 'Instant Camera',
  1016. 'Camera Lens',
  1017. 'SLR Camera',
  1018. ]);
  1019. });
  1020. it('startsWith operator', async () => {
  1021. const collection = await createVariantNameFilteredCollection('startsWith', 'camera');
  1022. const result = await adminClient.query<
  1023. GetCollectionProducts.Query,
  1024. GetCollectionProducts.Variables
  1025. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1026. id: collection.id,
  1027. });
  1028. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual(['Camera Lens']);
  1029. });
  1030. it('endsWith operator', async () => {
  1031. const collection = await createVariantNameFilteredCollection('endsWith', 'camera');
  1032. const result = await adminClient.query<
  1033. GetCollectionProducts.Query,
  1034. GetCollectionProducts.Variables
  1035. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1036. id: collection.id,
  1037. });
  1038. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1039. 'Instant Camera',
  1040. 'SLR Camera',
  1041. ]);
  1042. });
  1043. it('doesNotContain operator', async () => {
  1044. const collection = await createVariantNameFilteredCollection('doesNotContain', 'camera');
  1045. const result = await adminClient.query<
  1046. GetCollectionProducts.Query,
  1047. GetCollectionProducts.Variables
  1048. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1049. id: collection.id,
  1050. });
  1051. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1052. 'Laptop 13 inch 8GB',
  1053. 'Laptop 15 inch 8GB',
  1054. 'Laptop 13 inch 16GB',
  1055. 'Laptop 15 inch 16GB',
  1056. 'Curvy Monitor 24 inch',
  1057. 'Curvy Monitor 27 inch',
  1058. 'Gaming PC i7-8700 240GB SSD',
  1059. 'Gaming PC R7-2700 240GB SSD',
  1060. 'Gaming PC i7-8700 120GB SSD',
  1061. 'Gaming PC R7-2700 120GB SSD',
  1062. 'Hard Drive 1TB',
  1063. 'Hard Drive 2TB',
  1064. 'Hard Drive 3TB',
  1065. 'Hard Drive 4TB',
  1066. 'Hard Drive 6TB',
  1067. 'Clacky Keyboard',
  1068. 'USB Cable',
  1069. 'Tripod',
  1070. 'Hat',
  1071. ]);
  1072. });
  1073. });
  1074. describe('re-evaluation of contents on changes', () => {
  1075. let products: GetProductsWithVariantIds.Items[];
  1076. beforeAll(async () => {
  1077. const result = await adminClient.query<GetProductsWithVariantIds.Query>(gql`
  1078. query GetProductsWithVariantIds {
  1079. products(options: { sort: { id: ASC } }) {
  1080. items {
  1081. id
  1082. name
  1083. variants {
  1084. id
  1085. name
  1086. }
  1087. }
  1088. }
  1089. }
  1090. `);
  1091. products = result.products.items;
  1092. });
  1093. it('updates contents when Product is updated', async () => {
  1094. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  1095. input: {
  1096. id: products[1].id,
  1097. facetValueIds: [
  1098. getFacetValueId('electronics'),
  1099. getFacetValueId('computers'),
  1100. getFacetValueId('pear'),
  1101. ],
  1102. },
  1103. });
  1104. await awaitRunningJobs(adminClient);
  1105. const result = await adminClient.query<
  1106. GetCollectionProducts.Query,
  1107. GetCollectionProducts.Variables
  1108. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  1109. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1110. 'Laptop 13 inch 8GB',
  1111. 'Laptop 15 inch 8GB',
  1112. 'Laptop 13 inch 16GB',
  1113. 'Laptop 15 inch 16GB',
  1114. 'Curvy Monitor 24 inch',
  1115. 'Curvy Monitor 27 inch',
  1116. 'Instant Camera',
  1117. ]);
  1118. });
  1119. it('updates contents when ProductVariant is updated', async () => {
  1120. const gamingPc240GB = products
  1121. .find(p => p.name === 'Gaming PC')!
  1122. .variants.find(v => v.name.includes('240GB'))!;
  1123. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1124. UPDATE_PRODUCT_VARIANTS,
  1125. {
  1126. input: [
  1127. {
  1128. id: gamingPc240GB.id,
  1129. facetValueIds: [getFacetValueId('pear')],
  1130. },
  1131. ],
  1132. },
  1133. );
  1134. await awaitRunningJobs(adminClient);
  1135. const result = await adminClient.query<
  1136. GetCollectionProducts.Query,
  1137. GetCollectionProducts.Variables
  1138. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  1139. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1140. 'Laptop 13 inch 8GB',
  1141. 'Laptop 15 inch 8GB',
  1142. 'Laptop 13 inch 16GB',
  1143. 'Laptop 15 inch 16GB',
  1144. 'Curvy Monitor 24 inch',
  1145. 'Curvy Monitor 27 inch',
  1146. 'Gaming PC i7-8700 240GB SSD',
  1147. 'Instant Camera',
  1148. ]);
  1149. });
  1150. it('correctly filters when ProductVariant and Product both have matching FacetValue', async () => {
  1151. const gamingPc240GB = products
  1152. .find(p => p.name === 'Gaming PC')!
  1153. .variants.find(v => v.name.includes('240GB'))!;
  1154. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1155. UPDATE_PRODUCT_VARIANTS,
  1156. {
  1157. input: [
  1158. {
  1159. id: gamingPc240GB.id,
  1160. facetValueIds: [getFacetValueId('electronics'), getFacetValueId('pear')],
  1161. },
  1162. ],
  1163. },
  1164. );
  1165. await awaitRunningJobs(adminClient);
  1166. const result = await adminClient.query<
  1167. GetCollectionProducts.Query,
  1168. GetCollectionProducts.Variables
  1169. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  1170. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1171. 'Laptop 13 inch 8GB',
  1172. 'Laptop 15 inch 8GB',
  1173. 'Laptop 13 inch 16GB',
  1174. 'Laptop 15 inch 16GB',
  1175. 'Curvy Monitor 24 inch',
  1176. 'Curvy Monitor 27 inch',
  1177. 'Gaming PC i7-8700 240GB SSD',
  1178. 'Instant Camera',
  1179. ]);
  1180. });
  1181. });
  1182. it('filter inheritance of nested collections (issue #158)', async () => {
  1183. const a = 1;
  1184. const { createCollection: pearElectronics } = await adminClient.query<
  1185. CreateCollectionSelectVariants.Mutation,
  1186. CreateCollectionSelectVariants.Variables
  1187. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  1188. input: {
  1189. parentId: electronicsCollection.id,
  1190. translations: [
  1191. {
  1192. languageCode: LanguageCode.en,
  1193. name: 'pear electronics',
  1194. description: '',
  1195. slug: 'pear-electronics',
  1196. },
  1197. ],
  1198. filters: [
  1199. {
  1200. code: facetValueCollectionFilter.code,
  1201. arguments: [
  1202. {
  1203. name: 'facetValueIds',
  1204. value: `["${getFacetValueId('pear')}"]`,
  1205. },
  1206. {
  1207. name: 'containsAny',
  1208. value: `false`,
  1209. },
  1210. ],
  1211. },
  1212. ],
  1213. } as CreateCollectionInput,
  1214. });
  1215. await awaitRunningJobs(adminClient);
  1216. const result = await adminClient.query<
  1217. GetCollectionProducts.Query,
  1218. GetCollectionProducts.Variables
  1219. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearElectronics.id });
  1220. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1221. 'Laptop 13 inch 8GB',
  1222. 'Laptop 15 inch 8GB',
  1223. 'Laptop 13 inch 16GB',
  1224. 'Laptop 15 inch 16GB',
  1225. 'Curvy Monitor 24 inch',
  1226. 'Curvy Monitor 27 inch',
  1227. 'Gaming PC i7-8700 240GB SSD',
  1228. 'Instant Camera',
  1229. // no "Hat"
  1230. ]);
  1231. });
  1232. });
  1233. describe('Product collections property', () => {
  1234. it('returns all collections to which the Product belongs', async () => {
  1235. const result = await adminClient.query<
  1236. GetCollectionsForProducts.Query,
  1237. GetCollectionsForProducts.Variables
  1238. >(GET_COLLECTIONS_FOR_PRODUCTS, { term: 'camera' });
  1239. expect(result.products.items[0].collections).toEqual([
  1240. { id: 'T_3', name: 'Electronics' },
  1241. { id: 'T_5', name: 'Pear' },
  1242. { id: 'T_11', name: 'Photo AND Pear' },
  1243. { id: 'T_12', name: 'Photo OR Pear' },
  1244. { id: 'T_14', name: 'contains camera' },
  1245. { id: 'T_16', name: 'endsWith camera' },
  1246. { id: 'T_18', name: 'pear electronics' },
  1247. ]);
  1248. });
  1249. });
  1250. it('collection does not list deleted products', async () => {
  1251. await adminClient.query<DeleteProduct.Mutation, DeleteProduct.Variables>(DELETE_PRODUCT, {
  1252. id: 'T_2', // curvy monitor
  1253. });
  1254. const { collection } = await adminClient.query<
  1255. GetCollectionProducts.Query,
  1256. GetCollectionProducts.Variables
  1257. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1258. id: pearCollection.id,
  1259. });
  1260. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  1261. 'Laptop 13 inch 8GB',
  1262. 'Laptop 15 inch 8GB',
  1263. 'Laptop 13 inch 16GB',
  1264. 'Laptop 15 inch 16GB',
  1265. 'Gaming PC i7-8700 240GB SSD',
  1266. 'Instant Camera',
  1267. ]);
  1268. });
  1269. function getFacetValueId(code: string): string {
  1270. const match = facetValues.find(fv => fv.code === code);
  1271. if (!match) {
  1272. throw new Error(`Could not find a FacetValue with the code "${code}"`);
  1273. }
  1274. return match.id;
  1275. }
  1276. });
  1277. export const GET_COLLECTION = gql`
  1278. query GetCollection($id: ID, $slug: String, $variantListOptions: ProductVariantListOptions) {
  1279. collection(id: $id, slug: $slug) {
  1280. ...Collection
  1281. productVariants(options: $variantListOptions) {
  1282. items {
  1283. id
  1284. name
  1285. price
  1286. }
  1287. }
  1288. }
  1289. }
  1290. ${COLLECTION_FRAGMENT}
  1291. `;
  1292. export const MOVE_COLLECTION = gql`
  1293. mutation MoveCollection($input: MoveCollectionInput!) {
  1294. moveCollection(input: $input) {
  1295. ...Collection
  1296. }
  1297. }
  1298. ${COLLECTION_FRAGMENT}
  1299. `;
  1300. const GET_FACET_VALUES = gql`
  1301. query GetFacetValues {
  1302. facets {
  1303. items {
  1304. values {
  1305. ...FacetValue
  1306. }
  1307. }
  1308. }
  1309. }
  1310. ${FACET_VALUE_FRAGMENT}
  1311. `;
  1312. const GET_COLLECTIONS = gql`
  1313. query GetCollections {
  1314. collections {
  1315. items {
  1316. id
  1317. name
  1318. position
  1319. parent {
  1320. id
  1321. name
  1322. }
  1323. }
  1324. }
  1325. }
  1326. `;
  1327. const GET_COLLECTION_PRODUCT_VARIANTS = gql`
  1328. query GetCollectionProducts($id: ID!) {
  1329. collection(id: $id) {
  1330. productVariants(options: { sort: { id: ASC } }) {
  1331. items {
  1332. id
  1333. name
  1334. facetValues {
  1335. code
  1336. }
  1337. productId
  1338. }
  1339. }
  1340. }
  1341. }
  1342. `;
  1343. const CREATE_COLLECTION_SELECT_VARIANTS = gql`
  1344. mutation CreateCollectionSelectVariants($input: CreateCollectionInput!) {
  1345. createCollection(input: $input) {
  1346. id
  1347. productVariants {
  1348. items {
  1349. name
  1350. }
  1351. totalItems
  1352. }
  1353. }
  1354. }
  1355. `;
  1356. const GET_COLLECTION_BREADCRUMBS = gql`
  1357. query GetCollectionBreadcrumbs($id: ID!) {
  1358. collection(id: $id) {
  1359. breadcrumbs {
  1360. id
  1361. name
  1362. slug
  1363. }
  1364. }
  1365. }
  1366. `;
  1367. const GET_COLLECTIONS_FOR_PRODUCTS = gql`
  1368. query GetCollectionsForProducts($term: String!) {
  1369. products(options: { filter: { name: { contains: $term } } }) {
  1370. items {
  1371. id
  1372. name
  1373. collections {
  1374. id
  1375. name
  1376. }
  1377. }
  1378. }
  1379. }
  1380. `;
  1381. const DELETE_COLLECTION = gql`
  1382. mutation DeleteCollection($id: ID!) {
  1383. deleteCollection(id: $id) {
  1384. result
  1385. message
  1386. }
  1387. }
  1388. `;
  1389. const GET_PRODUCT_COLLECTIONS = gql`
  1390. query GetProductCollections($id: ID!) {
  1391. product(id: $id) {
  1392. id
  1393. collections {
  1394. id
  1395. name
  1396. }
  1397. }
  1398. }
  1399. `;
  1400. const GET_PRODUCT_COLLECTIONS_WITH_PARENT = gql`
  1401. query GetProductCollectionsWithParent($id: ID!) {
  1402. product(id: $id) {
  1403. id
  1404. collections {
  1405. id
  1406. name
  1407. parent {
  1408. id
  1409. name
  1410. }
  1411. }
  1412. }
  1413. }
  1414. `;