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

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