collection.e2e-spec.ts 48 KB

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