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

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