elasticsearch-plugin.e2e-spec.ts 64 KB

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