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

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