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

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