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

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