elasticsearch-plugin.e2e-spec.ts 60 KB

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