collection.e2e-spec.ts 39 KB

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