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

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