collection.e2e-spec.ts 35 KB

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