collection.e2e-spec.ts 40 KB

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