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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /* tslint:disable:no-non-null-assertion */
  2. import { pick } from '@vendure/common/lib/pick';
  3. import {
  4. DefaultJobQueuePlugin,
  5. DefaultSearchPlugin,
  6. facetValueCollectionFilter,
  7. mergeConfig,
  8. } from '@vendure/core';
  9. import { createTestEnvironment, E2E_DEFAULT_CHANNEL_TOKEN, SimpleGraphQLClient } from '@vendure/testing';
  10. import gql from 'graphql-tag';
  11. import path from 'path';
  12. import { initialData } from '../../../e2e-common/e2e-initial-data';
  13. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  14. import {
  15. AssignProductsToChannel,
  16. AssignProductVariantsToChannel,
  17. ChannelFragment,
  18. CreateChannel,
  19. CreateCollection,
  20. CreateFacet,
  21. CreateProduct,
  22. CreateProductVariants,
  23. CurrencyCode,
  24. DeleteAsset,
  25. DeleteProduct,
  26. DeleteProductVariant,
  27. LanguageCode,
  28. Reindex,
  29. RemoveProductsFromChannel,
  30. RemoveProductVariantsFromChannel,
  31. SearchFacetValues,
  32. SearchGetAssets,
  33. SearchGetPrices,
  34. SearchInput,
  35. SearchResultSortParameter,
  36. SortOrder,
  37. UpdateAsset,
  38. UpdateCollection,
  39. UpdateProduct,
  40. UpdateProductVariants,
  41. UpdateTaxRate,
  42. } from './graphql/generated-e2e-admin-types';
  43. import { LogicalOperator, SearchProductsShop } from './graphql/generated-e2e-shop-types';
  44. import {
  45. ASSIGN_PRODUCTVARIANT_TO_CHANNEL,
  46. ASSIGN_PRODUCT_TO_CHANNEL,
  47. CREATE_CHANNEL,
  48. CREATE_COLLECTION,
  49. CREATE_FACET,
  50. CREATE_PRODUCT,
  51. CREATE_PRODUCT_VARIANTS,
  52. DELETE_ASSET,
  53. DELETE_PRODUCT,
  54. DELETE_PRODUCT_VARIANT,
  55. REMOVE_PRODUCTVARIANT_FROM_CHANNEL,
  56. REMOVE_PRODUCT_FROM_CHANNEL,
  57. UPDATE_ASSET,
  58. UPDATE_COLLECTION,
  59. UPDATE_PRODUCT,
  60. UPDATE_PRODUCT_VARIANTS,
  61. UPDATE_TAX_RATE,
  62. } from './graphql/shared-definitions';
  63. import { SEARCH_PRODUCTS_SHOP } from './graphql/shop-definitions';
  64. import { awaitRunningJobs } from './utils/await-running-jobs';
  65. describe('Default search plugin', () => {
  66. const { server, adminClient, shopClient } = createTestEnvironment(
  67. mergeConfig(testConfig, {
  68. plugins: [DefaultSearchPlugin, DefaultJobQueuePlugin],
  69. }),
  70. );
  71. beforeAll(async () => {
  72. await server.init({
  73. initialData,
  74. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  75. customerCount: 1,
  76. });
  77. await adminClient.asSuperAdmin();
  78. }, TEST_SETUP_TIMEOUT_MS);
  79. afterAll(async () => {
  80. await awaitRunningJobs(adminClient);
  81. await server.destroy();
  82. });
  83. function doAdminSearchQuery(input: SearchInput) {
  84. return adminClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(SEARCH_PRODUCTS, {
  85. input,
  86. });
  87. }
  88. async function testGroupByProduct(client: SimpleGraphQLClient) {
  89. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  90. SEARCH_PRODUCTS_SHOP,
  91. {
  92. input: {
  93. groupByProduct: true,
  94. },
  95. },
  96. );
  97. expect(result.search.totalItems).toBe(20);
  98. }
  99. async function testNoGrouping(client: SimpleGraphQLClient) {
  100. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  101. SEARCH_PRODUCTS_SHOP,
  102. {
  103. input: {
  104. groupByProduct: false,
  105. },
  106. },
  107. );
  108. expect(result.search.totalItems).toBe(34);
  109. }
  110. async function testSortingWithGrouping(
  111. client: SimpleGraphQLClient,
  112. sortBy: keyof SearchResultSortParameter,
  113. ) {
  114. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  115. SEARCH_PRODUCTS_SHOP,
  116. {
  117. input: {
  118. groupByProduct: true,
  119. sort: {
  120. [sortBy]: SortOrder.ASC,
  121. },
  122. take: 3,
  123. },
  124. },
  125. );
  126. const expected =
  127. sortBy === 'name'
  128. ? ['Bonsai Tree', 'Boxing Gloves', 'Camera Lens']
  129. : ['Skipping Rope', 'Tripod', 'Spiky Cactus'];
  130. expect(result.search.items.map(i => i.productName)).toEqual(expected);
  131. }
  132. async function testSortingNoGrouping(
  133. client: SimpleGraphQLClient,
  134. sortBy: keyof SearchResultSortParameter,
  135. ) {
  136. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  137. SEARCH_PRODUCTS_SHOP,
  138. {
  139. input: {
  140. groupByProduct: false,
  141. sort: {
  142. [sortBy]: SortOrder.DESC,
  143. },
  144. take: 3,
  145. },
  146. },
  147. );
  148. const expected =
  149. sortBy === 'name'
  150. ? ['USB Cable', 'Tripod', 'Tent']
  151. : ['Road Bike', 'Laptop 15 inch 16GB', 'Laptop 13 inch 16GB'];
  152. expect(result.search.items.map(i => i.productVariantName)).toEqual(expected);
  153. }
  154. async function testMatchSearchTerm(client: SimpleGraphQLClient) {
  155. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  156. SEARCH_PRODUCTS_SHOP,
  157. {
  158. input: {
  159. term: 'camera',
  160. groupByProduct: true,
  161. sort: {
  162. name: SortOrder.ASC,
  163. },
  164. },
  165. },
  166. );
  167. expect(result.search.items.map(i => i.productName)).toEqual([
  168. 'Camera Lens',
  169. 'Instant Camera',
  170. 'Slr Camera',
  171. ]);
  172. }
  173. async function testMatchFacetIdsAnd(client: SimpleGraphQLClient) {
  174. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  175. SEARCH_PRODUCTS_SHOP,
  176. {
  177. input: {
  178. facetValueIds: ['T_1', 'T_2'],
  179. facetValueOperator: LogicalOperator.AND,
  180. groupByProduct: true,
  181. },
  182. },
  183. );
  184. expect(result.search.items.map(i => i.productName)).toEqual([
  185. 'Laptop',
  186. 'Curvy Monitor',
  187. 'Gaming PC',
  188. 'Hard Drive',
  189. 'Clacky Keyboard',
  190. 'USB Cable',
  191. ]);
  192. }
  193. async function testMatchFacetIdsOr(client: SimpleGraphQLClient) {
  194. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  195. SEARCH_PRODUCTS_SHOP,
  196. {
  197. input: {
  198. facetValueIds: ['T_1', 'T_5'],
  199. facetValueOperator: LogicalOperator.OR,
  200. groupByProduct: true,
  201. },
  202. },
  203. );
  204. expect(result.search.items.map(i => i.productName)).toEqual([
  205. 'Laptop',
  206. 'Curvy Monitor',
  207. 'Gaming PC',
  208. 'Hard Drive',
  209. 'Clacky Keyboard',
  210. 'USB Cable',
  211. 'Instant Camera',
  212. 'Camera Lens',
  213. 'Tripod',
  214. 'Slr Camera',
  215. 'Spiky Cactus',
  216. 'Orchid',
  217. 'Bonsai Tree',
  218. ]);
  219. }
  220. async function testMatchCollectionId(client: SimpleGraphQLClient) {
  221. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  222. SEARCH_PRODUCTS_SHOP,
  223. {
  224. input: {
  225. collectionId: 'T_2',
  226. groupByProduct: true,
  227. },
  228. },
  229. );
  230. expect(result.search.items.map(i => i.productName)).toEqual([
  231. 'Spiky Cactus',
  232. 'Orchid',
  233. 'Bonsai Tree',
  234. ]);
  235. }
  236. async function testMatchCollectionSlug(client: SimpleGraphQLClient) {
  237. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  238. SEARCH_PRODUCTS_SHOP,
  239. {
  240. input: {
  241. collectionSlug: 'plants',
  242. groupByProduct: true,
  243. },
  244. },
  245. );
  246. expect(result.search.items.map(i => i.productName)).toEqual([
  247. 'Spiky Cactus',
  248. 'Orchid',
  249. 'Bonsai Tree',
  250. ]);
  251. }
  252. async function testSinglePrices(client: SimpleGraphQLClient) {
  253. const result = await client.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
  254. SEARCH_GET_PRICES,
  255. {
  256. input: {
  257. groupByProduct: false,
  258. take: 3,
  259. } as SearchInput,
  260. },
  261. );
  262. expect(result.search.items).toEqual([
  263. {
  264. price: { value: 129900 },
  265. priceWithTax: { value: 155880 },
  266. },
  267. {
  268. price: { value: 139900 },
  269. priceWithTax: { value: 167880 },
  270. },
  271. {
  272. price: { value: 219900 },
  273. priceWithTax: { value: 263880 },
  274. },
  275. ]);
  276. }
  277. async function testPriceRanges(client: SimpleGraphQLClient) {
  278. const result = await client.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
  279. SEARCH_GET_PRICES,
  280. {
  281. input: {
  282. groupByProduct: true,
  283. take: 3,
  284. } as SearchInput,
  285. },
  286. );
  287. expect(result.search.items).toEqual([
  288. {
  289. price: { min: 129900, max: 229900 },
  290. priceWithTax: { min: 155880, max: 275880 },
  291. },
  292. {
  293. price: { min: 14374, max: 16994 },
  294. priceWithTax: { min: 17249, max: 20393 },
  295. },
  296. {
  297. price: { min: 93120, max: 109995 },
  298. priceWithTax: { min: 111744, max: 131994 },
  299. },
  300. ]);
  301. }
  302. describe('shop api', () => {
  303. it('group by product', () => testGroupByProduct(shopClient));
  304. it('no grouping', () => testNoGrouping(shopClient));
  305. it('matches search term', () => testMatchSearchTerm(shopClient));
  306. it('matches by facetId with AND operator', () => testMatchFacetIdsAnd(shopClient));
  307. it('matches by facetId with OR operator', () => testMatchFacetIdsOr(shopClient));
  308. it('matches by collectionId', () => testMatchCollectionId(shopClient));
  309. it('matches by collectionSlug', () => testMatchCollectionSlug(shopClient));
  310. it('single prices', () => testSinglePrices(shopClient));
  311. it('price ranges', () => testPriceRanges(shopClient));
  312. it('returns correct facetValues when not grouped by product', async () => {
  313. const result = await shopClient.query<SearchFacetValues.Query, SearchFacetValues.Variables>(
  314. SEARCH_GET_FACET_VALUES,
  315. {
  316. input: {
  317. groupByProduct: false,
  318. },
  319. },
  320. );
  321. expect(result.search.facetValues).toEqual([
  322. { count: 21, facetValue: { id: 'T_1', name: 'electronics' } },
  323. { count: 17, facetValue: { id: 'T_2', name: 'computers' } },
  324. { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
  325. { count: 10, facetValue: { id: 'T_4', name: 'sports equipment' } },
  326. { count: 3, facetValue: { id: 'T_5', name: 'home & garden' } },
  327. { count: 3, facetValue: { id: 'T_6', name: 'plants' } },
  328. ]);
  329. });
  330. it('returns correct facetValues when grouped by product', async () => {
  331. const result = await shopClient.query<SearchFacetValues.Query, SearchFacetValues.Variables>(
  332. SEARCH_GET_FACET_VALUES,
  333. {
  334. input: {
  335. groupByProduct: true,
  336. },
  337. },
  338. );
  339. expect(result.search.facetValues).toEqual([
  340. { count: 10, facetValue: { id: 'T_1', name: 'electronics' } },
  341. { count: 6, facetValue: { id: 'T_2', name: 'computers' } },
  342. { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
  343. { count: 7, facetValue: { id: 'T_4', name: 'sports equipment' } },
  344. { count: 3, facetValue: { id: 'T_5', name: 'home & garden' } },
  345. { count: 3, facetValue: { id: 'T_6', name: 'plants' } },
  346. ]);
  347. });
  348. it('omits facetValues of private facets', async () => {
  349. const { createFacet } = await adminClient.query<CreateFacet.Mutation, CreateFacet.Variables>(
  350. CREATE_FACET,
  351. {
  352. input: {
  353. code: 'profit-margin',
  354. isPrivate: true,
  355. translations: [{ languageCode: LanguageCode.en, name: 'Profit Margin' }],
  356. values: [
  357. {
  358. code: 'massive',
  359. translations: [{ languageCode: LanguageCode.en, name: 'massive' }],
  360. },
  361. ],
  362. },
  363. },
  364. );
  365. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  366. input: {
  367. id: 'T_2',
  368. // T_1 & T_2 are the existing facetValues (electronics & photo)
  369. facetValueIds: ['T_1', 'T_2', createFacet.values[0].id],
  370. },
  371. });
  372. await awaitRunningJobs(adminClient);
  373. const result = await shopClient.query<SearchFacetValues.Query, SearchFacetValues.Variables>(
  374. SEARCH_GET_FACET_VALUES,
  375. {
  376. input: {
  377. groupByProduct: true,
  378. },
  379. },
  380. );
  381. expect(result.search.facetValues).toEqual([
  382. { count: 10, facetValue: { id: 'T_1', name: 'electronics' } },
  383. { count: 6, facetValue: { id: 'T_2', name: 'computers' } },
  384. { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
  385. { count: 7, facetValue: { id: 'T_4', name: 'sports equipment' } },
  386. { count: 3, facetValue: { id: 'T_5', name: 'home & garden' } },
  387. { count: 3, facetValue: { id: 'T_6', name: 'plants' } },
  388. ]);
  389. });
  390. it('encodes the productId and productVariantId', async () => {
  391. const result = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  392. SEARCH_PRODUCTS_SHOP,
  393. {
  394. input: {
  395. groupByProduct: false,
  396. take: 1,
  397. },
  398. },
  399. );
  400. expect(pick(result.search.items[0], ['productId', 'productVariantId'])).toEqual({
  401. productId: 'T_1',
  402. productVariantId: 'T_1',
  403. });
  404. });
  405. it('sort name with grouping', () => testSortingWithGrouping(shopClient, 'name'));
  406. it('sort price with grouping', () => testSortingWithGrouping(shopClient, 'price'));
  407. it('sort name without grouping', () => testSortingNoGrouping(shopClient, 'name'));
  408. it('sort price without grouping', () => testSortingNoGrouping(shopClient, 'price'));
  409. it('omits results for disabled ProductVariants', async () => {
  410. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  411. UPDATE_PRODUCT_VARIANTS,
  412. {
  413. input: [{ id: 'T_3', enabled: false }],
  414. },
  415. );
  416. await awaitRunningJobs(adminClient);
  417. const result = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  418. SEARCH_PRODUCTS_SHOP,
  419. {
  420. input: {
  421. groupByProduct: false,
  422. take: 3,
  423. },
  424. },
  425. );
  426. expect(result.search.items.map(i => i.productVariantId)).toEqual(['T_1', 'T_2', 'T_4']);
  427. });
  428. it('encodes collectionIds', async () => {
  429. const result = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  430. SEARCH_PRODUCTS_SHOP,
  431. {
  432. input: {
  433. groupByProduct: false,
  434. term: 'cactus',
  435. take: 1,
  436. },
  437. },
  438. );
  439. expect(result.search.items[0].collectionIds).toEqual(['T_2']);
  440. });
  441. });
  442. describe('admin api', () => {
  443. it('group by product', () => testGroupByProduct(adminClient));
  444. it('no grouping', () => testNoGrouping(adminClient));
  445. it('matches search term', () => testMatchSearchTerm(adminClient));
  446. it('matches by facetId with AND operator', () => testMatchFacetIdsAnd(adminClient));
  447. it('matches by facetId with OR operator', () => testMatchFacetIdsOr(adminClient));
  448. it('matches by collectionId', () => testMatchCollectionId(adminClient));
  449. it('matches by collectionSlug', () => testMatchCollectionSlug(adminClient));
  450. it('single prices', () => testSinglePrices(adminClient));
  451. it('price ranges', () => testPriceRanges(adminClient));
  452. it('sort name with grouping', () => testSortingWithGrouping(adminClient, 'name'));
  453. it('sort price with grouping', () => testSortingWithGrouping(adminClient, 'price'));
  454. it('sort name without grouping', () => testSortingNoGrouping(adminClient, 'name'));
  455. it('sort price without grouping', () => testSortingNoGrouping(adminClient, 'price'));
  456. describe('updating the index', () => {
  457. it('updates index when ProductVariants are changed', async () => {
  458. await awaitRunningJobs(adminClient);
  459. const { search } = await doAdminSearchQuery({ term: 'drive', groupByProduct: false });
  460. expect(search.items.map(i => i.sku)).toEqual([
  461. 'IHD455T1',
  462. 'IHD455T2',
  463. 'IHD455T3',
  464. 'IHD455T4',
  465. 'IHD455T6',
  466. ]);
  467. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  468. UPDATE_PRODUCT_VARIANTS,
  469. {
  470. input: search.items.map(i => ({
  471. id: i.productVariantId,
  472. sku: i.sku + '_updated',
  473. })),
  474. },
  475. );
  476. await awaitRunningJobs(adminClient);
  477. const { search: search2 } = await doAdminSearchQuery({
  478. term: 'drive',
  479. groupByProduct: false,
  480. });
  481. expect(search2.items.map(i => i.sku)).toEqual([
  482. 'IHD455T1_updated',
  483. 'IHD455T2_updated',
  484. 'IHD455T3_updated',
  485. 'IHD455T4_updated',
  486. 'IHD455T6_updated',
  487. ]);
  488. });
  489. it('updates index when ProductVariants are deleted', async () => {
  490. await awaitRunningJobs(adminClient);
  491. const { search } = await doAdminSearchQuery({ term: 'drive', groupByProduct: false });
  492. await adminClient.query<DeleteProductVariant.Mutation, DeleteProductVariant.Variables>(
  493. DELETE_PRODUCT_VARIANT,
  494. {
  495. id: search.items[0].productVariantId,
  496. },
  497. );
  498. await awaitRunningJobs(adminClient);
  499. const { search: search2 } = await doAdminSearchQuery({
  500. term: 'drive',
  501. groupByProduct: false,
  502. });
  503. expect(search2.items.map(i => i.sku)).toEqual([
  504. 'IHD455T2_updated',
  505. 'IHD455T3_updated',
  506. 'IHD455T4_updated',
  507. 'IHD455T6_updated',
  508. ]);
  509. });
  510. it('updates index when a Product is changed', async () => {
  511. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  512. input: {
  513. id: 'T_1',
  514. facetValueIds: [],
  515. },
  516. });
  517. await awaitRunningJobs(adminClient);
  518. const result = await doAdminSearchQuery({ facetValueIds: ['T_2'], groupByProduct: true });
  519. expect(result.search.items.map(i => i.productName)).toEqual([
  520. 'Curvy Monitor',
  521. 'Gaming PC',
  522. 'Hard Drive',
  523. 'Clacky Keyboard',
  524. 'USB Cable',
  525. ]);
  526. });
  527. it('updates index when a Product is deleted', async () => {
  528. const { search } = await doAdminSearchQuery({ facetValueIds: ['T_2'], groupByProduct: true });
  529. expect(search.items.map(i => i.productId)).toEqual(['T_2', 'T_3', 'T_4', 'T_5', 'T_6']);
  530. await adminClient.query<DeleteProduct.Mutation, DeleteProduct.Variables>(DELETE_PRODUCT, {
  531. id: 'T_5',
  532. });
  533. await awaitRunningJobs(adminClient);
  534. const { search: search2 } = await doAdminSearchQuery({
  535. facetValueIds: ['T_2'],
  536. groupByProduct: true,
  537. });
  538. expect(search2.items.map(i => i.productId)).toEqual(['T_2', 'T_3', 'T_4', 'T_6']);
  539. });
  540. it('updates index when a Collection is changed', async () => {
  541. await adminClient.query<UpdateCollection.Mutation, UpdateCollection.Variables>(
  542. UPDATE_COLLECTION,
  543. {
  544. input: {
  545. id: 'T_2',
  546. filters: [
  547. {
  548. code: facetValueCollectionFilter.code,
  549. arguments: [
  550. {
  551. name: 'facetValueIds',
  552. value: `["T_4"]`,
  553. },
  554. {
  555. name: 'containsAny',
  556. value: `false`,
  557. },
  558. ],
  559. },
  560. ],
  561. },
  562. },
  563. );
  564. await awaitRunningJobs(adminClient);
  565. // add an additional check for the collection filters to update
  566. await awaitRunningJobs(adminClient);
  567. const result1 = await doAdminSearchQuery({ collectionId: 'T_2', groupByProduct: true });
  568. expect(result1.search.items.map(i => i.productName)).toEqual([
  569. 'Road Bike',
  570. 'Skipping Rope',
  571. 'Boxing Gloves',
  572. 'Tent',
  573. 'Cruiser Skateboard',
  574. 'Football',
  575. 'Running Shoe',
  576. ]);
  577. const result2 = await doAdminSearchQuery({ collectionSlug: 'plants', groupByProduct: true });
  578. expect(result2.search.items.map(i => i.productName)).toEqual([
  579. 'Road Bike',
  580. 'Skipping Rope',
  581. 'Boxing Gloves',
  582. 'Tent',
  583. 'Cruiser Skateboard',
  584. 'Football',
  585. 'Running Shoe',
  586. ]);
  587. });
  588. it('updates index when a Collection created', async () => {
  589. const { createCollection } = await adminClient.query<
  590. CreateCollection.Mutation,
  591. CreateCollection.Variables
  592. >(CREATE_COLLECTION, {
  593. input: {
  594. translations: [
  595. {
  596. languageCode: LanguageCode.en,
  597. name: 'Photo',
  598. description: '',
  599. slug: 'photo',
  600. },
  601. ],
  602. filters: [
  603. {
  604. code: facetValueCollectionFilter.code,
  605. arguments: [
  606. {
  607. name: 'facetValueIds',
  608. value: `["T_3"]`,
  609. },
  610. {
  611. name: 'containsAny',
  612. value: `false`,
  613. },
  614. ],
  615. },
  616. ],
  617. },
  618. });
  619. await awaitRunningJobs(adminClient);
  620. // add an additional check for the collection filters to update
  621. await awaitRunningJobs(adminClient);
  622. const result = await doAdminSearchQuery({
  623. collectionId: createCollection.id,
  624. groupByProduct: true,
  625. });
  626. expect(result.search.items.map(i => i.productName)).toEqual([
  627. 'Instant Camera',
  628. 'Camera Lens',
  629. 'Tripod',
  630. 'Slr Camera',
  631. ]);
  632. });
  633. it('updates index when a taxRate is changed', async () => {
  634. await adminClient.query<UpdateTaxRate.Mutation, UpdateTaxRate.Variables>(UPDATE_TAX_RATE, {
  635. input: {
  636. // Default Channel's defaultTaxZone is Europe (id 2) and the id of the standard TaxRate
  637. // to Europe is 2.
  638. id: 'T_2',
  639. value: 50,
  640. },
  641. });
  642. await awaitRunningJobs(adminClient);
  643. const result = await adminClient.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
  644. SEARCH_GET_PRICES,
  645. {
  646. input: {
  647. groupByProduct: true,
  648. term: 'laptop',
  649. } as SearchInput,
  650. },
  651. );
  652. expect(result.search.items).toEqual([
  653. {
  654. price: { min: 129900, max: 229900 },
  655. priceWithTax: { min: 194850, max: 344850 },
  656. },
  657. ]);
  658. });
  659. describe('asset changes', () => {
  660. function searchForLaptop() {
  661. return adminClient.query<SearchGetAssets.Query, SearchGetAssets.Variables>(
  662. SEARCH_GET_ASSETS,
  663. {
  664. input: {
  665. term: 'laptop',
  666. take: 1,
  667. },
  668. },
  669. );
  670. }
  671. it('updates index when asset focalPoint is changed', async () => {
  672. const { search: search1 } = await searchForLaptop();
  673. expect(search1.items[0].productAsset!.id).toBe('T_1');
  674. expect(search1.items[0].productAsset!.focalPoint).toBeNull();
  675. await adminClient.query<UpdateAsset.Mutation, UpdateAsset.Variables>(UPDATE_ASSET, {
  676. input: {
  677. id: 'T_1',
  678. focalPoint: {
  679. x: 0.42,
  680. y: 0.42,
  681. },
  682. },
  683. });
  684. await awaitRunningJobs(adminClient);
  685. const { search: search2 } = await searchForLaptop();
  686. expect(search2.items[0].productAsset!.id).toBe('T_1');
  687. expect(search2.items[0].productAsset!.focalPoint).toEqual({ x: 0.42, y: 0.42 });
  688. });
  689. it('updates index when asset deleted', async () => {
  690. const { search: search1 } = await searchForLaptop();
  691. const assetId = search1.items[0].productAsset?.id;
  692. expect(assetId).toBeTruthy();
  693. await adminClient.query<DeleteAsset.Mutation, DeleteAsset.Variables>(DELETE_ASSET, {
  694. id: assetId!,
  695. force: true,
  696. });
  697. await awaitRunningJobs(adminClient);
  698. const { search: search2 } = await searchForLaptop();
  699. expect(search2.items[0].productAsset).toBeNull();
  700. });
  701. });
  702. it('does not include deleted ProductVariants in index', async () => {
  703. const { search: s1 } = await doAdminSearchQuery({
  704. term: 'hard drive',
  705. groupByProduct: false,
  706. });
  707. const { deleteProductVariant } = await adminClient.query<
  708. DeleteProductVariant.Mutation,
  709. DeleteProductVariant.Variables
  710. >(DELETE_PRODUCT_VARIANT, { id: s1.items[0].productVariantId });
  711. await awaitRunningJobs(adminClient);
  712. const { search } = await adminClient.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
  713. SEARCH_GET_PRICES,
  714. { input: { term: 'hard drive', groupByProduct: true } },
  715. );
  716. expect(search.items[0].price).toEqual({
  717. min: 7896,
  718. max: 13435,
  719. });
  720. });
  721. it('returns enabled field when not grouped', async () => {
  722. const result = await doAdminSearchQuery({ groupByProduct: false, take: 3 });
  723. expect(result.search.items.map(pick(['productVariantId', 'enabled']))).toEqual([
  724. { productVariantId: 'T_1', enabled: true },
  725. { productVariantId: 'T_2', enabled: true },
  726. { productVariantId: 'T_3', enabled: false },
  727. ]);
  728. });
  729. it('when grouped, enabled is true if at least one variant is enabled', async () => {
  730. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  731. UPDATE_PRODUCT_VARIANTS,
  732. {
  733. input: [
  734. { id: 'T_1', enabled: false },
  735. { id: 'T_2', enabled: false },
  736. ],
  737. },
  738. );
  739. await awaitRunningJobs(adminClient);
  740. const result = await doAdminSearchQuery({ groupByProduct: true, take: 3 });
  741. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  742. { productId: 'T_1', enabled: true },
  743. { productId: 'T_2', enabled: true },
  744. { productId: 'T_3', enabled: true },
  745. ]);
  746. });
  747. it('when grouped, enabled is false if all variants are disabled', async () => {
  748. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  749. UPDATE_PRODUCT_VARIANTS,
  750. {
  751. input: [{ id: 'T_4', enabled: false }],
  752. },
  753. );
  754. await awaitRunningJobs(adminClient);
  755. const result = await doAdminSearchQuery({ groupByProduct: true, take: 3 });
  756. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  757. { productId: 'T_1', enabled: false },
  758. { productId: 'T_2', enabled: true },
  759. { productId: 'T_3', enabled: true },
  760. ]);
  761. });
  762. it('when grouped, enabled is false if product is disabled', async () => {
  763. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  764. input: {
  765. id: 'T_3',
  766. enabled: false,
  767. },
  768. });
  769. await awaitRunningJobs(adminClient);
  770. const result = await doAdminSearchQuery({ groupByProduct: true, take: 3 });
  771. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  772. { productId: 'T_1', enabled: false },
  773. { productId: 'T_2', enabled: true },
  774. { productId: 'T_3', enabled: false },
  775. ]);
  776. });
  777. // https://github.com/vendure-ecommerce/vendure/issues/295
  778. it('enabled status survives reindex', async () => {
  779. await adminClient.query<Reindex.Mutation>(REINDEX);
  780. await awaitRunningJobs(adminClient);
  781. const result = await doAdminSearchQuery({ groupByProduct: true, take: 3 });
  782. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  783. { productId: 'T_1', enabled: false },
  784. { productId: 'T_2', enabled: true },
  785. { productId: 'T_3', enabled: false },
  786. ]);
  787. });
  788. });
  789. // https://github.com/vendure-ecommerce/vendure/issues/609
  790. describe('Synthetic index items', () => {
  791. let createdProductId: string;
  792. it('creates synthetic index item for Product with no variants', async () => {
  793. const { createProduct } = await adminClient.query<
  794. CreateProduct.Mutation,
  795. CreateProduct.Variables
  796. >(CREATE_PRODUCT, {
  797. input: {
  798. facetValueIds: ['T_1'],
  799. translations: [
  800. {
  801. languageCode: LanguageCode.en,
  802. name: 'Strawberry cheesecake',
  803. slug: 'strawberry-cheesecake',
  804. description: 'A yummy dessert',
  805. },
  806. ],
  807. },
  808. });
  809. await awaitRunningJobs(adminClient);
  810. const result = await doAdminSearchQuery({ groupByProduct: true, term: 'strawberry' });
  811. expect(
  812. result.search.items.map(
  813. pick([
  814. 'productId',
  815. 'enabled',
  816. 'productName',
  817. 'productVariantName',
  818. 'slug',
  819. 'description',
  820. ]),
  821. ),
  822. ).toEqual([
  823. {
  824. productId: createProduct.id,
  825. enabled: false,
  826. productName: 'Strawberry cheesecake',
  827. productVariantName: 'Strawberry cheesecake',
  828. slug: 'strawberry-cheesecake',
  829. description: 'A yummy dessert',
  830. },
  831. ]);
  832. createdProductId = createProduct.id;
  833. });
  834. it('removes synthetic index item once a variant is created', async () => {
  835. const { createProductVariants } = await adminClient.query<
  836. CreateProductVariants.Mutation,
  837. CreateProductVariants.Variables
  838. >(CREATE_PRODUCT_VARIANTS, {
  839. input: [
  840. {
  841. productId: createdProductId,
  842. sku: 'SC01',
  843. price: 1399,
  844. translations: [
  845. { languageCode: LanguageCode.en, name: 'Strawberry Cheesecake Pie' },
  846. ],
  847. },
  848. ],
  849. });
  850. await awaitRunningJobs(adminClient);
  851. const result = await doAdminSearchQuery({ groupByProduct: false, term: 'strawberry' });
  852. expect(result.search.items.map(pick(['productVariantName']))).toEqual([
  853. { productVariantName: 'Strawberry Cheesecake Pie' },
  854. ]);
  855. });
  856. });
  857. describe('channel handling', () => {
  858. const SECOND_CHANNEL_TOKEN = 'second-channel-token';
  859. let secondChannel: ChannelFragment;
  860. beforeAll(async () => {
  861. const { createChannel } = await adminClient.query<
  862. CreateChannel.Mutation,
  863. CreateChannel.Variables
  864. >(CREATE_CHANNEL, {
  865. input: {
  866. code: 'second-channel',
  867. token: SECOND_CHANNEL_TOKEN,
  868. defaultLanguageCode: LanguageCode.en,
  869. currencyCode: CurrencyCode.GBP,
  870. pricesIncludeTax: true,
  871. defaultTaxZoneId: 'T_1',
  872. defaultShippingZoneId: 'T_1',
  873. },
  874. });
  875. secondChannel = createChannel as ChannelFragment;
  876. });
  877. it('adding product to channel', async () => {
  878. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  879. await adminClient.query<AssignProductsToChannel.Mutation, AssignProductsToChannel.Variables>(
  880. ASSIGN_PRODUCT_TO_CHANNEL,
  881. {
  882. input: { channelId: secondChannel.id, productIds: ['T_1', 'T_2'] },
  883. },
  884. );
  885. await awaitRunningJobs(adminClient);
  886. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  887. const { search } = await doAdminSearchQuery({ groupByProduct: true });
  888. expect(search.items.map(i => i.productId)).toEqual(['T_1', 'T_2']);
  889. }, 10000);
  890. it('removing product from channel', async () => {
  891. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  892. const { removeProductsFromChannel } = await adminClient.query<
  893. RemoveProductsFromChannel.Mutation,
  894. RemoveProductsFromChannel.Variables
  895. >(REMOVE_PRODUCT_FROM_CHANNEL, {
  896. input: {
  897. productIds: ['T_2'],
  898. channelId: secondChannel.id,
  899. },
  900. });
  901. await awaitRunningJobs(adminClient);
  902. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  903. const { search } = await doAdminSearchQuery({ groupByProduct: true });
  904. expect(search.items.map(i => i.productId)).toEqual(['T_1']);
  905. }, 10000);
  906. it('adding product variant to channel', async () => {
  907. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  908. await adminClient.query<
  909. AssignProductVariantsToChannel.Mutation,
  910. AssignProductVariantsToChannel.Variables
  911. >(ASSIGN_PRODUCTVARIANT_TO_CHANNEL, {
  912. input: { channelId: secondChannel.id, productVariantIds: ['T_10', 'T_15'] },
  913. });
  914. await awaitRunningJobs(adminClient);
  915. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  916. const { search: searchGrouped } = await doAdminSearchQuery({ groupByProduct: true });
  917. expect(searchGrouped.items.map(i => i.productId)).toEqual(['T_1', 'T_3', 'T_4']);
  918. const { search: searchUngrouped } = await doAdminSearchQuery({ groupByProduct: false });
  919. expect(searchUngrouped.items.map(i => i.productVariantId)).toEqual([
  920. 'T_1',
  921. 'T_2',
  922. 'T_3',
  923. 'T_4',
  924. 'T_10',
  925. 'T_15',
  926. ]);
  927. }, 10000);
  928. it('removing product variant from channel', async () => {
  929. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  930. await adminClient.query<
  931. RemoveProductVariantsFromChannel.Mutation,
  932. RemoveProductVariantsFromChannel.Variables
  933. >(REMOVE_PRODUCTVARIANT_FROM_CHANNEL, {
  934. input: { channelId: secondChannel.id, productVariantIds: ['T_1', 'T_15'] },
  935. });
  936. await awaitRunningJobs(adminClient);
  937. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  938. const { search: searchGrouped } = await doAdminSearchQuery({ groupByProduct: true });
  939. expect(searchGrouped.items.map(i => i.productId)).toEqual(['T_1', 'T_3']);
  940. const { search: searchUngrouped } = await doAdminSearchQuery({ groupByProduct: false });
  941. expect(searchUngrouped.items.map(i => i.productVariantId)).toEqual([
  942. 'T_2',
  943. 'T_3',
  944. 'T_4',
  945. 'T_10',
  946. ]);
  947. }, 10000);
  948. it('updating product affects current channel', async () => {
  949. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  950. const { updateProduct } = await adminClient.query<
  951. UpdateProduct.Mutation,
  952. UpdateProduct.Variables
  953. >(UPDATE_PRODUCT, {
  954. input: {
  955. id: 'T_3',
  956. enabled: true,
  957. translations: [{ languageCode: LanguageCode.en, name: 'xyz' }],
  958. },
  959. });
  960. await awaitRunningJobs(adminClient);
  961. const { search: searchGrouped } = await doAdminSearchQuery({
  962. groupByProduct: true,
  963. term: 'xyz',
  964. });
  965. expect(searchGrouped.items.map(i => i.productName)).toEqual(['xyz']);
  966. });
  967. it('updating product affects other channels', async () => {
  968. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  969. const { search: searchGrouped } = await doAdminSearchQuery({
  970. groupByProduct: true,
  971. term: 'xyz',
  972. });
  973. expect(searchGrouped.items.map(i => i.productName)).toEqual(['xyz']);
  974. });
  975. });
  976. describe('multiple language handling', () => {
  977. function searchInLanguage(languageCode: LanguageCode) {
  978. return adminClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  979. SEARCH_PRODUCTS,
  980. {
  981. input: {
  982. take: 1,
  983. },
  984. },
  985. {
  986. languageCode,
  987. },
  988. );
  989. }
  990. beforeAll(async () => {
  991. const { updateProduct } = await adminClient.query<
  992. UpdateProduct.Mutation,
  993. UpdateProduct.Variables
  994. >(UPDATE_PRODUCT, {
  995. input: {
  996. id: 'T_1',
  997. translations: [
  998. {
  999. languageCode: LanguageCode.de,
  1000. name: 'laptop name de',
  1001. slug: 'laptop-slug-de',
  1002. description: 'laptop description de',
  1003. },
  1004. {
  1005. languageCode: LanguageCode.zh,
  1006. name: 'laptop name zh',
  1007. slug: 'laptop-slug-zh',
  1008. description: 'laptop description zh',
  1009. },
  1010. ],
  1011. },
  1012. });
  1013. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1014. UPDATE_PRODUCT_VARIANTS,
  1015. {
  1016. input: [
  1017. {
  1018. id: updateProduct.variants[0].id,
  1019. translations: [
  1020. {
  1021. languageCode: LanguageCode.fr,
  1022. name: 'laptop variant fr',
  1023. },
  1024. ],
  1025. },
  1026. ],
  1027. },
  1028. );
  1029. await awaitRunningJobs(adminClient);
  1030. });
  1031. it('indexes product-level languages', async () => {
  1032. const { search: search1 } = await searchInLanguage(LanguageCode.de);
  1033. expect(search1.items[0].productName).toBe('laptop name de');
  1034. expect(search1.items[0].slug).toBe('laptop-slug-de');
  1035. expect(search1.items[0].description).toBe('laptop description de');
  1036. const { search: search2 } = await searchInLanguage(LanguageCode.zh);
  1037. expect(search2.items[0].productName).toBe('laptop name zh');
  1038. expect(search2.items[0].slug).toBe('laptop-slug-zh');
  1039. expect(search2.items[0].description).toBe('laptop description zh');
  1040. });
  1041. it('indexes product variant-level languages', async () => {
  1042. const { search: search1 } = await searchInLanguage(LanguageCode.fr);
  1043. expect(search1.items[0].productName).toBe('Laptop');
  1044. expect(search1.items[0].productVariantName).toBe('laptop variant fr');
  1045. });
  1046. });
  1047. });
  1048. });
  1049. export const REINDEX = gql`
  1050. mutation Reindex {
  1051. reindex {
  1052. id
  1053. }
  1054. }
  1055. `;
  1056. export const SEARCH_PRODUCTS = gql`
  1057. query SearchProductsAdmin($input: SearchInput!) {
  1058. search(input: $input) {
  1059. totalItems
  1060. items {
  1061. enabled
  1062. productId
  1063. productName
  1064. slug
  1065. description
  1066. productPreview
  1067. productVariantId
  1068. productVariantName
  1069. productVariantPreview
  1070. sku
  1071. }
  1072. }
  1073. }
  1074. `;
  1075. export const SEARCH_GET_FACET_VALUES = gql`
  1076. query SearchFacetValues($input: SearchInput!) {
  1077. search(input: $input) {
  1078. totalItems
  1079. facetValues {
  1080. count
  1081. facetValue {
  1082. id
  1083. name
  1084. }
  1085. }
  1086. }
  1087. }
  1088. `;
  1089. export const SEARCH_GET_ASSETS = gql`
  1090. query SearchGetAssets($input: SearchInput!) {
  1091. search(input: $input) {
  1092. totalItems
  1093. items {
  1094. productId
  1095. productName
  1096. productVariantName
  1097. productAsset {
  1098. id
  1099. preview
  1100. focalPoint {
  1101. x
  1102. y
  1103. }
  1104. }
  1105. productVariantAsset {
  1106. id
  1107. preview
  1108. focalPoint {
  1109. x
  1110. y
  1111. }
  1112. }
  1113. }
  1114. }
  1115. }
  1116. `;
  1117. export const SEARCH_GET_PRICES = gql`
  1118. query SearchGetPrices($input: SearchInput!) {
  1119. search(input: $input) {
  1120. items {
  1121. price {
  1122. ... on PriceRange {
  1123. min
  1124. max
  1125. }
  1126. ... on SinglePrice {
  1127. value
  1128. }
  1129. }
  1130. priceWithTax {
  1131. ... on PriceRange {
  1132. min
  1133. max
  1134. }
  1135. ... on SinglePrice {
  1136. value
  1137. }
  1138. }
  1139. }
  1140. }
  1141. }
  1142. `;