collection.e2e-spec.ts 59 KB

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