collection.e2e-spec.ts 48 KB

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