collection.e2e-spec.ts 48 KB

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