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

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