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

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