collection.e2e-spec.ts 41 KB

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