collection.e2e-spec.ts 48 KB

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