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

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