elasticsearch-plugin.e2e-spec.ts 63 KB

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