elasticsearch-plugin.e2e-spec.ts 64 KB

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