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

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