collection.e2e-spec.ts 47 KB

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