elasticsearch-plugin.e2e-spec.ts 64 KB

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