collection.e2e-spec.ts 57 KB

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