collection.e2e-spec.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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 { StringOperator } from '../src/common/configurable-operation';
  6. import { facetValueCollectionFilter, variantNameCollectionFilter } from '../src/config/collection/default-collection-filters';
  7. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  8. import { COLLECTION_FRAGMENT, FACET_VALUE_FRAGMENT } from './graphql/fragments';
  9. import {
  10. Collection,
  11. ConfigArgType,
  12. CreateCollection,
  13. CreateCollectionInput,
  14. CreateCollectionSelectVariants,
  15. FacetValueFragment,
  16. GetAssetList,
  17. GetCollection,
  18. GetCollectionBreadcrumbs,
  19. GetCollectionProducts,
  20. GetCollections,
  21. GetCollectionsForProducts,
  22. GetFacetValues,
  23. GetProductsWithVariantIds,
  24. LanguageCode,
  25. MoveCollection,
  26. SortOrder,
  27. UpdateCollection,
  28. UpdateProduct,
  29. UpdateProductVariants,
  30. } from './graphql/generated-e2e-admin-types';
  31. import { CREATE_COLLECTION, GET_ASSET_LIST, UPDATE_COLLECTION, UPDATE_PRODUCT, UPDATE_PRODUCT_VARIANTS } from './graphql/shared-definitions';
  32. import { TestAdminClient } from './test-client';
  33. import { TestServer } from './test-server';
  34. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  35. describe('Collection resolver', () => {
  36. const client = new TestAdminClient();
  37. const server = new TestServer();
  38. let assets: GetAssetList.Items[];
  39. let facetValues: FacetValueFragment[];
  40. let electronicsCollection: Collection.Fragment;
  41. let computersCollection: Collection.Fragment;
  42. let pearCollection: Collection.Fragment;
  43. beforeAll(async () => {
  44. const token = await server.init({
  45. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-collections.csv'),
  46. customerCount: 1,
  47. });
  48. await client.init();
  49. const assetsResult = await client.query<GetAssetList.Query, GetAssetList.Variables>(GET_ASSET_LIST, {
  50. options: {
  51. sort: {
  52. name: SortOrder.ASC,
  53. },
  54. },
  55. });
  56. assets = assetsResult.assets.items;
  57. const facetValuesResult = await client.query<GetFacetValues.Query>(GET_FACET_VALUES);
  58. facetValues = facetValuesResult.facets.items.reduce(
  59. (values, facet) => [...values, ...facet.values],
  60. [] as FacetValueFragment[],
  61. );
  62. }, TEST_SETUP_TIMEOUT_MS);
  63. afterAll(async () => {
  64. await server.destroy();
  65. });
  66. describe('createCollection', () => {
  67. it('creates a root collection', async () => {
  68. const result = await client.query<CreateCollection.Mutation, CreateCollection.Variables>(
  69. CREATE_COLLECTION,
  70. {
  71. input: {
  72. assetIds: [assets[0].id, assets[1].id],
  73. featuredAssetId: assets[1].id,
  74. filters: [
  75. {
  76. code: facetValueCollectionFilter.code,
  77. arguments: [
  78. {
  79. name: 'facetValueIds',
  80. value: `["${getFacetValueId('electronics')}"]`,
  81. type: ConfigArgType.FACET_VALUE_IDS,
  82. },
  83. ],
  84. },
  85. ],
  86. translations: [
  87. { languageCode: LanguageCode.en, name: 'Electronics', description: '' },
  88. ],
  89. },
  90. },
  91. );
  92. electronicsCollection = result.createCollection;
  93. expect(electronicsCollection).toMatchSnapshot();
  94. expect(electronicsCollection.parent!.name).toBe(ROOT_COLLECTION_NAME);
  95. });
  96. it('creates a nested category', async () => {
  97. const result = await client.query<CreateCollection.Mutation, CreateCollection.Variables>(
  98. CREATE_COLLECTION,
  99. {
  100. input: {
  101. parentId: electronicsCollection.id,
  102. translations: [{ languageCode: LanguageCode.en, name: 'Computers', description: '' }],
  103. filters: [
  104. {
  105. code: facetValueCollectionFilter.code,
  106. arguments: [
  107. {
  108. name: 'facetValueIds',
  109. value: `["${getFacetValueId('computers')}"]`,
  110. type: ConfigArgType.FACET_VALUE_IDS,
  111. },
  112. ],
  113. },
  114. ],
  115. },
  116. },
  117. );
  118. computersCollection = result.createCollection;
  119. expect(computersCollection.parent!.name).toBe(electronicsCollection.name);
  120. });
  121. it('creates a 2nd level nested category', async () => {
  122. const result = await client.query<CreateCollection.Mutation, CreateCollection.Variables>(
  123. CREATE_COLLECTION,
  124. {
  125. input: {
  126. parentId: computersCollection.id,
  127. translations: [{ languageCode: LanguageCode.en, name: 'Pear', description: '' }],
  128. filters: [
  129. {
  130. code: facetValueCollectionFilter.code,
  131. arguments: [
  132. {
  133. name: 'facetValueIds',
  134. value: `["${getFacetValueId('pear')}"]`,
  135. type: ConfigArgType.FACET_VALUE_IDS,
  136. },
  137. ],
  138. },
  139. ],
  140. },
  141. },
  142. );
  143. pearCollection = result.createCollection;
  144. expect(pearCollection.parent!.name).toBe(computersCollection.name);
  145. });
  146. });
  147. it('collection query', async () => {
  148. const result = await client.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
  149. id: computersCollection.id,
  150. });
  151. if (!result.collection) {
  152. fail(`did not return the collection`);
  153. return;
  154. }
  155. expect(result.collection.id).toBe(computersCollection.id);
  156. });
  157. it('breadcrumbs', async () => {
  158. const result = await client.query<GetCollectionBreadcrumbs.Query, GetCollectionBreadcrumbs.Variables>(
  159. GET_COLLECTION_BREADCRUMBS,
  160. {
  161. id: pearCollection.id,
  162. },
  163. );
  164. if (!result.collection) {
  165. fail(`did not return the collection`);
  166. return;
  167. }
  168. expect(result.collection.breadcrumbs).toEqual([
  169. { id: 'T_1', name: ROOT_COLLECTION_NAME },
  170. { id: electronicsCollection.id, name: electronicsCollection.name },
  171. { id: computersCollection.id, name: computersCollection.name },
  172. { id: pearCollection.id, name: pearCollection.name },
  173. ]);
  174. });
  175. it('breadcrumbs for root collection', async () => {
  176. const result = await client.query<GetCollectionBreadcrumbs.Query, GetCollectionBreadcrumbs.Variables>(
  177. GET_COLLECTION_BREADCRUMBS,
  178. {
  179. id: 'T_1',
  180. },
  181. );
  182. if (!result.collection) {
  183. fail(`did not return the collection`);
  184. return;
  185. }
  186. expect(result.collection.breadcrumbs).toEqual([{ id: 'T_1', name: ROOT_COLLECTION_NAME }]);
  187. });
  188. it('updateCollection', async () => {
  189. const { updateCollection } = await client.query<
  190. UpdateCollection.Mutation,
  191. UpdateCollection.Variables
  192. >(UPDATE_COLLECTION, {
  193. input: {
  194. id: pearCollection.id,
  195. assetIds: [assets[1].id],
  196. featuredAssetId: assets[1].id,
  197. translations: [{ languageCode: LanguageCode.en, description: 'Apple stuff ' }],
  198. },
  199. });
  200. expect(updateCollection).toMatchSnapshot();
  201. });
  202. describe('moveCollection', () => {
  203. it('moves a collection to a new parent', async () => {
  204. const result = await client.query<MoveCollection.Mutation, MoveCollection.Variables>(
  205. MOVE_COLLECTION,
  206. {
  207. input: {
  208. collectionId: pearCollection.id,
  209. parentId: electronicsCollection.id,
  210. index: 0,
  211. },
  212. },
  213. );
  214. expect(result.moveCollection.parent!.id).toBe(electronicsCollection.id);
  215. const positions = await getChildrenOf(electronicsCollection.id);
  216. expect(positions.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  217. });
  218. it('re-evaluates Collection contents on move', async () => {
  219. const result = await client.query<GetCollectionProducts.Query, GetCollectionProducts.Variables>(
  220. GET_COLLECTION_PRODUCT_VARIANTS,
  221. { id: pearCollection.id },
  222. );
  223. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  224. 'Laptop 13 inch 8GB',
  225. 'Laptop 15 inch 8GB',
  226. 'Laptop 13 inch 16GB',
  227. 'Laptop 15 inch 16GB',
  228. 'Instant Camera',
  229. ]);
  230. });
  231. it('alters the position in the current parent 1', async () => {
  232. await client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  233. input: {
  234. collectionId: computersCollection.id,
  235. parentId: electronicsCollection.id,
  236. index: 0,
  237. },
  238. });
  239. const afterResult = await getChildrenOf(electronicsCollection.id);
  240. expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
  241. });
  242. it('alters the position in the current parent 2', async () => {
  243. await client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  244. input: {
  245. collectionId: pearCollection.id,
  246. parentId: electronicsCollection.id,
  247. index: 0,
  248. },
  249. });
  250. const afterResult = await getChildrenOf(electronicsCollection.id);
  251. expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  252. });
  253. it('corrects an out-of-bounds negative index value', async () => {
  254. await client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  255. input: {
  256. collectionId: pearCollection.id,
  257. parentId: electronicsCollection.id,
  258. index: -3,
  259. },
  260. });
  261. const afterResult = await getChildrenOf(electronicsCollection.id);
  262. expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  263. });
  264. it('corrects an out-of-bounds positive index value', async () => {
  265. await client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  266. input: {
  267. collectionId: pearCollection.id,
  268. parentId: electronicsCollection.id,
  269. index: 10,
  270. },
  271. });
  272. const afterResult = await getChildrenOf(electronicsCollection.id);
  273. expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
  274. });
  275. it(
  276. 'throws if attempting to move into self',
  277. assertThrowsWithMessage(
  278. () =>
  279. client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  280. input: {
  281. collectionId: pearCollection.id,
  282. parentId: pearCollection.id,
  283. index: 0,
  284. },
  285. }),
  286. `Cannot move a Collection into itself`,
  287. ),
  288. );
  289. it(
  290. 'throws if attempting to move into a decendant of self',
  291. assertThrowsWithMessage(
  292. () =>
  293. client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  294. input: {
  295. collectionId: pearCollection.id,
  296. parentId: pearCollection.id,
  297. index: 0,
  298. },
  299. }),
  300. `Cannot move a Collection into itself`,
  301. ),
  302. );
  303. async function getChildrenOf(parentId: string): Promise<Array<{ name: string; id: string }>> {
  304. const result = await client.query<GetCollections.Query>(GET_COLLECTIONS);
  305. return result.collections.items.filter(i => i.parent!.id === parentId);
  306. }
  307. });
  308. describe('filters', () => {
  309. it('Collection with no filters has no productVariants', async () => {
  310. const result = await client.query<
  311. CreateCollectionSelectVariants.Mutation,
  312. CreateCollectionSelectVariants.Variables
  313. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  314. input: {
  315. translations: [{ languageCode: LanguageCode.en, name: 'Empty', description: '' }],
  316. filters: [],
  317. } as CreateCollectionInput,
  318. });
  319. expect(result.createCollection.productVariants.totalItems).toBe(0);
  320. });
  321. describe('facetValue filter', () => {
  322. it('electronics', async () => {
  323. const result = await client.query<
  324. GetCollectionProducts.Query,
  325. GetCollectionProducts.Variables
  326. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  327. id: electronicsCollection.id,
  328. });
  329. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  330. 'Laptop 13 inch 8GB',
  331. 'Laptop 15 inch 8GB',
  332. 'Laptop 13 inch 16GB',
  333. 'Laptop 15 inch 16GB',
  334. 'Curvy Monitor 24 inch',
  335. 'Curvy Monitor 27 inch',
  336. 'Gaming PC i7-8700 240GB SSD',
  337. 'Gaming PC R7-2700 240GB SSD',
  338. 'Gaming PC i7-8700 120GB SSD',
  339. 'Gaming PC R7-2700 120GB SSD',
  340. 'Hard Drive 1TB',
  341. 'Hard Drive 2TB',
  342. 'Hard Drive 3TB',
  343. 'Hard Drive 4TB',
  344. 'Hard Drive 6TB',
  345. 'Clacky Keyboard',
  346. 'USB Cable',
  347. 'Instant Camera',
  348. 'Camera Lens',
  349. 'Tripod',
  350. 'SLR Camera',
  351. ]);
  352. });
  353. it('computers', async () => {
  354. const result = await client.query<
  355. GetCollectionProducts.Query,
  356. GetCollectionProducts.Variables
  357. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  358. id: computersCollection.id,
  359. });
  360. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  361. 'Laptop 13 inch 8GB',
  362. 'Laptop 15 inch 8GB',
  363. 'Laptop 13 inch 16GB',
  364. 'Laptop 15 inch 16GB',
  365. 'Curvy Monitor 24 inch',
  366. 'Curvy Monitor 27 inch',
  367. 'Gaming PC i7-8700 240GB SSD',
  368. 'Gaming PC R7-2700 240GB SSD',
  369. 'Gaming PC i7-8700 120GB SSD',
  370. 'Gaming PC R7-2700 120GB SSD',
  371. 'Hard Drive 1TB',
  372. 'Hard Drive 2TB',
  373. 'Hard Drive 3TB',
  374. 'Hard Drive 4TB',
  375. 'Hard Drive 6TB',
  376. 'Clacky Keyboard',
  377. 'USB Cable',
  378. ]);
  379. });
  380. it('photo and pear', async () => {
  381. const result = await client.query<
  382. CreateCollectionSelectVariants.Mutation,
  383. CreateCollectionSelectVariants.Variables
  384. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  385. input: {
  386. translations: [
  387. { languageCode: LanguageCode.en, name: 'Photo Pear', description: '' },
  388. ],
  389. filters: [
  390. {
  391. code: facetValueCollectionFilter.code,
  392. arguments: [
  393. {
  394. name: 'facetValueIds',
  395. value: `["${getFacetValueId('pear')}", "${getFacetValueId(
  396. 'photo',
  397. )}"]`,
  398. type: ConfigArgType.FACET_VALUE_IDS,
  399. },
  400. ],
  401. },
  402. ],
  403. } as CreateCollectionInput,
  404. });
  405. expect(result.createCollection.productVariants.items.map(i => i.name)).toEqual([
  406. 'Instant Camera',
  407. ]);
  408. });
  409. });
  410. describe('variantName filter', () => {
  411. async function createVariantNameFilteredCollection(
  412. operator: StringOperator,
  413. term: string,
  414. ): Promise<Collection.Fragment> {
  415. const { createCollection } = await client.query<
  416. CreateCollection.Mutation,
  417. CreateCollection.Variables
  418. >(CREATE_COLLECTION, {
  419. input: {
  420. translations: [
  421. { languageCode: LanguageCode.en, name: `${operator} ${term}`, description: '' },
  422. ],
  423. filters: [
  424. {
  425. code: variantNameCollectionFilter.code,
  426. arguments: [
  427. {
  428. name: 'operator',
  429. value: operator,
  430. type: ConfigArgType.STRING_OPERATOR,
  431. },
  432. {
  433. name: 'term',
  434. value: term,
  435. type: ConfigArgType.STRING,
  436. },
  437. ],
  438. },
  439. ],
  440. },
  441. });
  442. return createCollection;
  443. }
  444. it('contains operator', async () => {
  445. const collection = await createVariantNameFilteredCollection('contains', 'camera');
  446. const result = await client.query<
  447. GetCollectionProducts.Query,
  448. GetCollectionProducts.Variables
  449. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  450. id: collection.id,
  451. });
  452. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  453. 'Instant Camera',
  454. 'Camera Lens',
  455. 'SLR Camera',
  456. ]);
  457. });
  458. it('startsWith operator', async () => {
  459. const collection = await createVariantNameFilteredCollection('startsWith', 'camera');
  460. const result = await client.query<
  461. GetCollectionProducts.Query,
  462. GetCollectionProducts.Variables
  463. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  464. id: collection.id,
  465. });
  466. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual(['Camera Lens']);
  467. });
  468. it('endsWith operator', async () => {
  469. const collection = await createVariantNameFilteredCollection('endsWith', 'camera');
  470. const result = await client.query<
  471. GetCollectionProducts.Query,
  472. GetCollectionProducts.Variables
  473. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  474. id: collection.id,
  475. });
  476. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  477. 'Instant Camera',
  478. 'SLR Camera',
  479. ]);
  480. });
  481. it('doesNotContain operator', async () => {
  482. const collection = await createVariantNameFilteredCollection('doesNotContain', 'camera');
  483. const result = await client.query<
  484. GetCollectionProducts.Query,
  485. GetCollectionProducts.Variables
  486. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  487. id: collection.id,
  488. });
  489. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  490. 'Laptop 13 inch 8GB',
  491. 'Laptop 15 inch 8GB',
  492. 'Laptop 13 inch 16GB',
  493. 'Laptop 15 inch 16GB',
  494. 'Curvy Monitor 24 inch',
  495. 'Curvy Monitor 27 inch',
  496. 'Gaming PC i7-8700 240GB SSD',
  497. 'Gaming PC R7-2700 240GB SSD',
  498. 'Gaming PC i7-8700 120GB SSD',
  499. 'Gaming PC R7-2700 120GB SSD',
  500. 'Hard Drive 1TB',
  501. 'Hard Drive 2TB',
  502. 'Hard Drive 3TB',
  503. 'Hard Drive 4TB',
  504. 'Hard Drive 6TB',
  505. 'Clacky Keyboard',
  506. 'USB Cable',
  507. 'Tripod',
  508. ]);
  509. });
  510. });
  511. describe('re-evaluation of contents on changes', () => {
  512. let products: GetProductsWithVariantIds.Items[];
  513. beforeAll(async () => {
  514. const result = await client.query<GetProductsWithVariantIds.Query>(gql`
  515. query GetProductsWithVariantIds {
  516. products {
  517. items {
  518. id
  519. name
  520. variants {
  521. id
  522. }
  523. }
  524. }
  525. }
  526. `);
  527. products = result.products.items;
  528. });
  529. it('updates contents when Product is updated', async () => {
  530. await client.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  531. input: {
  532. id: products[1].id,
  533. facetValueIds: [
  534. getFacetValueId('electronics'),
  535. getFacetValueId('computers'),
  536. getFacetValueId('pear'),
  537. ],
  538. },
  539. });
  540. const result = await client.query<
  541. GetCollectionProducts.Query,
  542. GetCollectionProducts.Variables
  543. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  544. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  545. 'Laptop 13 inch 8GB',
  546. 'Laptop 15 inch 8GB',
  547. 'Laptop 13 inch 16GB',
  548. 'Laptop 15 inch 16GB',
  549. 'Curvy Monitor 24 inch',
  550. 'Curvy Monitor 27 inch',
  551. 'Instant Camera',
  552. ]);
  553. });
  554. it('updates contents when ProductVariant is updated', async () => {
  555. const gamingPcFirstVariant = products.find(p => p.name === 'Gaming PC')!.variants[0];
  556. await client.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  557. UPDATE_PRODUCT_VARIANTS,
  558. {
  559. input: [
  560. {
  561. id: gamingPcFirstVariant.id,
  562. facetValueIds: [getFacetValueId('pear')],
  563. },
  564. ],
  565. },
  566. );
  567. const result = await client.query<
  568. GetCollectionProducts.Query,
  569. GetCollectionProducts.Variables
  570. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  571. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  572. 'Laptop 13 inch 8GB',
  573. 'Laptop 15 inch 8GB',
  574. 'Laptop 13 inch 16GB',
  575. 'Laptop 15 inch 16GB',
  576. 'Curvy Monitor 24 inch',
  577. 'Curvy Monitor 27 inch',
  578. 'Gaming PC i7-8700 240GB SSD',
  579. 'Instant Camera',
  580. ]);
  581. });
  582. it('correctly filters when ProductVariant and Product both have matching FacetValue', async () => {
  583. const gamingPcFirstVariant = products.find(p => p.name === 'Gaming PC')!.variants[0];
  584. await client.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  585. UPDATE_PRODUCT_VARIANTS,
  586. {
  587. input: [
  588. {
  589. id: gamingPcFirstVariant.id,
  590. facetValueIds: [getFacetValueId('electronics'), getFacetValueId('pear')],
  591. },
  592. ],
  593. },
  594. );
  595. const result = await client.query<
  596. GetCollectionProducts.Query,
  597. GetCollectionProducts.Variables
  598. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  599. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  600. 'Laptop 13 inch 8GB',
  601. 'Laptop 15 inch 8GB',
  602. 'Laptop 13 inch 16GB',
  603. 'Laptop 15 inch 16GB',
  604. 'Curvy Monitor 24 inch',
  605. 'Curvy Monitor 27 inch',
  606. 'Gaming PC i7-8700 240GB SSD',
  607. 'Instant Camera',
  608. ]);
  609. });
  610. });
  611. });
  612. describe('Product collections property', () => {
  613. it('returns all collections to which the Product belongs', async () => {
  614. const result = await client.query<
  615. GetCollectionsForProducts.Query,
  616. GetCollectionsForProducts.Variables
  617. >(GET_COLLECTIONS_FOR_PRODUCTS, { term: 'camera' });
  618. expect(result.products.items[0].collections).toEqual([
  619. { id: 'T_3', name: 'Electronics' },
  620. { id: 'T_5', name: 'Pear' },
  621. { id: 'T_7', name: 'Photo Pear' },
  622. { id: 'T_8', name: 'contains camera' },
  623. { id: 'T_10', name: 'endsWith camera' },
  624. ]);
  625. });
  626. });
  627. function getFacetValueId(code: string): string {
  628. const match = facetValues.find(fv => fv.code === code);
  629. if (!match) {
  630. throw new Error(`Could not find a FacetValue with the code "${code}"`);
  631. }
  632. return match.id;
  633. }
  634. });
  635. export const GET_COLLECTION = gql`
  636. query GetCollection($id: ID!, $languageCode: LanguageCode) {
  637. collection(id: $id, languageCode: $languageCode) {
  638. ...Collection
  639. }
  640. }
  641. ${COLLECTION_FRAGMENT}
  642. `;
  643. export const MOVE_COLLECTION = gql`
  644. mutation MoveCollection($input: MoveCollectionInput!) {
  645. moveCollection(input: $input) {
  646. ...Collection
  647. }
  648. }
  649. ${COLLECTION_FRAGMENT}
  650. `;
  651. const GET_FACET_VALUES = gql`
  652. query GetFacetValues {
  653. facets {
  654. items {
  655. values {
  656. ...FacetValue
  657. }
  658. }
  659. }
  660. }
  661. ${FACET_VALUE_FRAGMENT}
  662. `;
  663. const GET_COLLECTIONS = gql`
  664. query GetCollections {
  665. collections(languageCode: en) {
  666. items {
  667. id
  668. name
  669. position
  670. parent {
  671. id
  672. name
  673. }
  674. }
  675. }
  676. }
  677. `;
  678. const GET_COLLECTION_PRODUCT_VARIANTS = gql`
  679. query GetCollectionProducts($id: ID!) {
  680. collection(id: $id) {
  681. productVariants {
  682. items {
  683. id
  684. name
  685. facetValues {
  686. code
  687. }
  688. }
  689. }
  690. }
  691. }
  692. `;
  693. const CREATE_COLLECTION_SELECT_VARIANTS = gql`
  694. mutation CreateCollectionSelectVariants($input: CreateCollectionInput!) {
  695. createCollection(input: $input) {
  696. productVariants {
  697. items {
  698. name
  699. }
  700. totalItems
  701. }
  702. }
  703. }
  704. `;
  705. const GET_COLLECTION_BREADCRUMBS = gql`
  706. query GetCollectionBreadcrumbs($id: ID!) {
  707. collection(id: $id) {
  708. breadcrumbs {
  709. id
  710. name
  711. }
  712. }
  713. }
  714. `;
  715. const GET_COLLECTIONS_FOR_PRODUCTS = gql`
  716. query GetCollectionsForProducts($term: String!) {
  717. products(options: { filter: { name: { contains: $term } } }) {
  718. items {
  719. id
  720. name
  721. collections {
  722. id
  723. name
  724. }
  725. }
  726. }
  727. }
  728. `;