1
0

collection.e2e-spec.ts 32 KB

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