collection.e2e-spec.ts 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /* tslint:disable:no-non-null-assertion */
  2. import { ROOT_COLLECTION_NAME } from '@vendure/common/lib/shared-constants';
  3. import gql from 'graphql-tag';
  4. import path from 'path';
  5. import { pick } from '../../common/lib/pick';
  6. import { StringOperator } from '../src/common/configurable-operation';
  7. import {
  8. facetValueCollectionFilter,
  9. variantNameCollectionFilter,
  10. } from '../src/config/collection/default-collection-filters';
  11. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  12. import { COLLECTION_FRAGMENT, FACET_VALUE_FRAGMENT } from './graphql/fragments';
  13. import {
  14. Collection,
  15. ConfigArgType,
  16. CreateCollection,
  17. CreateCollectionInput,
  18. CreateCollectionSelectVariants,
  19. DeleteCollection,
  20. DeleteProduct,
  21. DeletionResult,
  22. FacetValueFragment,
  23. GetAssetList,
  24. GetCollection,
  25. GetCollectionBreadcrumbs,
  26. GetCollectionProducts,
  27. GetCollections,
  28. GetCollectionsForProducts,
  29. GetFacetValues,
  30. GetProductCollections,
  31. GetProductsWithVariantIds,
  32. LanguageCode,
  33. MoveCollection,
  34. SortOrder,
  35. UpdateCollection,
  36. UpdateProduct,
  37. UpdateProductVariants,
  38. } from './graphql/generated-e2e-admin-types';
  39. import {
  40. CREATE_COLLECTION,
  41. DELETE_PRODUCT,
  42. GET_ASSET_LIST,
  43. UPDATE_COLLECTION,
  44. UPDATE_PRODUCT,
  45. UPDATE_PRODUCT_VARIANTS,
  46. } from './graphql/shared-definitions';
  47. import { TestAdminClient } from './test-client';
  48. import { TestServer } from './test-server';
  49. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  50. describe('Collection resolver', () => {
  51. const client = new TestAdminClient();
  52. const server = new TestServer();
  53. let assets: GetAssetList.Items[];
  54. let facetValues: FacetValueFragment[];
  55. let electronicsCollection: Collection.Fragment;
  56. let computersCollection: Collection.Fragment;
  57. let pearCollection: Collection.Fragment;
  58. beforeAll(async () => {
  59. const token = await server.init({
  60. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-collections.csv'),
  61. customerCount: 1,
  62. });
  63. await client.init();
  64. const assetsResult = await client.query<GetAssetList.Query, GetAssetList.Variables>(GET_ASSET_LIST, {
  65. options: {
  66. sort: {
  67. name: SortOrder.ASC,
  68. },
  69. },
  70. });
  71. assets = assetsResult.assets.items;
  72. const facetValuesResult = await client.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 client.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 client.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: ConfigArgType.FACET_VALUE_IDS,
  106. },
  107. {
  108. name: 'containsAny',
  109. value: `false`,
  110. type: ConfigArgType.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 category', async () => {
  126. const result = await client.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: ConfigArgType.FACET_VALUE_IDS,
  140. },
  141. {
  142. name: 'containsAny',
  143. value: `false`,
  144. type: ConfigArgType.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 category', async () => {
  156. const result = await client.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: ConfigArgType.FACET_VALUE_IDS,
  170. },
  171. {
  172. name: 'containsAny',
  173. value: `false`,
  174. type: ConfigArgType.BOOLEAN,
  175. },
  176. ],
  177. },
  178. ],
  179. },
  180. },
  181. );
  182. pearCollection = result.createCollection;
  183. expect(pearCollection.parent!.name).toBe(computersCollection.name);
  184. });
  185. });
  186. it('collection query', async () => {
  187. const result = await client.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
  188. id: computersCollection.id,
  189. });
  190. if (!result.collection) {
  191. fail(`did not return the collection`);
  192. return;
  193. }
  194. expect(result.collection.id).toBe(computersCollection.id);
  195. });
  196. it('parent field', async () => {
  197. const result = await client.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
  198. id: computersCollection.id,
  199. });
  200. if (!result.collection) {
  201. fail(`did not return the collection`);
  202. return;
  203. }
  204. expect(result.collection.parent!.name).toBe('Electronics');
  205. });
  206. it('children field', async () => {
  207. const result = await client.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
  208. id: electronicsCollection.id,
  209. });
  210. if (!result.collection) {
  211. fail(`did not return the collection`);
  212. return;
  213. }
  214. expect(result.collection.children!.length).toBe(1);
  215. expect(result.collection.children![0].name).toBe('Computers');
  216. });
  217. it('breadcrumbs', async () => {
  218. const result = await client.query<GetCollectionBreadcrumbs.Query, GetCollectionBreadcrumbs.Variables>(
  219. GET_COLLECTION_BREADCRUMBS,
  220. {
  221. id: pearCollection.id,
  222. },
  223. );
  224. if (!result.collection) {
  225. fail(`did not return the collection`);
  226. return;
  227. }
  228. expect(result.collection.breadcrumbs).toEqual([
  229. { id: 'T_1', name: ROOT_COLLECTION_NAME },
  230. { id: electronicsCollection.id, name: electronicsCollection.name },
  231. { id: computersCollection.id, name: computersCollection.name },
  232. { id: pearCollection.id, name: pearCollection.name },
  233. ]);
  234. });
  235. it('breadcrumbs for root collection', async () => {
  236. const result = await client.query<GetCollectionBreadcrumbs.Query, GetCollectionBreadcrumbs.Variables>(
  237. GET_COLLECTION_BREADCRUMBS,
  238. {
  239. id: 'T_1',
  240. },
  241. );
  242. if (!result.collection) {
  243. fail(`did not return the collection`);
  244. return;
  245. }
  246. expect(result.collection.breadcrumbs).toEqual([{ id: 'T_1', name: ROOT_COLLECTION_NAME }]);
  247. });
  248. it('updateCollection', async () => {
  249. const { updateCollection } = await client.query<
  250. UpdateCollection.Mutation,
  251. UpdateCollection.Variables
  252. >(UPDATE_COLLECTION, {
  253. input: {
  254. id: pearCollection.id,
  255. assetIds: [assets[1].id],
  256. featuredAssetId: assets[1].id,
  257. translations: [{ languageCode: LanguageCode.en, description: 'Apple stuff ' }],
  258. },
  259. });
  260. expect(updateCollection).toMatchSnapshot();
  261. });
  262. describe('moveCollection', () => {
  263. it('moves a collection to a new parent', async () => {
  264. const result = await client.query<MoveCollection.Mutation, MoveCollection.Variables>(
  265. MOVE_COLLECTION,
  266. {
  267. input: {
  268. collectionId: pearCollection.id,
  269. parentId: electronicsCollection.id,
  270. index: 0,
  271. },
  272. },
  273. );
  274. expect(result.moveCollection.parent!.id).toBe(electronicsCollection.id);
  275. const positions = await getChildrenOf(electronicsCollection.id);
  276. expect(positions.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  277. });
  278. it('re-evaluates Collection contents on move', async () => {
  279. const result = await client.query<GetCollectionProducts.Query, GetCollectionProducts.Variables>(
  280. GET_COLLECTION_PRODUCT_VARIANTS,
  281. { id: pearCollection.id },
  282. );
  283. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  284. 'Laptop 13 inch 8GB',
  285. 'Laptop 15 inch 8GB',
  286. 'Laptop 13 inch 16GB',
  287. 'Laptop 15 inch 16GB',
  288. 'Instant Camera',
  289. ]);
  290. });
  291. it('alters the position in the current parent 1', async () => {
  292. await client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  293. input: {
  294. collectionId: computersCollection.id,
  295. parentId: electronicsCollection.id,
  296. index: 0,
  297. },
  298. });
  299. const afterResult = await getChildrenOf(electronicsCollection.id);
  300. expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
  301. });
  302. it('alters the position in the current parent 2', async () => {
  303. await client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  304. input: {
  305. collectionId: pearCollection.id,
  306. parentId: electronicsCollection.id,
  307. index: 0,
  308. },
  309. });
  310. const afterResult = await getChildrenOf(electronicsCollection.id);
  311. expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  312. });
  313. it('corrects an out-of-bounds negative index value', async () => {
  314. await client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  315. input: {
  316. collectionId: pearCollection.id,
  317. parentId: electronicsCollection.id,
  318. index: -3,
  319. },
  320. });
  321. const afterResult = await getChildrenOf(electronicsCollection.id);
  322. expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  323. });
  324. it('corrects an out-of-bounds positive index value', async () => {
  325. await client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  326. input: {
  327. collectionId: pearCollection.id,
  328. parentId: electronicsCollection.id,
  329. index: 10,
  330. },
  331. });
  332. const afterResult = await getChildrenOf(electronicsCollection.id);
  333. expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
  334. });
  335. it(
  336. 'throws if attempting to move into self',
  337. assertThrowsWithMessage(
  338. () =>
  339. client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  340. input: {
  341. collectionId: pearCollection.id,
  342. parentId: pearCollection.id,
  343. index: 0,
  344. },
  345. }),
  346. `Cannot move a Collection into itself`,
  347. ),
  348. );
  349. it(
  350. 'throws if attempting to move into a decendant of self',
  351. assertThrowsWithMessage(
  352. () =>
  353. client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  354. input: {
  355. collectionId: pearCollection.id,
  356. parentId: pearCollection.id,
  357. index: 0,
  358. },
  359. }),
  360. `Cannot move a Collection into itself`,
  361. ),
  362. );
  363. async function getChildrenOf(parentId: string): Promise<Array<{ name: string; id: string }>> {
  364. const result = await client.query<GetCollections.Query>(GET_COLLECTIONS);
  365. return result.collections.items.filter(i => i.parent!.id === parentId);
  366. }
  367. });
  368. describe('deleteCollection', () => {
  369. let collectionToDelete: CreateCollection.CreateCollection;
  370. let laptopProductId: string;
  371. beforeAll(async () => {
  372. const { createCollection } = await client.query<
  373. CreateCollection.Mutation,
  374. CreateCollection.Variables
  375. >(CREATE_COLLECTION, {
  376. input: {
  377. filters: [
  378. {
  379. code: variantNameCollectionFilter.code,
  380. arguments: [
  381. {
  382. name: 'operator',
  383. value: 'contains',
  384. type: ConfigArgType.STRING_OPERATOR,
  385. },
  386. {
  387. name: 'term',
  388. value: 'laptop',
  389. type: ConfigArgType.STRING,
  390. },
  391. ],
  392. },
  393. ],
  394. translations: [{ languageCode: LanguageCode.en, name: 'Delete Me', description: '' }],
  395. },
  396. });
  397. collectionToDelete = createCollection;
  398. });
  399. it(
  400. 'throws for invalid collection id',
  401. assertThrowsWithMessage(async () => {
  402. await client.query<DeleteCollection.Mutation, DeleteCollection.Variables>(DELETE_COLLECTION, {
  403. id: 'T_999',
  404. });
  405. }, "No Collection with the id '999' could be found"),
  406. );
  407. it('collection and product related prior to deletion', async () => {
  408. const { collection } = await client.query<
  409. GetCollectionProducts.Query,
  410. GetCollectionProducts.Variables
  411. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  412. id: collectionToDelete.id,
  413. });
  414. expect(collection!.productVariants.items.map(pick(['name']))).toEqual([
  415. { name: 'Laptop 13 inch 8GB' },
  416. { name: 'Laptop 15 inch 8GB' },
  417. { name: 'Laptop 13 inch 16GB' },
  418. { name: 'Laptop 15 inch 16GB' },
  419. ]);
  420. laptopProductId = collection!.productVariants.items[0].productId;
  421. const { product } = await client.query<
  422. GetProductCollections.Query,
  423. GetProductCollections.Variables
  424. >(GET_PRODUCT_COLLECTIONS, {
  425. id: laptopProductId,
  426. });
  427. expect(product!.collections).toEqual([
  428. { id: 'T_3', name: 'Electronics' },
  429. { id: 'T_4', name: 'Computers' },
  430. { id: 'T_5', name: 'Pear' },
  431. { id: 'T_6', name: 'Delete Me' },
  432. ]);
  433. });
  434. it('deleteCollection works', async () => {
  435. const { deleteCollection } = await client.query<
  436. DeleteCollection.Mutation,
  437. DeleteCollection.Variables
  438. >(DELETE_COLLECTION, {
  439. id: collectionToDelete.id,
  440. });
  441. expect(deleteCollection.result).toBe(DeletionResult.DELETED);
  442. });
  443. it('deleted collection is null', async () => {
  444. const { collection } = await client.query<GetCollection.Query, GetCollection.Variables>(
  445. GET_COLLECTION,
  446. {
  447. id: collectionToDelete.id,
  448. },
  449. );
  450. expect(collection).toBeNull();
  451. });
  452. it('product no longer lists collection', async () => {
  453. const { product } = await client.query<
  454. GetProductCollections.Query,
  455. GetProductCollections.Variables
  456. >(GET_PRODUCT_COLLECTIONS, {
  457. id: laptopProductId,
  458. });
  459. expect(product!.collections).toEqual([
  460. { id: 'T_3', name: 'Electronics' },
  461. { id: 'T_4', name: 'Computers' },
  462. { id: 'T_5', name: 'Pear' },
  463. ]);
  464. });
  465. });
  466. describe('filters', () => {
  467. it('Collection with no filters has no productVariants', async () => {
  468. const result = await client.query<
  469. CreateCollectionSelectVariants.Mutation,
  470. CreateCollectionSelectVariants.Variables
  471. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  472. input: {
  473. translations: [{ languageCode: LanguageCode.en, name: 'Empty', description: '' }],
  474. filters: [],
  475. } as CreateCollectionInput,
  476. });
  477. expect(result.createCollection.productVariants.totalItems).toBe(0);
  478. });
  479. describe('facetValue filter', () => {
  480. it('electronics', async () => {
  481. const result = await client.query<
  482. GetCollectionProducts.Query,
  483. GetCollectionProducts.Variables
  484. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  485. id: electronicsCollection.id,
  486. });
  487. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  488. 'Laptop 13 inch 8GB',
  489. 'Laptop 15 inch 8GB',
  490. 'Laptop 13 inch 16GB',
  491. 'Laptop 15 inch 16GB',
  492. 'Curvy Monitor 24 inch',
  493. 'Curvy Monitor 27 inch',
  494. 'Gaming PC i7-8700 240GB SSD',
  495. 'Gaming PC R7-2700 240GB SSD',
  496. 'Gaming PC i7-8700 120GB SSD',
  497. 'Gaming PC R7-2700 120GB SSD',
  498. 'Hard Drive 1TB',
  499. 'Hard Drive 2TB',
  500. 'Hard Drive 3TB',
  501. 'Hard Drive 4TB',
  502. 'Hard Drive 6TB',
  503. 'Clacky Keyboard',
  504. 'USB Cable',
  505. 'Instant Camera',
  506. 'Camera Lens',
  507. 'Tripod',
  508. 'SLR Camera',
  509. ]);
  510. });
  511. it('computers', async () => {
  512. const result = await client.query<
  513. GetCollectionProducts.Query,
  514. GetCollectionProducts.Variables
  515. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  516. id: computersCollection.id,
  517. });
  518. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  519. 'Laptop 13 inch 8GB',
  520. 'Laptop 15 inch 8GB',
  521. 'Laptop 13 inch 16GB',
  522. 'Laptop 15 inch 16GB',
  523. 'Curvy Monitor 24 inch',
  524. 'Curvy Monitor 27 inch',
  525. 'Gaming PC i7-8700 240GB SSD',
  526. 'Gaming PC R7-2700 240GB SSD',
  527. 'Gaming PC i7-8700 120GB SSD',
  528. 'Gaming PC R7-2700 120GB SSD',
  529. 'Hard Drive 1TB',
  530. 'Hard Drive 2TB',
  531. 'Hard Drive 3TB',
  532. 'Hard Drive 4TB',
  533. 'Hard Drive 6TB',
  534. 'Clacky Keyboard',
  535. 'USB Cable',
  536. ]);
  537. });
  538. it('photo AND pear', async () => {
  539. const result = await client.query<
  540. CreateCollectionSelectVariants.Mutation,
  541. CreateCollectionSelectVariants.Variables
  542. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  543. input: {
  544. translations: [
  545. { languageCode: LanguageCode.en, name: 'Photo AND Pear', description: '' },
  546. ],
  547. filters: [
  548. {
  549. code: facetValueCollectionFilter.code,
  550. arguments: [
  551. {
  552. name: 'facetValueIds',
  553. value: `["${getFacetValueId('pear')}", "${getFacetValueId(
  554. 'photo',
  555. )}"]`,
  556. type: ConfigArgType.FACET_VALUE_IDS,
  557. },
  558. {
  559. name: 'containsAny',
  560. value: `false`,
  561. type: ConfigArgType.BOOLEAN,
  562. },
  563. ],
  564. },
  565. ],
  566. } as CreateCollectionInput,
  567. });
  568. expect(result.createCollection.productVariants.items.map(i => i.name)).toEqual([
  569. 'Instant Camera',
  570. ]);
  571. });
  572. it('photo OR pear', async () => {
  573. const result = await client.query<
  574. CreateCollectionSelectVariants.Mutation,
  575. CreateCollectionSelectVariants.Variables
  576. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  577. input: {
  578. translations: [
  579. { languageCode: LanguageCode.en, name: 'Photo OR Pear', description: '' },
  580. ],
  581. filters: [
  582. {
  583. code: facetValueCollectionFilter.code,
  584. arguments: [
  585. {
  586. name: 'facetValueIds',
  587. value: `["${getFacetValueId('pear')}", "${getFacetValueId(
  588. 'photo',
  589. )}"]`,
  590. type: ConfigArgType.FACET_VALUE_IDS,
  591. },
  592. {
  593. name: 'containsAny',
  594. value: `true`,
  595. type: ConfigArgType.BOOLEAN,
  596. },
  597. ],
  598. },
  599. ],
  600. } as CreateCollectionInput,
  601. });
  602. expect(result.createCollection.productVariants.items.map(i => i.name)).toEqual([
  603. 'Laptop 13 inch 8GB',
  604. 'Laptop 15 inch 8GB',
  605. 'Laptop 13 inch 16GB',
  606. 'Laptop 15 inch 16GB',
  607. 'Instant Camera',
  608. 'Camera Lens',
  609. 'Tripod',
  610. 'SLR Camera',
  611. ]);
  612. });
  613. });
  614. describe('variantName filter', () => {
  615. async function createVariantNameFilteredCollection(
  616. operator: StringOperator,
  617. term: string,
  618. ): Promise<Collection.Fragment> {
  619. const { createCollection } = await client.query<
  620. CreateCollection.Mutation,
  621. CreateCollection.Variables
  622. >(CREATE_COLLECTION, {
  623. input: {
  624. translations: [
  625. { languageCode: LanguageCode.en, name: `${operator} ${term}`, description: '' },
  626. ],
  627. filters: [
  628. {
  629. code: variantNameCollectionFilter.code,
  630. arguments: [
  631. {
  632. name: 'operator',
  633. value: operator,
  634. type: ConfigArgType.STRING_OPERATOR,
  635. },
  636. {
  637. name: 'term',
  638. value: term,
  639. type: ConfigArgType.STRING,
  640. },
  641. ],
  642. },
  643. ],
  644. },
  645. });
  646. return createCollection;
  647. }
  648. it('contains operator', async () => {
  649. const collection = await createVariantNameFilteredCollection('contains', 'camera');
  650. const result = await client.query<
  651. GetCollectionProducts.Query,
  652. GetCollectionProducts.Variables
  653. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  654. id: collection.id,
  655. });
  656. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  657. 'Instant Camera',
  658. 'Camera Lens',
  659. 'SLR Camera',
  660. ]);
  661. });
  662. it('startsWith operator', async () => {
  663. const collection = await createVariantNameFilteredCollection('startsWith', 'camera');
  664. const result = await client.query<
  665. GetCollectionProducts.Query,
  666. GetCollectionProducts.Variables
  667. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  668. id: collection.id,
  669. });
  670. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual(['Camera Lens']);
  671. });
  672. it('endsWith operator', async () => {
  673. const collection = await createVariantNameFilteredCollection('endsWith', 'camera');
  674. const result = await client.query<
  675. GetCollectionProducts.Query,
  676. GetCollectionProducts.Variables
  677. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  678. id: collection.id,
  679. });
  680. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  681. 'Instant Camera',
  682. 'SLR Camera',
  683. ]);
  684. });
  685. it('doesNotContain operator', async () => {
  686. const collection = await createVariantNameFilteredCollection('doesNotContain', 'camera');
  687. const result = await client.query<
  688. GetCollectionProducts.Query,
  689. GetCollectionProducts.Variables
  690. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  691. id: collection.id,
  692. });
  693. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  694. 'Laptop 13 inch 8GB',
  695. 'Laptop 15 inch 8GB',
  696. 'Laptop 13 inch 16GB',
  697. 'Laptop 15 inch 16GB',
  698. 'Curvy Monitor 24 inch',
  699. 'Curvy Monitor 27 inch',
  700. 'Gaming PC i7-8700 240GB SSD',
  701. 'Gaming PC R7-2700 240GB SSD',
  702. 'Gaming PC i7-8700 120GB SSD',
  703. 'Gaming PC R7-2700 120GB SSD',
  704. 'Hard Drive 1TB',
  705. 'Hard Drive 2TB',
  706. 'Hard Drive 3TB',
  707. 'Hard Drive 4TB',
  708. 'Hard Drive 6TB',
  709. 'Clacky Keyboard',
  710. 'USB Cable',
  711. 'Tripod',
  712. ]);
  713. });
  714. });
  715. describe('re-evaluation of contents on changes', () => {
  716. let products: GetProductsWithVariantIds.Items[];
  717. beforeAll(async () => {
  718. const result = await client.query<GetProductsWithVariantIds.Query>(gql`
  719. query GetProductsWithVariantIds {
  720. products {
  721. items {
  722. id
  723. name
  724. variants {
  725. id
  726. }
  727. }
  728. }
  729. }
  730. `);
  731. products = result.products.items;
  732. });
  733. it('updates contents when Product is updated', async () => {
  734. await client.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  735. input: {
  736. id: products[1].id,
  737. facetValueIds: [
  738. getFacetValueId('electronics'),
  739. getFacetValueId('computers'),
  740. getFacetValueId('pear'),
  741. ],
  742. },
  743. });
  744. const result = await client.query<
  745. GetCollectionProducts.Query,
  746. GetCollectionProducts.Variables
  747. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  748. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  749. 'Laptop 13 inch 8GB',
  750. 'Laptop 15 inch 8GB',
  751. 'Laptop 13 inch 16GB',
  752. 'Laptop 15 inch 16GB',
  753. 'Curvy Monitor 24 inch',
  754. 'Curvy Monitor 27 inch',
  755. 'Instant Camera',
  756. ]);
  757. });
  758. it('updates contents when ProductVariant is updated', async () => {
  759. const gamingPcFirstVariant = products.find(p => p.name === 'Gaming PC')!.variants[0];
  760. await client.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  761. UPDATE_PRODUCT_VARIANTS,
  762. {
  763. input: [
  764. {
  765. id: gamingPcFirstVariant.id,
  766. facetValueIds: [getFacetValueId('pear')],
  767. },
  768. ],
  769. },
  770. );
  771. const result = await client.query<
  772. GetCollectionProducts.Query,
  773. GetCollectionProducts.Variables
  774. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  775. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  776. 'Laptop 13 inch 8GB',
  777. 'Laptop 15 inch 8GB',
  778. 'Laptop 13 inch 16GB',
  779. 'Laptop 15 inch 16GB',
  780. 'Curvy Monitor 24 inch',
  781. 'Curvy Monitor 27 inch',
  782. 'Gaming PC i7-8700 240GB SSD',
  783. 'Instant Camera',
  784. ]);
  785. });
  786. it('correctly filters when ProductVariant and Product both have matching FacetValue', async () => {
  787. const gamingPcFirstVariant = products.find(p => p.name === 'Gaming PC')!.variants[0];
  788. await client.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  789. UPDATE_PRODUCT_VARIANTS,
  790. {
  791. input: [
  792. {
  793. id: gamingPcFirstVariant.id,
  794. facetValueIds: [getFacetValueId('electronics'), getFacetValueId('pear')],
  795. },
  796. ],
  797. },
  798. );
  799. const result = await client.query<
  800. GetCollectionProducts.Query,
  801. GetCollectionProducts.Variables
  802. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  803. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  804. 'Laptop 13 inch 8GB',
  805. 'Laptop 15 inch 8GB',
  806. 'Laptop 13 inch 16GB',
  807. 'Laptop 15 inch 16GB',
  808. 'Curvy Monitor 24 inch',
  809. 'Curvy Monitor 27 inch',
  810. 'Gaming PC i7-8700 240GB SSD',
  811. 'Instant Camera',
  812. ]);
  813. });
  814. });
  815. });
  816. describe('Product collections property', () => {
  817. it('returns all collections to which the Product belongs', async () => {
  818. const result = await client.query<
  819. GetCollectionsForProducts.Query,
  820. GetCollectionsForProducts.Variables
  821. >(GET_COLLECTIONS_FOR_PRODUCTS, { term: 'camera' });
  822. expect(result.products.items[0].collections).toEqual([
  823. { id: 'T_3', name: 'Electronics' },
  824. { id: 'T_5', name: 'Pear' },
  825. { id: 'T_8', name: 'Photo AND Pear' },
  826. { id: 'T_9', name: 'Photo OR Pear' },
  827. { id: 'T_10', name: 'contains camera' },
  828. { id: 'T_12', name: 'endsWith camera' },
  829. ]);
  830. });
  831. });
  832. it('collection does not list deleted products', async () => {
  833. await client.query<DeleteProduct.Mutation, DeleteProduct.Variables>(DELETE_PRODUCT, {
  834. id: 'T_2', // curvy monitor
  835. });
  836. const { collection } = await client.query<
  837. GetCollectionProducts.Query,
  838. GetCollectionProducts.Variables
  839. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  840. id: pearCollection.id,
  841. });
  842. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  843. 'Laptop 13 inch 8GB',
  844. 'Laptop 15 inch 8GB',
  845. 'Laptop 13 inch 16GB',
  846. 'Laptop 15 inch 16GB',
  847. 'Gaming PC i7-8700 240GB SSD',
  848. 'Instant Camera',
  849. ]);
  850. });
  851. function getFacetValueId(code: string): string {
  852. const match = facetValues.find(fv => fv.code === code);
  853. if (!match) {
  854. throw new Error(`Could not find a FacetValue with the code "${code}"`);
  855. }
  856. return match.id;
  857. }
  858. });
  859. export const GET_COLLECTION = gql`
  860. query GetCollection($id: ID!) {
  861. collection(id: $id) {
  862. ...Collection
  863. }
  864. }
  865. ${COLLECTION_FRAGMENT}
  866. `;
  867. export const MOVE_COLLECTION = gql`
  868. mutation MoveCollection($input: MoveCollectionInput!) {
  869. moveCollection(input: $input) {
  870. ...Collection
  871. }
  872. }
  873. ${COLLECTION_FRAGMENT}
  874. `;
  875. const GET_FACET_VALUES = gql`
  876. query GetFacetValues {
  877. facets {
  878. items {
  879. values {
  880. ...FacetValue
  881. }
  882. }
  883. }
  884. }
  885. ${FACET_VALUE_FRAGMENT}
  886. `;
  887. const GET_COLLECTIONS = gql`
  888. query GetCollections {
  889. collections {
  890. items {
  891. id
  892. name
  893. position
  894. parent {
  895. id
  896. name
  897. }
  898. }
  899. }
  900. }
  901. `;
  902. const GET_COLLECTION_PRODUCT_VARIANTS = gql`
  903. query GetCollectionProducts($id: ID!) {
  904. collection(id: $id) {
  905. productVariants {
  906. items {
  907. id
  908. name
  909. facetValues {
  910. code
  911. }
  912. productId
  913. }
  914. }
  915. }
  916. }
  917. `;
  918. const CREATE_COLLECTION_SELECT_VARIANTS = gql`
  919. mutation CreateCollectionSelectVariants($input: CreateCollectionInput!) {
  920. createCollection(input: $input) {
  921. productVariants {
  922. items {
  923. name
  924. }
  925. totalItems
  926. }
  927. }
  928. }
  929. `;
  930. const GET_COLLECTION_BREADCRUMBS = gql`
  931. query GetCollectionBreadcrumbs($id: ID!) {
  932. collection(id: $id) {
  933. breadcrumbs {
  934. id
  935. name
  936. }
  937. }
  938. }
  939. `;
  940. const GET_COLLECTIONS_FOR_PRODUCTS = gql`
  941. query GetCollectionsForProducts($term: String!) {
  942. products(options: { filter: { name: { contains: $term } } }) {
  943. items {
  944. id
  945. name
  946. collections {
  947. id
  948. name
  949. }
  950. }
  951. }
  952. }
  953. `;
  954. const DELETE_COLLECTION = gql`
  955. mutation DeleteCollection($id: ID!) {
  956. deleteCollection(id: $id) {
  957. result
  958. message
  959. }
  960. }
  961. `;
  962. const GET_PRODUCT_COLLECTIONS = gql`
  963. query GetProductCollections($id: ID!) {
  964. product(id: $id) {
  965. id
  966. collections {
  967. id
  968. name
  969. }
  970. }
  971. }
  972. `;