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

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