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

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