default-search-plugin.e2e-spec.ts 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import { CurrencyCode, LanguageCode, LogicalOperator, SortOrder } from '@vendure/common/lib/generated-types';
  3. import { pick } from '@vendure/common/lib/pick';
  4. import {
  5. DefaultJobQueuePlugin,
  6. DefaultSearchPlugin,
  7. facetValueCollectionFilter,
  8. mergeConfig,
  9. } from '@vendure/core';
  10. import {
  11. createErrorResultGuard,
  12. createTestEnvironment,
  13. E2E_DEFAULT_CHANNEL_TOKEN,
  14. ErrorResultGuard,
  15. SimpleGraphQLClient,
  16. } from '@vendure/testing';
  17. import path from 'node:path';
  18. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  19. import { initialData } from '../../../e2e-common/e2e-initial-data';
  20. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  21. import {
  22. reindexDocument,
  23. searchGetAssetsDocument,
  24. searchGetPricesDocument,
  25. searchProductsAdminDocument,
  26. } from './graphql/admin-definitions';
  27. import { channelFragment } from './graphql/fragments-admin';
  28. import { FragmentOf, ResultOf, VariablesOf } from './graphql/graphql-admin';
  29. import {
  30. assignProductToChannelDocument,
  31. assignProductVariantToChannelDocument,
  32. createChannelDocument,
  33. createCollectionDocument,
  34. createFacetDocument,
  35. createProductDocument,
  36. createProductVariantsDocument,
  37. deleteAssetDocument,
  38. deleteProductDocument,
  39. deleteProductVariantDocument,
  40. removeProductFromChannelDocument,
  41. removeProductVariantFromChannelDocument,
  42. updateAssetDocument,
  43. updateChannelDocument,
  44. updateCollectionDocument,
  45. updateProductDocument,
  46. updateProductVariantsDocument,
  47. updateTaxRateDocument,
  48. } from './graphql/shared-definitions';
  49. import {
  50. searchCollectionsDocument,
  51. searchFacetValuesDocument,
  52. searchGetPricesShopDocument,
  53. searchProductsShopDocument,
  54. } from './graphql/shop-definitions';
  55. import { awaitRunningJobs } from './utils/await-running-jobs';
  56. type SearchInput = VariablesOf<typeof searchProductsAdminDocument>['input'];
  57. type SearchProductsShopInput = VariablesOf<typeof searchProductsShopDocument>['input'];
  58. type SearchProductsShopQueryVariablesExt = {
  59. input: SearchProductsShopInput & {
  60. // This input field is dynamically added only when the `indexStockStatus` init option
  61. // is set to `true`, and therefore not included in the generated type. Therefore
  62. // we need to manually patch it here.
  63. inStock?: boolean;
  64. };
  65. };
  66. describe('Default search plugin', () => {
  67. const { server, adminClient, shopClient } = createTestEnvironment(
  68. mergeConfig(testConfig(), {
  69. plugins: [DefaultSearchPlugin.init({ indexStockStatus: true }), DefaultJobQueuePlugin],
  70. }),
  71. );
  72. beforeAll(async () => {
  73. await server.init({
  74. initialData,
  75. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-default-search.csv'),
  76. customerCount: 1,
  77. });
  78. await adminClient.asSuperAdmin();
  79. // We have extra time here because a lot of jobs are
  80. // triggered from all the product updates
  81. await awaitRunningJobs(adminClient, 10_000, 1000);
  82. }, TEST_SETUP_TIMEOUT_MS);
  83. afterAll(async () => {
  84. await awaitRunningJobs(adminClient);
  85. await server.destroy();
  86. });
  87. function testProductsShop(input: SearchProductsShopQueryVariablesExt['input']) {
  88. return shopClient.query(searchProductsShopDocument, { input });
  89. }
  90. function testProductsAdmin(input: SearchInput) {
  91. return adminClient.query(searchProductsAdminDocument, { input });
  92. }
  93. type TestProducts = (
  94. input: SearchInput,
  95. ) => Promise<ResultOf<typeof searchProductsShopDocument> | ResultOf<typeof searchProductsAdminDocument>>;
  96. async function testGroupByProduct(testProducts: TestProducts) {
  97. const result = await testProducts({
  98. groupByProduct: true,
  99. });
  100. expect(result.search.totalItems).toBe(20);
  101. }
  102. async function testNoGrouping(testProducts: TestProducts) {
  103. const result = await testProducts({
  104. groupByProduct: false,
  105. });
  106. expect(result.search.totalItems).toBe(34);
  107. }
  108. async function testSortingWithGrouping(
  109. testProducts: TestProducts,
  110. sortBy: keyof NonNullable<SearchInput['sort']>,
  111. ) {
  112. const result = await testProducts({
  113. groupByProduct: true,
  114. sort: {
  115. [sortBy]: SortOrder.ASC,
  116. },
  117. take: 3,
  118. });
  119. const expected =
  120. sortBy === 'name'
  121. ? ['Bonsai Tree', 'Boxing Gloves', 'Camera Lens']
  122. : ['Skipping Rope', 'Tripod', 'Spiky Cactus'];
  123. expect(result.search.items.map(i => i.productName)).toEqual(expected);
  124. }
  125. async function testSortingNoGrouping(
  126. testProducts: TestProducts,
  127. sortBy: keyof NonNullable<SearchInput['sort']>,
  128. ) {
  129. const result = await testProducts({
  130. groupByProduct: false,
  131. sort: {
  132. [sortBy]: SortOrder.DESC,
  133. },
  134. take: 3,
  135. });
  136. const expected =
  137. sortBy === 'name'
  138. ? ['USB Cable', 'Tripod', 'Tent']
  139. : ['Road Bike', 'Laptop 15 inch 16GB', 'Laptop 13 inch 16GB'];
  140. expect(result.search.items.map(i => i.productVariantName)).toEqual(expected);
  141. }
  142. async function testMatchSearchTerm(testProducts: TestProducts) {
  143. const result = await testProducts({
  144. term: 'camera',
  145. groupByProduct: true,
  146. sort: {
  147. name: SortOrder.ASC,
  148. },
  149. });
  150. expect(result.search.items.map(i => i.productName)).toEqual([
  151. 'Camera Lens',
  152. 'Instant Camera',
  153. 'Slr Camera',
  154. ]);
  155. }
  156. async function testMatchPartialSearchTerm(testProducts: TestProducts) {
  157. const result = await testProducts({
  158. term: 'lap',
  159. groupByProduct: true,
  160. sort: {
  161. name: SortOrder.ASC,
  162. },
  163. });
  164. expect(result.search.items.map(i => i.productName)).toEqual(['Laptop']);
  165. }
  166. async function testMatchFacetIdsAnd(testProducts: TestProducts) {
  167. const result = await testProducts({
  168. facetValueIds: ['T_1', 'T_2'],
  169. facetValueOperator: LogicalOperator.AND,
  170. groupByProduct: true,
  171. });
  172. expect(result.search.items.map(i => i.productName)).toEqual([
  173. 'Laptop',
  174. 'Curvy Monitor',
  175. 'Gaming PC',
  176. 'Hard Drive',
  177. 'Clacky Keyboard',
  178. 'USB Cable',
  179. ]);
  180. }
  181. async function testMatchFacetIdsOr(testProducts: TestProducts) {
  182. const result = await testProducts({
  183. facetValueIds: ['T_1', 'T_5'],
  184. facetValueOperator: LogicalOperator.OR,
  185. groupByProduct: true,
  186. });
  187. expect(result.search.items.map(i => i.productName)).toEqual([
  188. 'Laptop',
  189. 'Curvy Monitor',
  190. 'Gaming PC',
  191. 'Hard Drive',
  192. 'Clacky Keyboard',
  193. 'USB Cable',
  194. 'Instant Camera',
  195. 'Camera Lens',
  196. 'Tripod',
  197. 'Slr Camera',
  198. 'Spiky Cactus',
  199. 'Orchid',
  200. 'Bonsai Tree',
  201. ]);
  202. }
  203. async function testMatchFacetValueFiltersAnd(testProducts: TestProducts) {
  204. const result = await testProducts({
  205. groupByProduct: true,
  206. facetValueFilters: [{ and: 'T_1' }, { and: 'T_2' }],
  207. });
  208. expect(result.search.items.map(i => i.productName)).toEqual([
  209. 'Laptop',
  210. 'Curvy Monitor',
  211. 'Gaming PC',
  212. 'Hard Drive',
  213. 'Clacky Keyboard',
  214. 'USB Cable',
  215. ]);
  216. }
  217. async function testMatchFacetValueFiltersOr(testProducts: TestProducts) {
  218. const result = await testProducts({
  219. groupByProduct: true,
  220. facetValueFilters: [{ or: ['T_1', 'T_5'] }],
  221. });
  222. expect(result.search.items.map(i => i.productName)).toEqual([
  223. 'Laptop',
  224. 'Curvy Monitor',
  225. 'Gaming PC',
  226. 'Hard Drive',
  227. 'Clacky Keyboard',
  228. 'USB Cable',
  229. 'Instant Camera',
  230. 'Camera Lens',
  231. 'Tripod',
  232. 'Slr Camera',
  233. 'Spiky Cactus',
  234. 'Orchid',
  235. 'Bonsai Tree',
  236. ]);
  237. }
  238. async function testMatchFacetValueFiltersOrWithAnd(testProducts: TestProducts) {
  239. const result = await testProducts({
  240. groupByProduct: true,
  241. facetValueFilters: [{ and: 'T_1' }, { or: ['T_2', 'T_3'] }],
  242. });
  243. expect(result.search.items.map(i => i.productName)).toEqual([
  244. 'Laptop',
  245. 'Curvy Monitor',
  246. 'Gaming PC',
  247. 'Hard Drive',
  248. 'Clacky Keyboard',
  249. 'USB Cable',
  250. 'Instant Camera',
  251. 'Camera Lens',
  252. 'Tripod',
  253. 'Slr Camera',
  254. ]);
  255. }
  256. async function testMatchFacetValueFiltersWithFacetIdsOr(testProducts: TestProducts) {
  257. const result = await testProducts({
  258. facetValueIds: ['T_2', 'T_3'],
  259. facetValueOperator: LogicalOperator.OR,
  260. facetValueFilters: [{ and: 'T_1' }],
  261. groupByProduct: true,
  262. });
  263. expect(result.search.items.map(i => i.productName)).toEqual([
  264. 'Laptop',
  265. 'Curvy Monitor',
  266. 'Gaming PC',
  267. 'Hard Drive',
  268. 'Clacky Keyboard',
  269. 'USB Cable',
  270. 'Instant Camera',
  271. 'Camera Lens',
  272. 'Tripod',
  273. 'Slr Camera',
  274. ]);
  275. }
  276. async function testMatchFacetValueFiltersWithFacetIdsAnd(testProducts: TestProducts) {
  277. const result = await testProducts({
  278. facetValueIds: ['T_1'],
  279. facetValueFilters: [{ and: 'T_3' }],
  280. facetValueOperator: LogicalOperator.AND,
  281. groupByProduct: true,
  282. });
  283. expect(result.search.items.map(i => i.productName)).toEqual([
  284. 'Instant Camera',
  285. 'Camera Lens',
  286. 'Tripod',
  287. 'Slr Camera',
  288. ]);
  289. }
  290. async function testMatchCollectionId(testProducts: TestProducts) {
  291. const result = await testProducts({
  292. collectionId: 'T_2',
  293. groupByProduct: true,
  294. });
  295. expect(result.search.items.map(i => i.productName)).toEqual([
  296. 'Spiky Cactus',
  297. 'Orchid',
  298. 'Bonsai Tree',
  299. ]);
  300. }
  301. async function testMatchCollectionSlug(testProducts: TestProducts) {
  302. const result = await testProducts({
  303. collectionSlug: 'plants',
  304. groupByProduct: true,
  305. });
  306. expect(result.search.items.map(i => i.productName)).toEqual([
  307. 'Spiky Cactus',
  308. 'Orchid',
  309. 'Bonsai Tree',
  310. ]);
  311. }
  312. async function testMatchCollectionIds(testProducts: TestProducts) {
  313. const result = await testProducts({
  314. collectionIds: ['T_2', 'T_3'],
  315. groupByProduct: true,
  316. });
  317. // Should return products from both Plants (T_2) and Electronics (T_3) collections
  318. // Plants has 3 products in the default test data
  319. expect(result.search.items.length).toBeGreaterThanOrEqual(3);
  320. expect(result.search.totalItems).toBeGreaterThanOrEqual(3);
  321. const productNames = result.search.items.map(i => i.productName);
  322. // Verify that products from Plants collection are included
  323. expect(productNames).toContain('Bonsai Tree');
  324. expect(productNames).toContain('Orchid');
  325. expect(productNames).toContain('Spiky Cactus');
  326. }
  327. async function testMatchCollectionSlugs(testProducts: TestProducts) {
  328. const result = await testProducts({
  329. collectionSlugs: ['plants', 'electronics'],
  330. groupByProduct: true,
  331. });
  332. // Should return products from both Plants and Electronics collections
  333. // Plants has 3 products in the default test data
  334. expect(result.search.items.length).toBeGreaterThanOrEqual(3);
  335. expect(result.search.totalItems).toBeGreaterThanOrEqual(3);
  336. const productNames = result.search.items.map(i => i.productName);
  337. // Verify that products from Plants collection are included
  338. expect(productNames).toContain('Bonsai Tree');
  339. expect(productNames).toContain('Orchid');
  340. expect(productNames).toContain('Spiky Cactus');
  341. }
  342. async function testCollectionIdsEdgeCases(testProducts: TestProducts) {
  343. // Test with duplicate IDs - should handle gracefully
  344. const resultWithDuplicates = await testProducts({
  345. collectionIds: ['T_2', 'T_2', 'T_2'],
  346. groupByProduct: true,
  347. });
  348. // Should still return Plants collection products, de-duplicated
  349. expect(resultWithDuplicates.search.items.map(i => i.productName).sort()).toEqual([
  350. 'Bonsai Tree',
  351. 'Orchid',
  352. 'Spiky Cactus',
  353. ]);
  354. // Test with non-existent collection ID - should return no results
  355. const resultNonExistent = await testProducts({
  356. collectionIds: ['T_999'],
  357. groupByProduct: true,
  358. });
  359. expect(resultNonExistent.search.items).toEqual([]);
  360. expect(resultNonExistent.search.totalItems).toBe(0);
  361. }
  362. async function testCollectionSlugsEdgeCases(testProducts: TestProducts) {
  363. // Test with duplicate slugs - should handle gracefully
  364. const resultWithDuplicates = await testProducts({
  365. collectionSlugs: ['plants', 'plants', 'plants'],
  366. groupByProduct: true,
  367. });
  368. // Should still return Plants collection products, de-duplicated
  369. expect(resultWithDuplicates.search.items.map(i => i.productName).sort()).toEqual([
  370. 'Bonsai Tree',
  371. 'Orchid',
  372. 'Spiky Cactus',
  373. ]);
  374. // Test with non-existent collection slug - should return no results
  375. const resultNonExistent = await testProducts({
  376. collectionSlugs: ['non-existent'],
  377. groupByProduct: true,
  378. });
  379. expect(resultNonExistent.search.items).toEqual([]);
  380. expect(resultNonExistent.search.totalItems).toBe(0);
  381. }
  382. async function testSinglePrices(
  383. client: SimpleGraphQLClient,
  384. document:
  385. | typeof searchGetPricesDocument
  386. | typeof searchGetPricesShopDocument = searchGetPricesDocument,
  387. ) {
  388. const result = await client.query(document, {
  389. input: {
  390. groupByProduct: false,
  391. take: 3,
  392. },
  393. });
  394. expect(result.search.items).toEqual([
  395. {
  396. price: { value: 129900 },
  397. priceWithTax: { value: 155880 },
  398. },
  399. {
  400. price: { value: 139900 },
  401. priceWithTax: { value: 167880 },
  402. },
  403. {
  404. price: { value: 219900 },
  405. priceWithTax: { value: 263880 },
  406. },
  407. ]);
  408. }
  409. async function testPriceRanges(
  410. client: SimpleGraphQLClient,
  411. document:
  412. | typeof searchGetPricesDocument
  413. | typeof searchGetPricesShopDocument = searchGetPricesDocument,
  414. ) {
  415. const result = await client.query(document, {
  416. input: {
  417. groupByProduct: true,
  418. take: 3,
  419. },
  420. });
  421. expect(result.search.items).toEqual([
  422. {
  423. price: { min: 129900, max: 229900 },
  424. priceWithTax: { min: 155880, max: 275880 },
  425. },
  426. {
  427. price: { min: 14374, max: 16994 },
  428. priceWithTax: { min: 17249, max: 20393 },
  429. },
  430. {
  431. price: { min: 93120, max: 109995 },
  432. priceWithTax: { min: 111744, max: 131994 },
  433. },
  434. ]);
  435. }
  436. describe('shop api', () => {
  437. it('group by product', () => testGroupByProduct(testProductsShop));
  438. it('no grouping', () => testNoGrouping(testProductsShop));
  439. it('matches search term', () => testMatchSearchTerm(testProductsShop));
  440. it('matches partial search term', () => testMatchPartialSearchTerm(testProductsShop));
  441. it('matches by facetId with AND operator', () => testMatchFacetIdsAnd(testProductsShop));
  442. it('matches by facetId with OR operator', () => testMatchFacetIdsOr(testProductsShop));
  443. it('matches by FacetValueFilters AND', () => testMatchFacetValueFiltersAnd(testProductsShop));
  444. it('matches by FacetValueFilters OR', () => testMatchFacetValueFiltersOr(testProductsShop));
  445. it('matches by FacetValueFilters OR and AND', () =>
  446. testMatchFacetValueFiltersOrWithAnd(testProductsShop));
  447. it('matches by FacetValueFilters with facetId OR operator', () =>
  448. testMatchFacetValueFiltersWithFacetIdsOr(testProductsShop));
  449. it('matches by FacetValueFilters with facetId AND operator', () =>
  450. testMatchFacetValueFiltersWithFacetIdsAnd(testProductsShop));
  451. it('matches by collectionId', () => testMatchCollectionId(testProductsShop));
  452. it('matches by collectionSlug', () => testMatchCollectionSlug(testProductsShop));
  453. it('matches by multiple collectionIds', () => testMatchCollectionIds(testProductsShop));
  454. it('matches by multiple collectionSlugs', () => testMatchCollectionSlugs(testProductsShop));
  455. it('handles collectionIds edge cases', () => testCollectionIdsEdgeCases(testProductsShop));
  456. it('handles collectionSlugs edge cases', () => testCollectionSlugsEdgeCases(testProductsShop));
  457. it('single prices', () => testSinglePrices(shopClient, searchGetPricesShopDocument));
  458. it('price ranges', () => testPriceRanges(shopClient, searchGetPricesShopDocument));
  459. it('returns correct facetValues when not grouped by product', async () => {
  460. const result = await shopClient.query(searchFacetValuesDocument, {
  461. input: {
  462. groupByProduct: false,
  463. },
  464. });
  465. expect(result.search.facetValues).toEqual([
  466. { count: 21, facetValue: { id: 'T_1', name: 'electronics' } },
  467. { count: 17, facetValue: { id: 'T_2', name: 'computers' } },
  468. { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
  469. { count: 10, facetValue: { id: 'T_4', name: 'sports equipment' } },
  470. { count: 3, facetValue: { id: 'T_5', name: 'home & garden' } },
  471. { count: 3, facetValue: { id: 'T_6', name: 'plants' } },
  472. ]);
  473. });
  474. it('returns correct facetValues when grouped by product', async () => {
  475. const result = await shopClient.query(searchFacetValuesDocument, {
  476. input: {
  477. groupByProduct: true,
  478. },
  479. });
  480. expect(result.search.facetValues).toEqual([
  481. { count: 10, facetValue: { id: 'T_1', name: 'electronics' } },
  482. { count: 6, facetValue: { id: 'T_2', name: 'computers' } },
  483. { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
  484. { count: 7, facetValue: { id: 'T_4', name: 'sports equipment' } },
  485. { count: 3, facetValue: { id: 'T_5', name: 'home & garden' } },
  486. { count: 3, facetValue: { id: 'T_6', name: 'plants' } },
  487. ]);
  488. });
  489. // https://github.com/vendure-ecommerce/vendure/issues/1236
  490. it('returns correct facetValues when not grouped by product, with search term', async () => {
  491. const result = await shopClient.query(searchFacetValuesDocument, {
  492. input: {
  493. groupByProduct: false,
  494. term: 'laptop',
  495. },
  496. });
  497. expect(result.search.facetValues).toEqual([
  498. { count: 4, facetValue: { id: 'T_1', name: 'electronics' } },
  499. { count: 4, facetValue: { id: 'T_2', name: 'computers' } },
  500. ]);
  501. });
  502. it('omits facetValues of private facets', async () => {
  503. const { createFacet } = await adminClient.query(createFacetDocument, {
  504. input: {
  505. code: 'profit-margin',
  506. isPrivate: true,
  507. translations: [{ languageCode: LanguageCode.en, name: 'Profit Margin' }],
  508. values: [
  509. {
  510. code: 'massive',
  511. translations: [{ languageCode: LanguageCode.en, name: 'massive' }],
  512. },
  513. ],
  514. },
  515. });
  516. await adminClient.query(updateProductDocument, {
  517. input: {
  518. id: 'T_2',
  519. // T_1 & T_2 are the existing facetValues (electronics & photo)
  520. facetValueIds: ['T_1', 'T_2', createFacet.values[0].id],
  521. },
  522. });
  523. await awaitRunningJobs(adminClient);
  524. const result = await shopClient.query(searchFacetValuesDocument, {
  525. input: {
  526. groupByProduct: true,
  527. },
  528. });
  529. expect(result.search.facetValues).toEqual([
  530. { count: 10, facetValue: { id: 'T_1', name: 'electronics' } },
  531. { count: 6, facetValue: { id: 'T_2', name: 'computers' } },
  532. { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
  533. { count: 7, facetValue: { id: 'T_4', name: 'sports equipment' } },
  534. { count: 3, facetValue: { id: 'T_5', name: 'home & garden' } },
  535. { count: 3, facetValue: { id: 'T_6', name: 'plants' } },
  536. ]);
  537. });
  538. it('returns correct collections when not grouped by product', async () => {
  539. const result = await shopClient.query(searchCollectionsDocument, {
  540. input: {
  541. groupByProduct: false,
  542. },
  543. });
  544. expect(result.search.collections).toEqual([
  545. { collection: { id: 'T_2', name: 'Plants' }, count: 3 },
  546. ]);
  547. });
  548. it('returns correct collections when grouped by product', async () => {
  549. const result = await shopClient.query(searchCollectionsDocument, {
  550. input: {
  551. groupByProduct: true,
  552. },
  553. });
  554. expect(result.search.collections).toEqual([
  555. { collection: { id: 'T_2', name: 'Plants' }, count: 3 },
  556. ]);
  557. });
  558. it('encodes the productId and productVariantId', async () => {
  559. const result = await shopClient.query(searchProductsShopDocument, {
  560. input: {
  561. groupByProduct: false,
  562. take: 1,
  563. },
  564. });
  565. expect(pick(result.search.items[0], ['productId', 'productVariantId'])).toEqual({
  566. productId: 'T_1',
  567. productVariantId: 'T_1',
  568. });
  569. });
  570. it('sort name with grouping', () => testSortingWithGrouping(testProductsShop, 'name'));
  571. it('sort price with grouping', () => testSortingWithGrouping(testProductsShop, 'price'));
  572. it('sort name without grouping', () => testSortingNoGrouping(testProductsShop, 'name'));
  573. it('sort price without grouping', () => testSortingNoGrouping(testProductsShop, 'price'));
  574. it('omits results for disabled ProductVariants', async () => {
  575. await adminClient.query(updateProductVariantsDocument, {
  576. input: [{ id: 'T_3', enabled: false }],
  577. });
  578. await awaitRunningJobs(adminClient);
  579. const result = await shopClient.query(searchProductsShopDocument, {
  580. input: {
  581. groupByProduct: false,
  582. take: 3,
  583. },
  584. });
  585. expect(result.search.items.map(i => i.productVariantId)).toEqual(['T_1', 'T_2', 'T_4']);
  586. });
  587. it('encodes collectionIds', async () => {
  588. const result = await shopClient.query(searchProductsShopDocument, {
  589. input: {
  590. groupByProduct: false,
  591. term: 'cactus',
  592. take: 1,
  593. },
  594. });
  595. expect(result.search.items[0].collectionIds).toEqual(['T_2']);
  596. });
  597. it('inStock is false and not grouped by product', async () => {
  598. const result = await shopClient.query(searchProductsShopDocument, {
  599. input: {
  600. groupByProduct: false,
  601. inStock: false,
  602. },
  603. });
  604. expect(result.search.totalItems).toBe(2);
  605. });
  606. it('inStock is false and grouped by product', async () => {
  607. const result = await shopClient.query(searchProductsShopDocument, {
  608. input: {
  609. groupByProduct: true,
  610. inStock: false,
  611. },
  612. });
  613. expect(result.search.totalItems).toBe(1);
  614. });
  615. it('inStock is true and not grouped by product', async () => {
  616. const result = await shopClient.query(searchProductsShopDocument, {
  617. input: {
  618. groupByProduct: false,
  619. inStock: true,
  620. },
  621. });
  622. expect(result.search.totalItems).toBe(31);
  623. });
  624. it('inStock is true and grouped by product', async () => {
  625. const result = await shopClient.query(searchProductsShopDocument, {
  626. input: {
  627. groupByProduct: true,
  628. inStock: true,
  629. },
  630. });
  631. expect(result.search.totalItems).toBe(19);
  632. });
  633. it('inStock is undefined and not grouped by product', async () => {
  634. const result = await shopClient.query(searchProductsShopDocument, {
  635. input: {
  636. groupByProduct: false,
  637. inStock: undefined,
  638. },
  639. });
  640. expect(result.search.totalItems).toBe(33);
  641. });
  642. it('inStock is undefined and grouped by product', async () => {
  643. const result = await shopClient.query(searchProductsShopDocument, {
  644. input: {
  645. groupByProduct: true,
  646. inStock: undefined,
  647. },
  648. });
  649. expect(result.search.totalItems).toBe(20);
  650. });
  651. });
  652. describe('admin api', () => {
  653. it('group by product', () => testGroupByProduct(testProductsAdmin));
  654. it('no grouping', () => testNoGrouping(testProductsAdmin));
  655. it('matches search term', () => testMatchSearchTerm(testProductsAdmin));
  656. it('matches partial search term', () => testMatchPartialSearchTerm(testProductsAdmin));
  657. it('matches by facetId with AND operator', () => testMatchFacetIdsAnd(testProductsAdmin));
  658. it('matches by facetId with OR operator', () => testMatchFacetIdsOr(testProductsAdmin));
  659. it('matches by FacetValueFilters AND', () => testMatchFacetValueFiltersAnd(testProductsAdmin));
  660. it('matches by FacetValueFilters OR', () => testMatchFacetValueFiltersOr(testProductsAdmin));
  661. it('matches by FacetValueFilters OR and AND', () =>
  662. testMatchFacetValueFiltersOrWithAnd(testProductsAdmin));
  663. it('matches by FacetValueFilters with facetId OR operator', () =>
  664. testMatchFacetValueFiltersWithFacetIdsOr(testProductsAdmin));
  665. it('matches by FacetValueFilters with facetId AND operator', () =>
  666. testMatchFacetValueFiltersWithFacetIdsAnd(testProductsAdmin));
  667. it('matches by collectionId', () => testMatchCollectionId(testProductsAdmin));
  668. it('matches by collectionSlug', () => testMatchCollectionSlug(testProductsAdmin));
  669. it('matches by multiple collectionIds', () => testMatchCollectionIds(testProductsAdmin));
  670. it('matches by multiple collectionSlugs', () => testMatchCollectionSlugs(testProductsAdmin));
  671. it('handles collectionIds edge cases', () => testCollectionIdsEdgeCases(testProductsAdmin));
  672. it('handles collectionSlugs edge cases', () => testCollectionSlugsEdgeCases(testProductsAdmin));
  673. it('single prices', () => testSinglePrices(adminClient));
  674. it('price ranges', () => testPriceRanges(adminClient));
  675. it('sort name with grouping', () => testSortingWithGrouping(testProductsAdmin, 'name'));
  676. it('sort price with grouping', () => testSortingWithGrouping(testProductsAdmin, 'price'));
  677. it('sort name without grouping', () => testSortingNoGrouping(testProductsAdmin, 'name'));
  678. it('sort price without grouping', () => testSortingNoGrouping(testProductsAdmin, 'price'));
  679. describe('updating the index', () => {
  680. it('updates index when ProductVariants are changed', async () => {
  681. await awaitRunningJobs(adminClient);
  682. const { search } = await testProductsAdmin({ term: 'drive', groupByProduct: false });
  683. expect(search.items.map(i => i.sku)).toEqual([
  684. 'IHD455T1',
  685. 'IHD455T2',
  686. 'IHD455T3',
  687. 'IHD455T4',
  688. 'IHD455T6',
  689. ]);
  690. await adminClient.query(updateProductVariantsDocument, {
  691. input: search.items.map(i => ({
  692. id: i.productVariantId,
  693. sku: i.sku + '_updated',
  694. })),
  695. });
  696. await awaitRunningJobs(adminClient);
  697. const { search: search2 } = await testProductsAdmin({
  698. term: 'drive',
  699. groupByProduct: false,
  700. });
  701. expect(search2.items.map(i => i.sku)).toEqual([
  702. 'IHD455T1_updated',
  703. 'IHD455T2_updated',
  704. 'IHD455T3_updated',
  705. 'IHD455T4_updated',
  706. 'IHD455T6_updated',
  707. ]);
  708. });
  709. it('updates index when ProductVariants are deleted', async () => {
  710. await awaitRunningJobs(adminClient);
  711. const { search } = await testProductsAdmin({ term: 'drive', groupByProduct: false });
  712. const variantToDelete = search.items[0];
  713. expect(variantToDelete.sku).toBe('IHD455T1_updated');
  714. await adminClient.query(deleteProductVariantDocument, {
  715. id: variantToDelete.productVariantId,
  716. });
  717. await awaitRunningJobs(adminClient);
  718. const { search: search2 } = await testProductsAdmin({
  719. term: 'drive',
  720. groupByProduct: false,
  721. });
  722. expect(search2.items.map(i => i.sku)).toEqual([
  723. 'IHD455T2_updated',
  724. 'IHD455T3_updated',
  725. 'IHD455T4_updated',
  726. 'IHD455T6_updated',
  727. ]);
  728. });
  729. it('updates index when a Product is changed', async () => {
  730. await adminClient.query(updateProductDocument, {
  731. input: {
  732. id: 'T_1',
  733. facetValueIds: [],
  734. },
  735. });
  736. await awaitRunningJobs(adminClient);
  737. const result = await testProductsAdmin({ facetValueIds: ['T_2'], groupByProduct: true });
  738. expect(result.search.items.map(i => i.productName)).toEqual([
  739. 'Curvy Monitor',
  740. 'Gaming PC',
  741. 'Hard Drive',
  742. 'Clacky Keyboard',
  743. 'USB Cable',
  744. ]);
  745. });
  746. it('updates index when a Product is deleted', async () => {
  747. const { search } = await testProductsAdmin({ facetValueIds: ['T_2'], groupByProduct: true });
  748. expect(search.items.map(i => i.productId)).toEqual(['T_2', 'T_3', 'T_4', 'T_5', 'T_6']);
  749. await adminClient.query(deleteProductDocument, {
  750. id: 'T_5',
  751. });
  752. await awaitRunningJobs(adminClient);
  753. const { search: search2 } = await testProductsAdmin({
  754. facetValueIds: ['T_2'],
  755. groupByProduct: true,
  756. });
  757. expect(search2.items.map(i => i.productId)).toEqual(['T_2', 'T_3', 'T_4', 'T_6']);
  758. });
  759. it('updates index when a Collection is changed', async () => {
  760. await adminClient.query(updateCollectionDocument, {
  761. input: {
  762. id: 'T_2',
  763. filters: [
  764. {
  765. code: facetValueCollectionFilter.code,
  766. arguments: [
  767. {
  768. name: 'facetValueIds',
  769. value: '["T_4"]',
  770. },
  771. {
  772. name: 'containsAny',
  773. value: 'false',
  774. },
  775. ],
  776. },
  777. ],
  778. },
  779. });
  780. await awaitRunningJobs(adminClient);
  781. // add an additional check for the collection filters to update
  782. await awaitRunningJobs(adminClient);
  783. const result1 = await testProductsAdmin({ collectionId: 'T_2', groupByProduct: true });
  784. expect(result1.search.items.map(i => i.productName)).toEqual([
  785. 'Road Bike',
  786. 'Skipping Rope',
  787. 'Boxing Gloves',
  788. 'Tent',
  789. 'Cruiser Skateboard',
  790. 'Football',
  791. 'Running Shoe',
  792. ]);
  793. const result2 = await testProductsAdmin({ collectionSlug: 'plants', groupByProduct: true });
  794. expect(result2.search.items.map(i => i.productName)).toEqual([
  795. 'Road Bike',
  796. 'Skipping Rope',
  797. 'Boxing Gloves',
  798. 'Tent',
  799. 'Cruiser Skateboard',
  800. 'Football',
  801. 'Running Shoe',
  802. ]);
  803. }, 10000);
  804. it('updates index when a Collection created', async () => {
  805. const { createCollection } = await adminClient.query(createCollectionDocument, {
  806. input: {
  807. translations: [
  808. {
  809. languageCode: LanguageCode.en,
  810. name: 'Photo',
  811. description: '',
  812. slug: 'photo',
  813. },
  814. ],
  815. filters: [
  816. {
  817. code: facetValueCollectionFilter.code,
  818. arguments: [
  819. {
  820. name: 'facetValueIds',
  821. value: '["T_3"]',
  822. },
  823. {
  824. name: 'containsAny',
  825. value: 'false',
  826. },
  827. ],
  828. },
  829. ],
  830. },
  831. });
  832. await awaitRunningJobs(adminClient);
  833. // add an additional check for the collection filters to update
  834. await awaitRunningJobs(adminClient);
  835. const result = await testProductsAdmin({
  836. collectionId: createCollection.id,
  837. groupByProduct: true,
  838. });
  839. expect(result.search.items.map(i => i.productName)).toEqual([
  840. 'Instant Camera',
  841. 'Camera Lens',
  842. 'Tripod',
  843. 'Slr Camera',
  844. ]);
  845. });
  846. it('updates index when a taxRate is changed', async () => {
  847. await adminClient.query(updateTaxRateDocument, {
  848. input: {
  849. // Default Channel's defaultTaxZone is Europe (id 2) and the id of the standard TaxRate
  850. // to Europe is 2.
  851. id: 'T_2',
  852. value: 50,
  853. },
  854. });
  855. await awaitRunningJobs(adminClient);
  856. const result = await adminClient.query(searchGetPricesDocument, {
  857. input: {
  858. groupByProduct: true,
  859. term: 'laptop',
  860. } as SearchInput,
  861. });
  862. expect(result.search.items).toEqual([
  863. {
  864. price: { min: 129900, max: 229900 },
  865. priceWithTax: { min: 194850, max: 344850 },
  866. },
  867. ]);
  868. });
  869. describe('asset changes', () => {
  870. function searchForLaptop() {
  871. return adminClient.query(searchGetAssetsDocument, {
  872. input: {
  873. term: 'laptop',
  874. take: 1,
  875. },
  876. });
  877. }
  878. it('updates index when asset focalPoint is changed', async () => {
  879. const { search: search1 } = await searchForLaptop();
  880. expect(search1.items[0].productAsset!.id).toBe('T_1');
  881. expect(search1.items[0].productAsset!.focalPoint).toBeNull();
  882. await adminClient.query(updateAssetDocument, {
  883. input: {
  884. id: 'T_1',
  885. focalPoint: {
  886. x: 0.42,
  887. y: 0.42,
  888. },
  889. },
  890. });
  891. await awaitRunningJobs(adminClient);
  892. const { search: search2 } = await searchForLaptop();
  893. expect(search2.items[0].productAsset!.id).toBe('T_1');
  894. expect(search2.items[0].productAsset!.focalPoint).toEqual({ x: 0.42, y: 0.42 });
  895. });
  896. it('updates index when asset deleted', async () => {
  897. const { search: search1 } = await searchForLaptop();
  898. const assetId = search1.items[0].productAsset?.id;
  899. expect(assetId).toBeTruthy();
  900. await adminClient.query(deleteAssetDocument, {
  901. input: {
  902. assetId: assetId!,
  903. force: true,
  904. },
  905. });
  906. await awaitRunningJobs(adminClient);
  907. const { search: search2 } = await searchForLaptop();
  908. expect(search2.items[0].productAsset).toBeNull();
  909. });
  910. it('updates index when asset is added to a ProductVariant', async () => {
  911. const { search: search1 } = await searchForLaptop();
  912. expect(search1.items[0].productVariantAsset).toBeNull();
  913. await adminClient.query(updateProductVariantsDocument, {
  914. input: search1.items.map(item => ({
  915. id: item.productVariantId,
  916. featuredAssetId: 'T_2',
  917. })),
  918. });
  919. await awaitRunningJobs(adminClient);
  920. const { search: search2 } = await searchForLaptop();
  921. expect(search2.items[0].productVariantAsset!.id).toBe('T_2');
  922. });
  923. });
  924. it('does not include deleted ProductVariants in index', async () => {
  925. const { search: s1 } = await testProductsAdmin({
  926. term: 'hard drive',
  927. groupByProduct: false,
  928. });
  929. await adminClient.query(deleteProductVariantDocument, {
  930. id: s1.items[0].productVariantId,
  931. });
  932. await awaitRunningJobs(adminClient);
  933. const { search } = await adminClient.query(searchGetPricesDocument, {
  934. input: { term: 'hard drive', groupByProduct: true },
  935. });
  936. expect(search.items[0].price).toEqual({
  937. min: 7896,
  938. max: 13435,
  939. });
  940. });
  941. it('returns enabled field when not grouped', async () => {
  942. const result = await testProductsAdmin({ groupByProduct: false, take: 3 });
  943. expect(result.search.items.map(pick(['productVariantId', 'enabled']))).toEqual([
  944. { productVariantId: 'T_1', enabled: true },
  945. { productVariantId: 'T_2', enabled: true },
  946. { productVariantId: 'T_3', enabled: false },
  947. ]);
  948. });
  949. it('when grouped, enabled is true if at least one variant is enabled', async () => {
  950. await adminClient.query(updateProductVariantsDocument, {
  951. input: [
  952. { id: 'T_1', enabled: false },
  953. { id: 'T_2', enabled: false },
  954. ],
  955. });
  956. await awaitRunningJobs(adminClient);
  957. const result = await testProductsAdmin({ groupByProduct: true, take: 3 });
  958. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  959. { productId: 'T_1', enabled: true },
  960. { productId: 'T_2', enabled: true },
  961. { productId: 'T_3', enabled: true },
  962. ]);
  963. });
  964. it('when grouped, enabled is false if all variants are disabled', async () => {
  965. await adminClient.query(updateProductVariantsDocument, {
  966. input: [{ id: 'T_4', enabled: false }],
  967. });
  968. await awaitRunningJobs(adminClient);
  969. const result = await testProductsAdmin({ groupByProduct: true, take: 3 });
  970. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  971. { productId: 'T_1', enabled: false },
  972. { productId: 'T_2', enabled: true },
  973. { productId: 'T_3', enabled: true },
  974. ]);
  975. });
  976. it('when grouped, enabled is false if product is disabled', async () => {
  977. await adminClient.query(updateProductDocument, {
  978. input: {
  979. id: 'T_3',
  980. enabled: false,
  981. },
  982. });
  983. await awaitRunningJobs(adminClient);
  984. const result = await testProductsAdmin({ groupByProduct: true, take: 3 });
  985. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  986. { productId: 'T_1', enabled: false },
  987. { productId: 'T_2', enabled: true },
  988. { productId: 'T_3', enabled: false },
  989. ]);
  990. });
  991. // https://github.com/vendure-ecommerce/vendure/issues/295
  992. it('enabled status survives reindex', async () => {
  993. await adminClient.query(reindexDocument);
  994. await awaitRunningJobs(adminClient);
  995. const result = await testProductsAdmin({ groupByProduct: true, take: 3 });
  996. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  997. { productId: 'T_1', enabled: false },
  998. { productId: 'T_2', enabled: true },
  999. { productId: 'T_3', enabled: false },
  1000. ]);
  1001. });
  1002. // https://github.com/vendure-ecommerce/vendure/issues/1482
  1003. it('price range omits disabled variant', async () => {
  1004. const result1 = await shopClient.query(searchGetPricesShopDocument, {
  1005. input: {
  1006. groupByProduct: true,
  1007. term: 'monitor',
  1008. take: 3,
  1009. } as SearchInput,
  1010. });
  1011. expect(result1.search.items).toEqual([
  1012. {
  1013. price: { min: 14374, max: 16994 },
  1014. priceWithTax: { min: 21561, max: 25491 },
  1015. },
  1016. ]);
  1017. await adminClient.query(updateProductVariantsDocument, {
  1018. input: [{ id: 'T_5', enabled: false }],
  1019. });
  1020. await awaitRunningJobs(adminClient);
  1021. const result2 = await shopClient.query(searchGetPricesShopDocument, {
  1022. input: {
  1023. groupByProduct: true,
  1024. term: 'monitor',
  1025. take: 3,
  1026. } as SearchInput,
  1027. });
  1028. expect(result2.search.items).toEqual([
  1029. {
  1030. price: { min: 16994, max: 16994 },
  1031. priceWithTax: { min: 25491, max: 25491 },
  1032. },
  1033. ]);
  1034. });
  1035. // https://github.com/vendure-ecommerce/vendure/issues/745
  1036. it('very long Product descriptions no not cause indexing to fail', async () => {
  1037. // We generate this long string out of random chars because Postgres uses compression
  1038. // when storing the string value, so e.g. a long series of a single character will not
  1039. // reproduce the error.
  1040. const description = Array.from({ length: 220 })
  1041. .map(() => Math.random().toString(36))
  1042. .join(' ');
  1043. const { createProduct } = await adminClient.query(createProductDocument, {
  1044. input: {
  1045. translations: [
  1046. {
  1047. languageCode: LanguageCode.en,
  1048. name: 'Very long description aabbccdd',
  1049. slug: 'very-long-description',
  1050. description,
  1051. },
  1052. ],
  1053. },
  1054. });
  1055. await adminClient.query(createProductVariantsDocument, {
  1056. input: [
  1057. {
  1058. productId: createProduct.id,
  1059. sku: 'VLD01',
  1060. price: 100,
  1061. translations: [
  1062. { languageCode: LanguageCode.en, name: 'Very long description variant' },
  1063. ],
  1064. },
  1065. ],
  1066. });
  1067. await awaitRunningJobs(adminClient);
  1068. const result = await testProductsAdmin({ term: 'aabbccdd' });
  1069. expect(result.search.items.map(i => i.productName)).toEqual([
  1070. 'Very long description aabbccdd',
  1071. ]);
  1072. await adminClient.query(deleteProductDocument, {
  1073. id: createProduct.id,
  1074. });
  1075. });
  1076. });
  1077. // https://github.com/vendure-ecommerce/vendure/issues/609
  1078. describe('Synthetic index items', () => {
  1079. let createdProductId: string;
  1080. it('creates synthetic index item for Product with no variants', async () => {
  1081. const { createProduct } = await adminClient.query(createProductDocument, {
  1082. input: {
  1083. facetValueIds: ['T_1'],
  1084. translations: [
  1085. {
  1086. languageCode: LanguageCode.en,
  1087. name: 'Strawberry cheesecake',
  1088. slug: 'strawberry-cheesecake',
  1089. description: 'A yummy dessert',
  1090. },
  1091. ],
  1092. },
  1093. });
  1094. await awaitRunningJobs(adminClient);
  1095. const result = await testProductsAdmin({ groupByProduct: true, term: 'strawberry' });
  1096. expect(
  1097. result.search.items.map(
  1098. pick([
  1099. 'productId',
  1100. 'enabled',
  1101. 'productName',
  1102. 'productVariantName',
  1103. 'slug',
  1104. 'description',
  1105. ]),
  1106. ),
  1107. ).toEqual([
  1108. {
  1109. productId: createProduct.id,
  1110. enabled: false,
  1111. productName: 'Strawberry cheesecake',
  1112. productVariantName: 'Strawberry cheesecake',
  1113. slug: 'strawberry-cheesecake',
  1114. description: 'A yummy dessert',
  1115. },
  1116. ]);
  1117. createdProductId = createProduct.id;
  1118. });
  1119. it('removes synthetic index item once a variant is created', async () => {
  1120. await adminClient.query(createProductVariantsDocument, {
  1121. input: [
  1122. {
  1123. productId: createdProductId,
  1124. sku: 'SC01',
  1125. price: 1399,
  1126. translations: [
  1127. { languageCode: LanguageCode.en, name: 'Strawberry Cheesecake Pie' },
  1128. ],
  1129. },
  1130. ],
  1131. });
  1132. await awaitRunningJobs(adminClient);
  1133. const result = await testProductsAdmin({ groupByProduct: false, term: 'strawberry' });
  1134. expect(result.search.items.map(pick(['productVariantName']))).toEqual([
  1135. { productVariantName: 'Strawberry Cheesecake Pie' },
  1136. ]);
  1137. });
  1138. });
  1139. describe('channel handling', () => {
  1140. const SECOND_CHANNEL_TOKEN = 'second-channel-token';
  1141. let secondChannel: FragmentOf<typeof channelFragment>;
  1142. beforeAll(async () => {
  1143. const { createChannel } = await adminClient.query(createChannelDocument, {
  1144. input: {
  1145. code: 'second-channel',
  1146. token: SECOND_CHANNEL_TOKEN,
  1147. defaultLanguageCode: LanguageCode.en,
  1148. availableLanguageCodes: [LanguageCode.en, LanguageCode.de, LanguageCode.zh],
  1149. currencyCode: CurrencyCode.GBP,
  1150. pricesIncludeTax: true,
  1151. defaultTaxZoneId: 'T_1',
  1152. defaultShippingZoneId: 'T_1',
  1153. },
  1154. });
  1155. const channelGuard: ErrorResultGuard<FragmentOf<typeof channelFragment>> =
  1156. createErrorResultGuard(input => !!input && !('errorCode' in input));
  1157. channelGuard.assertSuccess(createChannel);
  1158. secondChannel = createChannel;
  1159. });
  1160. it('assign product to channel', async () => {
  1161. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  1162. await adminClient.query(assignProductToChannelDocument, {
  1163. input: { channelId: secondChannel.id, productIds: ['T_1', 'T_2'] },
  1164. });
  1165. await awaitRunningJobs(adminClient);
  1166. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  1167. const { search } = await testProductsAdmin({ groupByProduct: true });
  1168. expect(search.items.map(i => i.productId)).toEqual(['T_1', 'T_2']);
  1169. }, 10000);
  1170. it('removing product from channel', async () => {
  1171. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  1172. await adminClient.query(removeProductFromChannelDocument, {
  1173. input: {
  1174. productIds: ['T_2'],
  1175. channelId: secondChannel.id,
  1176. },
  1177. });
  1178. await awaitRunningJobs(adminClient);
  1179. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  1180. const { search } = await testProductsAdmin({ groupByProduct: true });
  1181. expect(search.items.map(i => i.productId)).toEqual(['T_1']);
  1182. }, 10000);
  1183. it('assign product variant to channel', async () => {
  1184. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  1185. await adminClient.query(assignProductVariantToChannelDocument, {
  1186. input: { channelId: secondChannel.id, productVariantIds: ['T_10', 'T_15'] },
  1187. });
  1188. await awaitRunningJobs(adminClient);
  1189. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  1190. const { search: searchGrouped } = await testProductsAdmin({ groupByProduct: true });
  1191. expect(searchGrouped.items.map(i => i.productId)).toEqual(['T_1', 'T_3', 'T_4']);
  1192. const { search: searchUngrouped } = await testProductsAdmin({ groupByProduct: false });
  1193. expect(searchUngrouped.items.map(i => i.productVariantId)).toEqual([
  1194. 'T_1',
  1195. 'T_2',
  1196. 'T_3',
  1197. 'T_4',
  1198. 'T_10',
  1199. 'T_15',
  1200. ]);
  1201. }, 10000);
  1202. it('removing product variant from channel', async () => {
  1203. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  1204. await adminClient.query(removeProductVariantFromChannelDocument, {
  1205. input: { channelId: secondChannel.id, productVariantIds: ['T_1', 'T_15'] },
  1206. });
  1207. await awaitRunningJobs(adminClient);
  1208. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  1209. const { search: searchGrouped } = await testProductsAdmin({ groupByProduct: true });
  1210. expect(searchGrouped.items.map(i => i.productId)).toEqual(['T_1', 'T_3']);
  1211. const { search: searchUngrouped } = await testProductsAdmin({ groupByProduct: false });
  1212. expect(searchUngrouped.items.map(i => i.productVariantId)).toEqual([
  1213. 'T_2',
  1214. 'T_3',
  1215. 'T_4',
  1216. 'T_10',
  1217. ]);
  1218. }, 10000);
  1219. it('updating product affects current channel', async () => {
  1220. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  1221. await adminClient.query(updateProductDocument, {
  1222. input: {
  1223. id: 'T_3',
  1224. enabled: true,
  1225. translations: [{ languageCode: LanguageCode.en, name: 'xyz' }],
  1226. },
  1227. });
  1228. await awaitRunningJobs(adminClient);
  1229. const { search: searchGrouped } = await testProductsAdmin({
  1230. groupByProduct: true,
  1231. term: 'xyz',
  1232. });
  1233. expect(searchGrouped.items.map(i => i.productName)).toEqual(['xyz']);
  1234. });
  1235. it('updating product affects other channels', async () => {
  1236. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  1237. const { search: searchGrouped } = await testProductsAdmin({
  1238. groupByProduct: true,
  1239. term: 'xyz',
  1240. });
  1241. expect(searchGrouped.items.map(i => i.productName)).toEqual(['xyz']);
  1242. });
  1243. // https://github.com/vendure-ecommerce/vendure/issues/896
  1244. it('removing from channel with multiple languages', async () => {
  1245. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  1246. await adminClient.query(updateProductDocument, {
  1247. input: {
  1248. id: 'T_4',
  1249. translations: [
  1250. {
  1251. languageCode: LanguageCode.en,
  1252. name: 'product en',
  1253. slug: 'product-en',
  1254. description: 'en',
  1255. },
  1256. {
  1257. languageCode: LanguageCode.de,
  1258. name: 'product de',
  1259. slug: 'product-de',
  1260. description: 'de',
  1261. },
  1262. ],
  1263. },
  1264. });
  1265. await adminClient.query(assignProductToChannelDocument, {
  1266. input: { channelId: secondChannel.id, productIds: ['T_4'] },
  1267. });
  1268. await awaitRunningJobs(adminClient);
  1269. async function searchSecondChannelForDEProduct() {
  1270. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  1271. const { search } = await adminClient.query(
  1272. searchProductsAdminDocument,
  1273. {
  1274. input: { term: 'product', groupByProduct: true },
  1275. },
  1276. { languageCode: LanguageCode.de },
  1277. );
  1278. return search;
  1279. }
  1280. const search1 = await searchSecondChannelForDEProduct();
  1281. expect(search1.items.map(i => i.productName)).toEqual(['product de']);
  1282. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  1283. await adminClient.query(removeProductFromChannelDocument, {
  1284. input: {
  1285. productIds: ['T_4'],
  1286. channelId: secondChannel.id,
  1287. },
  1288. });
  1289. await awaitRunningJobs(adminClient);
  1290. const search2 = await searchSecondChannelForDEProduct();
  1291. expect(search2.items.map(i => i.productName)).toEqual([]);
  1292. });
  1293. });
  1294. describe('multiple language handling', () => {
  1295. beforeAll(async () => {
  1296. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  1297. await adminClient.query(updateChannelDocument, {
  1298. input: {
  1299. id: 'T_1',
  1300. availableLanguageCodes: [LanguageCode.en, LanguageCode.de, LanguageCode.zh],
  1301. },
  1302. });
  1303. const { updateProduct } = await adminClient.query(updateProductDocument, {
  1304. input: {
  1305. id: 'T_1',
  1306. translations: [
  1307. {
  1308. languageCode: LanguageCode.en,
  1309. name: 'Laptop en',
  1310. slug: 'laptop-slug-en',
  1311. description: 'Laptop description en',
  1312. },
  1313. {
  1314. languageCode: LanguageCode.de,
  1315. name: 'Laptop de',
  1316. slug: 'laptop-slug-de',
  1317. description: 'Laptop description de',
  1318. },
  1319. {
  1320. languageCode: LanguageCode.zh,
  1321. name: 'Laptop zh',
  1322. slug: 'laptop-slug-zh',
  1323. description: 'Laptop description zh',
  1324. },
  1325. ],
  1326. },
  1327. });
  1328. expect(updateProduct.variants.length).toEqual(4);
  1329. await adminClient.query(updateProductVariantsDocument, {
  1330. input: [
  1331. {
  1332. id: updateProduct.variants[0].id,
  1333. translations: [
  1334. {
  1335. languageCode: LanguageCode.en,
  1336. name: 'Laptop variant T_1 en',
  1337. },
  1338. {
  1339. languageCode: LanguageCode.de,
  1340. name: 'Laptop variant T_1 de',
  1341. },
  1342. {
  1343. languageCode: LanguageCode.zh,
  1344. name: 'Laptop variant T_1 zh',
  1345. },
  1346. ],
  1347. },
  1348. {
  1349. id: updateProduct.variants[1].id,
  1350. translations: [
  1351. {
  1352. languageCode: LanguageCode.en,
  1353. name: 'Laptop variant T_2 en',
  1354. },
  1355. {
  1356. languageCode: LanguageCode.de,
  1357. name: 'Laptop variant T_2 de',
  1358. },
  1359. ],
  1360. },
  1361. {
  1362. id: updateProduct.variants[2].id,
  1363. translations: [
  1364. {
  1365. languageCode: LanguageCode.en,
  1366. name: 'Laptop variant T_3 en',
  1367. },
  1368. {
  1369. languageCode: LanguageCode.zh,
  1370. name: 'Laptop variant T_3 zh',
  1371. },
  1372. ],
  1373. },
  1374. {
  1375. id: updateProduct.variants[3].id,
  1376. translations: [
  1377. {
  1378. languageCode: LanguageCode.en,
  1379. name: 'Laptop variant T_4 en',
  1380. },
  1381. ],
  1382. },
  1383. ],
  1384. });
  1385. await awaitRunningJobs(adminClient);
  1386. });
  1387. describe('search products', () => {
  1388. function searchInLanguage(languageCode: LanguageCode) {
  1389. return adminClient.query(
  1390. searchProductsAdminDocument,
  1391. {
  1392. input: {
  1393. take: 100,
  1394. },
  1395. },
  1396. {
  1397. languageCode,
  1398. },
  1399. );
  1400. }
  1401. it('fallbacks to default language en', async () => {
  1402. const { search } = await searchInLanguage(LanguageCode.af);
  1403. const laptopVariants = search.items.filter(i => i.productId === 'T_1');
  1404. expect(laptopVariants.length).toEqual(4);
  1405. const laptopVariantT1 = laptopVariants.find(i => i.productVariantId === 'T_1');
  1406. expect(laptopVariantT1?.productVariantName).toEqual('Laptop variant T_1 en');
  1407. expect(laptopVariantT1?.productName).toEqual('Laptop en');
  1408. expect(laptopVariantT1?.slug).toEqual('laptop-slug-en');
  1409. expect(laptopVariantT1?.description).toEqual('Laptop description en');
  1410. const laptopVariantT2 = laptopVariants.find(i => i.productVariantId === 'T_2');
  1411. expect(laptopVariantT2?.productVariantName).toEqual('Laptop variant T_2 en');
  1412. expect(laptopVariantT2?.productName).toEqual('Laptop en');
  1413. expect(laptopVariantT2?.slug).toEqual('laptop-slug-en');
  1414. expect(laptopVariantT2?.description).toEqual('Laptop description en');
  1415. const laptopVariantT3 = laptopVariants.find(i => i.productVariantId === 'T_3');
  1416. expect(laptopVariantT3?.productVariantName).toEqual('Laptop variant T_3 en');
  1417. expect(laptopVariantT3?.productName).toEqual('Laptop en');
  1418. expect(laptopVariantT3?.slug).toEqual('laptop-slug-en');
  1419. expect(laptopVariantT3?.description).toEqual('Laptop description en');
  1420. const laptopVariantT4 = laptopVariants.find(i => i.productVariantId === 'T_4');
  1421. expect(laptopVariantT4?.productVariantName).toEqual('Laptop variant T_4 en');
  1422. expect(laptopVariantT4?.productName).toEqual('Laptop en');
  1423. expect(laptopVariantT4?.slug).toEqual('laptop-slug-en');
  1424. expect(laptopVariantT4?.description).toEqual('Laptop description en');
  1425. });
  1426. it('indexes non-default language de when it is available language of channel', async () => {
  1427. const { search } = await searchInLanguage(LanguageCode.de);
  1428. const laptopVariants = search.items.filter(i => i.productId === 'T_1');
  1429. expect(laptopVariants.length).toEqual(4);
  1430. const laptopVariantT1 = laptopVariants.find(i => i.productVariantId === 'T_1');
  1431. expect(laptopVariantT1?.productVariantName).toEqual('Laptop variant T_1 de');
  1432. expect(laptopVariantT1?.productName).toEqual('Laptop de');
  1433. expect(laptopVariantT1?.slug).toEqual('laptop-slug-de');
  1434. expect(laptopVariantT1?.description).toEqual('Laptop description de');
  1435. const laptopVariantT2 = laptopVariants.find(i => i.productVariantId === 'T_2');
  1436. expect(laptopVariantT2?.productVariantName).toEqual('Laptop variant T_2 de');
  1437. expect(laptopVariantT2?.productName).toEqual('Laptop de');
  1438. expect(laptopVariantT2?.slug).toEqual('laptop-slug-de');
  1439. expect(laptopVariantT2?.description).toEqual('Laptop description de');
  1440. const laptopVariantT3 = laptopVariants.find(i => i.productVariantId === 'T_3');
  1441. expect(laptopVariantT3?.productVariantName).toEqual('Laptop variant T_3 en');
  1442. expect(laptopVariantT3?.productName).toEqual('Laptop de');
  1443. expect(laptopVariantT3?.slug).toEqual('laptop-slug-de');
  1444. expect(laptopVariantT3?.description).toEqual('Laptop description de');
  1445. const laptopVariantT4 = laptopVariants.find(i => i.productVariantId === 'T_4');
  1446. expect(laptopVariantT4?.productVariantName).toEqual('Laptop variant T_4 en');
  1447. expect(laptopVariantT4?.productName).toEqual('Laptop de');
  1448. expect(laptopVariantT4?.slug).toEqual('laptop-slug-de');
  1449. expect(laptopVariantT4?.description).toEqual('Laptop description de');
  1450. });
  1451. it('indexes non-default language zh when it is available language of channel', async () => {
  1452. const { search } = await searchInLanguage(LanguageCode.zh);
  1453. const laptopVariants = search.items.filter(i => i.productId === 'T_1');
  1454. expect(laptopVariants.length).toEqual(4);
  1455. const laptopVariantT1 = laptopVariants.find(i => i.productVariantId === 'T_1');
  1456. expect(laptopVariantT1?.productVariantName).toEqual('Laptop variant T_1 zh');
  1457. expect(laptopVariantT1?.productName).toEqual('Laptop zh');
  1458. expect(laptopVariantT1?.slug).toEqual('laptop-slug-zh');
  1459. expect(laptopVariantT1?.description).toEqual('Laptop description zh');
  1460. const laptopVariantT2 = laptopVariants.find(i => i.productVariantId === 'T_2');
  1461. expect(laptopVariantT2?.productVariantName).toEqual('Laptop variant T_2 en');
  1462. expect(laptopVariantT2?.productName).toEqual('Laptop zh');
  1463. expect(laptopVariantT2?.slug).toEqual('laptop-slug-zh');
  1464. expect(laptopVariantT2?.description).toEqual('Laptop description zh');
  1465. const laptopVariantT3 = laptopVariants.find(i => i.productVariantId === 'T_3');
  1466. expect(laptopVariantT3?.productVariantName).toEqual('Laptop variant T_3 zh');
  1467. expect(laptopVariantT3?.productName).toEqual('Laptop zh');
  1468. expect(laptopVariantT3?.slug).toEqual('laptop-slug-zh');
  1469. expect(laptopVariantT3?.description).toEqual('Laptop description zh');
  1470. const laptopVariantT4 = laptopVariants.find(i => i.productVariantId === 'T_4');
  1471. expect(laptopVariantT4?.productVariantName).toEqual('Laptop variant T_4 en');
  1472. expect(laptopVariantT4?.productName).toEqual('Laptop zh');
  1473. expect(laptopVariantT4?.slug).toEqual('laptop-slug-zh');
  1474. expect(laptopVariantT4?.description).toEqual('Laptop description zh');
  1475. });
  1476. });
  1477. describe('search products grouped by product and sorted by name ASC', () => {
  1478. function searchInLanguage(languageCode: LanguageCode) {
  1479. return adminClient.query(
  1480. searchProductsAdminDocument,
  1481. {
  1482. input: {
  1483. groupByProduct: true,
  1484. take: 100,
  1485. sort: {
  1486. name: SortOrder.ASC,
  1487. },
  1488. },
  1489. },
  1490. {
  1491. languageCode,
  1492. },
  1493. );
  1494. }
  1495. // https://github.com/vendure-ecommerce/vendure/issues/1752
  1496. // https://github.com/vendure-ecommerce/vendure/issues/1746
  1497. it('fallbacks to default language en', async () => {
  1498. const { search } = await searchInLanguage(LanguageCode.af);
  1499. const productNames = [
  1500. 'Bonsai Tree',
  1501. 'Boxing Gloves',
  1502. 'Camera Lens',
  1503. 'Cruiser Skateboard',
  1504. 'Curvy Monitor',
  1505. 'Football',
  1506. 'Gaming PC',
  1507. 'Instant Camera',
  1508. 'Laptop en', // fallback language en
  1509. 'Orchid',
  1510. 'product en', // fallback language en
  1511. 'Road Bike',
  1512. 'Running Shoe',
  1513. 'Skipping Rope',
  1514. 'Slr Camera',
  1515. 'Spiky Cactus',
  1516. 'Strawberry cheesecake',
  1517. 'Tent',
  1518. 'Tripod',
  1519. 'USB Cable',
  1520. ];
  1521. expect(search.items.map(i => i.productName)).toEqual(productNames);
  1522. });
  1523. it('indexes non-default language de', async () => {
  1524. const { search } = await searchInLanguage(LanguageCode.de);
  1525. const productNames = [
  1526. 'Bonsai Tree',
  1527. 'Boxing Gloves',
  1528. 'Camera Lens',
  1529. 'Cruiser Skateboard',
  1530. 'Curvy Monitor',
  1531. 'Football',
  1532. 'Gaming PC',
  1533. 'Instant Camera',
  1534. 'Laptop de', // language de
  1535. 'Orchid',
  1536. 'product de', // language de
  1537. 'Road Bike',
  1538. 'Running Shoe',
  1539. 'Skipping Rope',
  1540. 'Slr Camera',
  1541. 'Spiky Cactus',
  1542. 'Strawberry cheesecake',
  1543. 'Tent',
  1544. 'Tripod',
  1545. 'USB Cable',
  1546. ];
  1547. expect(search.items.map(i => i.productName)).toEqual(productNames);
  1548. });
  1549. it('indexes non-default language zh', async () => {
  1550. const { search } = await searchInLanguage(LanguageCode.zh);
  1551. const productNames = [
  1552. 'Bonsai Tree',
  1553. 'Boxing Gloves',
  1554. 'Camera Lens',
  1555. 'Cruiser Skateboard',
  1556. 'Curvy Monitor',
  1557. 'Football',
  1558. 'Gaming PC',
  1559. 'Instant Camera',
  1560. 'Laptop zh', // language zh
  1561. 'Orchid',
  1562. 'product en', // fallback language en
  1563. 'Road Bike',
  1564. 'Running Shoe',
  1565. 'Skipping Rope',
  1566. 'Slr Camera',
  1567. 'Spiky Cactus',
  1568. 'Strawberry cheesecake',
  1569. 'Tent',
  1570. 'Tripod',
  1571. 'USB Cable',
  1572. ];
  1573. expect(search.items.map(i => i.productName)).toEqual(productNames);
  1574. });
  1575. });
  1576. });
  1577. // https://github.com/vendure-ecommerce/vendure/issues/1789
  1578. describe('input escaping', () => {
  1579. function search(term: string) {
  1580. return adminClient.query(
  1581. searchProductsAdminDocument,
  1582. {
  1583. input: { take: 10, term },
  1584. },
  1585. {
  1586. languageCode: LanguageCode.en,
  1587. },
  1588. );
  1589. }
  1590. it('correctly escapes "a & b"', async () => {
  1591. const result = await search('laptop & camera');
  1592. expect(result.search.items).toBeDefined();
  1593. });
  1594. it('correctly escapes other special chars', async () => {
  1595. const result = await search('a : b ? * (c) ! "foo"');
  1596. expect(result.search.items).toBeDefined();
  1597. });
  1598. it('correctly escapes mysql binary mode chars', async () => {
  1599. expect((await search('foo+')).search.items).toBeDefined();
  1600. expect((await search('foo-')).search.items).toBeDefined();
  1601. expect((await search('foo<')).search.items).toBeDefined();
  1602. expect((await search('foo>')).search.items).toBeDefined();
  1603. expect((await search('foo*')).search.items).toBeDefined();
  1604. expect((await search('foo~')).search.items).toBeDefined();
  1605. expect((await search('foo@bar')).search.items).toBeDefined();
  1606. expect((await search('foo + - *')).search.items).toBeDefined();
  1607. expect((await search('foo + - bar')).search.items).toBeDefined();
  1608. });
  1609. });
  1610. });
  1611. });