elasticsearch-plugin.e2e-spec.ts 60 KB

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