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

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