elasticsearch-plugin.e2e-spec.ts 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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. expect(result.search.collections).toEqual([
  306. { collection: { id: 'T_2', name: 'Plants' }, count: 4 },
  307. { collection: { id: 'T_3', name: 'Electronics' }, count: 21 },
  308. ]);
  309. });
  310. it('returns correct collections when grouped by product', async () => {
  311. const result = await shopClient.query(searchGetCollectionsDocument, {
  312. input: {
  313. groupByProduct: true,
  314. },
  315. });
  316. expect(result.search.collections).toEqual([
  317. { collection: { id: 'T_2', name: 'Plants' }, count: 4 },
  318. { collection: { id: 'T_3', name: 'Electronics' }, count: 10 },
  319. ]);
  320. });
  321. it('encodes the productId and productVariantId', async () => {
  322. const result = await shopClient.query(searchProductsShopDocument, {
  323. input: {
  324. term: 'Laptop 13 inch 8GB',
  325. groupByProduct: false,
  326. take: 1,
  327. },
  328. });
  329. expect(pick(result.search.items[0], ['productId', 'productVariantId'])).toEqual({
  330. productId: 'T_1',
  331. productVariantId: 'T_1',
  332. });
  333. });
  334. it('omits results for disabled ProductVariants', async () => {
  335. await adminClient.query(updateProductVariantsDocument, {
  336. input: [{ id: 'T_3', enabled: false }],
  337. });
  338. await awaitRunningJobs(adminClient);
  339. const result = await shopClient.query(searchProductsShopDocument, {
  340. input: {
  341. groupByProduct: false,
  342. take: 100,
  343. },
  344. });
  345. expect(result.search.items.map(i => i.productVariantId).includes('T_3')).toBe(false);
  346. });
  347. it('encodes collectionIds', async () => {
  348. const result = await shopClient.query(searchProductsShopDocument, {
  349. input: {
  350. groupByProduct: false,
  351. term: 'cactus',
  352. take: 1,
  353. },
  354. });
  355. expect(result.search.items[0].collectionIds).toEqual(['T_2']);
  356. });
  357. it('inStock is false and not grouped by product', async () => {
  358. const result = await shopClient.query(searchProductsShopDocument, {
  359. input: {
  360. groupByProduct: false,
  361. inStock: false,
  362. },
  363. });
  364. expect(result.search.totalItems).toBe(3);
  365. });
  366. it('inStock is false and grouped by product', async () => {
  367. const result = await shopClient.query(searchProductsShopDocument, {
  368. input: {
  369. groupByProduct: true,
  370. inStock: false,
  371. },
  372. });
  373. expect(result.search.totalItems).toBe(2);
  374. });
  375. it('inStock is true and not grouped by product', async () => {
  376. const result = await shopClient.query(searchProductsShopDocument, {
  377. input: {
  378. groupByProduct: false,
  379. inStock: true,
  380. },
  381. });
  382. expect(result.search.totalItems).toBe(31);
  383. });
  384. it('inStock is true and grouped by product', async () => {
  385. const result = await shopClient.query(searchProductsShopDocument, {
  386. input: {
  387. groupByProduct: true,
  388. inStock: true,
  389. },
  390. });
  391. expect(result.search.totalItems).toBe(19);
  392. });
  393. it('inStock is undefined and not grouped by product', async () => {
  394. const result = await shopClient.query(searchProductsShopDocument, {
  395. input: {
  396. groupByProduct: false,
  397. inStock: undefined,
  398. },
  399. });
  400. expect(result.search.totalItems).toBe(34);
  401. });
  402. it('inStock is undefined and grouped by product', async () => {
  403. const result = await shopClient.query(searchProductsShopDocument, {
  404. input: {
  405. groupByProduct: true,
  406. inStock: undefined,
  407. },
  408. });
  409. expect(result.search.totalItems).toBe(21);
  410. });
  411. });
  412. describe('admin api', () => {
  413. it('group by product', () => testGroupByProduct(adminClient));
  414. it('group by SKU', () => testGroupBySKU(adminClient));
  415. it('no grouping', () => testNoGrouping(adminClient));
  416. it('matches search term', () => testMatchSearchTerm(adminClient));
  417. it('matches by facetValueId with AND operator', () => testMatchFacetIdsAnd(adminClient));
  418. it('matches by facetValueId with OR operator', () => testMatchFacetIdsOr(adminClient));
  419. it('matches by FacetValueFilters AND', () => testMatchFacetValueFiltersAnd(shopClient));
  420. it('matches by FacetValueFilters OR', () => testMatchFacetValueFiltersOr(shopClient));
  421. it('matches by FacetValueFilters OR and AND', () => testMatchFacetValueFiltersOrWithAnd(shopClient));
  422. it('matches by FacetValueFilters with facetId OR operator', () =>
  423. testMatchFacetValueFiltersWithFacetIdsOr(shopClient));
  424. it('matches by FacetValueFilters with facetId AND operator', () =>
  425. testMatchFacetValueFiltersWithFacetIdsAnd(shopClient));
  426. it('matches by collectionId', () => testMatchCollectionId(adminClient));
  427. it('matches by collectionSlug', () => testMatchCollectionSlug(adminClient));
  428. it('matches by multiple collectionIds', () => testMatchCollectionIds(adminClient));
  429. it('matches by multiple collectionSlugs', () => testMatchCollectionSlugs(adminClient));
  430. it('handles collectionIds edge cases', () => testCollectionIdsEdgeCases(adminClient));
  431. it('handles collectionSlugs edge cases', () => testCollectionSlugsEdgeCases(adminClient));
  432. it('single prices', () => testSinglePrices(adminClient));
  433. it('price ranges', () => testPriceRanges(adminClient));
  434. describe('updating the index', () => {
  435. it('updates index when ProductVariants are changed', async () => {
  436. await awaitRunningJobs(adminClient);
  437. const { search } = await doAdminSearchQuery(adminClient, {
  438. term: 'drive',
  439. groupByProduct: false,
  440. });
  441. expect(search.items.map(i => i.sku)).toEqual([
  442. 'IHD455T1',
  443. 'IHD455T2',
  444. 'IHD455T3',
  445. 'IHD455T4',
  446. 'IHD455T6',
  447. ]);
  448. await adminClient.query(updateProductVariantsDocument, {
  449. input: search.items.map(i => ({
  450. id: i.productVariantId,
  451. sku: i.sku + '_updated',
  452. })),
  453. });
  454. await awaitRunningJobs(adminClient);
  455. const { search: search2 } = await doAdminSearchQuery(adminClient, {
  456. term: 'drive',
  457. groupByProduct: false,
  458. });
  459. expect(search2.items.map(i => i.sku)).toEqual([
  460. 'IHD455T1_updated',
  461. 'IHD455T2_updated',
  462. 'IHD455T3_updated',
  463. 'IHD455T4_updated',
  464. 'IHD455T6_updated',
  465. ]);
  466. });
  467. it('updates index when ProductVariants are deleted', async () => {
  468. await awaitRunningJobs(adminClient);
  469. const { search } = await doAdminSearchQuery(adminClient, {
  470. term: 'drive',
  471. groupByProduct: false,
  472. });
  473. await adminClient.query(deleteProductVariantDocument, {
  474. id: search.items[0].productVariantId,
  475. });
  476. await awaitRunningJobs(adminClient);
  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).sort()).toEqual([
  483. 'IHD455T2_updated',
  484. 'IHD455T3_updated',
  485. 'IHD455T4_updated',
  486. 'IHD455T6_updated',
  487. ]);
  488. });
  489. it('updates index when a Product is changed', async () => {
  490. await adminClient.query(updateProductDocument, {
  491. input: {
  492. id: 'T_1',
  493. facetValueIds: [],
  494. },
  495. });
  496. await awaitRunningJobs(adminClient);
  497. const result = await doAdminSearchQuery(adminClient, {
  498. facetValueIds: ['T_2'],
  499. groupByProduct: true,
  500. });
  501. expect(result.search.items.map(i => i.productName).sort()).toEqual([
  502. 'Clacky Keyboard',
  503. 'Curvy Monitor',
  504. 'Gaming PC',
  505. 'Hard Drive',
  506. 'USB Cable',
  507. ]);
  508. });
  509. it('updates index when a Product is deleted', async () => {
  510. const { search } = await doAdminSearchQuery(adminClient, {
  511. facetValueIds: ['T_2'],
  512. groupByProduct: true,
  513. });
  514. expect(search.items.map(i => i.productId).sort()).toEqual([
  515. 'T_2',
  516. 'T_3',
  517. 'T_4',
  518. 'T_5',
  519. 'T_6',
  520. ]);
  521. await adminClient.query(deleteProductDocument, {
  522. id: 'T_5',
  523. });
  524. await awaitRunningJobs(adminClient);
  525. const { search: search2 } = await doAdminSearchQuery(adminClient, {
  526. facetValueIds: ['T_2'],
  527. groupByProduct: true,
  528. });
  529. expect(search2.items.map(i => i.productId).sort()).toEqual(['T_2', 'T_3', 'T_4', 'T_6']);
  530. });
  531. it('updates index when a Collection is changed', async () => {
  532. await adminClient.query(updateCollectionDocument, {
  533. input: {
  534. id: 'T_2',
  535. filters: [
  536. {
  537. code: facetValueCollectionFilter.code,
  538. arguments: [
  539. {
  540. name: 'facetValueIds',
  541. value: '["T_4"]',
  542. },
  543. {
  544. name: 'containsAny',
  545. value: 'false',
  546. },
  547. ],
  548. },
  549. ],
  550. },
  551. });
  552. await awaitRunningJobs(adminClient);
  553. // add an additional check for the collection filters to update
  554. await awaitRunningJobs(adminClient);
  555. const result1 = await doAdminSearchQuery(adminClient, {
  556. collectionId: 'T_2',
  557. groupByProduct: true,
  558. });
  559. expect(result1.search.items.map(i => i.productName).sort()).toEqual([
  560. 'Boxing Gloves',
  561. 'Cruiser Skateboard',
  562. 'Football',
  563. 'Road Bike',
  564. 'Running Shoe',
  565. 'Skipping Rope',
  566. 'Tent',
  567. ]);
  568. const result2 = await doAdminSearchQuery(adminClient, {
  569. collectionSlug: 'plants',
  570. groupByProduct: true,
  571. });
  572. expect(result2.search.items.map(i => i.productName).sort()).toEqual([
  573. 'Boxing Gloves',
  574. 'Cruiser Skateboard',
  575. 'Football',
  576. 'Road Bike',
  577. 'Running Shoe',
  578. 'Skipping Rope',
  579. 'Tent',
  580. ]);
  581. });
  582. it('updates index when a Collection created', async () => {
  583. const { createCollection } = await adminClient.query(createCollectionDocument, {
  584. input: {
  585. translations: [
  586. {
  587. languageCode: LanguageCode.en,
  588. name: 'Photo',
  589. description: '',
  590. slug: 'photo',
  591. },
  592. ],
  593. filters: [
  594. {
  595. code: facetValueCollectionFilter.code,
  596. arguments: [
  597. {
  598. name: 'facetValueIds',
  599. value: '["T_3"]',
  600. },
  601. {
  602. name: 'containsAny',
  603. value: 'false',
  604. },
  605. ],
  606. },
  607. ],
  608. },
  609. });
  610. await awaitRunningJobs(adminClient);
  611. // add an additional check for the collection filters to update
  612. await awaitRunningJobs(adminClient);
  613. const result = await doAdminSearchQuery(adminClient, {
  614. collectionId: createCollection.id,
  615. groupByProduct: true,
  616. });
  617. expect(result.search.items.map(i => i.productName).sort()).toEqual([
  618. 'Camera Lens',
  619. 'Instant Camera',
  620. 'SLR Camera',
  621. 'Tripod',
  622. ]);
  623. });
  624. it('updates index when a taxRate is changed', async () => {
  625. await adminClient.query(updateTaxRateDocument, {
  626. input: {
  627. // Default Channel's defaultTaxZone is Europe (id 2) and the id of the standard TaxRate
  628. // to Europe is 2.
  629. id: 'T_2',
  630. value: 50,
  631. },
  632. });
  633. await awaitRunningJobs(adminClient);
  634. const result = await adminClient.query(searchGetPricesDocument, {
  635. input: {
  636. groupByProduct: true,
  637. term: 'laptop',
  638. },
  639. });
  640. expect(result.search.items).toEqual([
  641. {
  642. price: { min: 129900, max: 229900 },
  643. priceWithTax: { min: 194850, max: 344850 },
  644. },
  645. ]);
  646. });
  647. describe('asset changes', () => {
  648. function searchForLaptop() {
  649. return doAdminSearchQuery(adminClient, {
  650. term: 'laptop',
  651. groupByProduct: true,
  652. take: 1,
  653. sort: {
  654. name: SortOrder.ASC,
  655. },
  656. });
  657. }
  658. it('updates index when asset focalPoint is changed', async () => {
  659. const { search: search1 } = await searchForLaptop();
  660. expect(search1.items[0].productAsset!.id).toBe('T_1');
  661. expect(search1.items[0].productAsset!.focalPoint).toBeNull();
  662. await adminClient.query(updateAssetDocument, {
  663. input: {
  664. id: 'T_1',
  665. focalPoint: {
  666. x: 0.42,
  667. y: 0.42,
  668. },
  669. },
  670. });
  671. await awaitRunningJobs(adminClient);
  672. const { search: search2 } = await searchForLaptop();
  673. expect(search2.items[0].productAsset!.id).toBe('T_1');
  674. expect(search2.items[0].productAsset!.focalPoint).toEqual({ x: 0.42, y: 0.42 });
  675. });
  676. it('updates index when asset deleted', async () => {
  677. const { search: search1 } = await searchForLaptop();
  678. const assetId = search1.items[0].productAsset?.id;
  679. expect(assetId).toBeTruthy();
  680. await adminClient.query(deleteAssetDocument, {
  681. input: {
  682. assetId: assetId!,
  683. force: true,
  684. },
  685. });
  686. await awaitRunningJobs(adminClient);
  687. const { search: search2 } = await searchForLaptop();
  688. expect(search2.items[0].productAsset).toBeNull();
  689. });
  690. });
  691. it('does not include deleted ProductVariants in index', async () => {
  692. const { search: s1 } = await doAdminSearchQuery(adminClient, {
  693. term: 'hard drive',
  694. groupByProduct: false,
  695. });
  696. const variantToDelete = s1.items.find(i => i.sku === 'IHD455T2_updated')!;
  697. await adminClient.query(deleteProductVariantDocument, {
  698. id: variantToDelete.productVariantId,
  699. });
  700. await awaitRunningJobs(adminClient);
  701. const { search } = await adminClient.query(searchGetPricesDocument, {
  702. input: { term: 'hard drive', groupByProduct: true },
  703. });
  704. expect(search.items[0].price).toEqual({
  705. min: 7896,
  706. max: 13435,
  707. });
  708. });
  709. it('returns disabled field when not grouped', async () => {
  710. const result = await doAdminSearchQuery(adminClient, {
  711. groupByProduct: false,
  712. term: 'laptop',
  713. });
  714. expect(result.search.items.map(pick(['productVariantId', 'enabled']))).toEqual([
  715. { productVariantId: 'T_1', enabled: true },
  716. { productVariantId: 'T_2', enabled: true },
  717. { productVariantId: 'T_3', enabled: false },
  718. { productVariantId: 'T_4', enabled: true },
  719. ]);
  720. });
  721. it('when grouped, disabled is false if at least one variant is enabled', async () => {
  722. await adminClient.query(updateProductVariantsDocument, {
  723. input: [
  724. { id: 'T_1', enabled: false },
  725. { id: 'T_2', enabled: false },
  726. ],
  727. });
  728. await awaitRunningJobs(adminClient);
  729. const result = await doAdminSearchQuery(adminClient, {
  730. groupByProduct: true,
  731. term: 'laptop',
  732. });
  733. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  734. { productId: 'T_1', enabled: true },
  735. ]);
  736. });
  737. it('when grouped, disabled is true if all variants are disabled', async () => {
  738. await adminClient.query(updateProductVariantsDocument, {
  739. input: [{ id: 'T_4', enabled: false }],
  740. });
  741. await awaitRunningJobs(adminClient);
  742. const result = await doAdminSearchQuery(adminClient, {
  743. groupByProduct: true,
  744. take: 3,
  745. term: 'laptop',
  746. });
  747. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  748. { productId: 'T_1', enabled: false },
  749. ]);
  750. });
  751. it('when grouped, disabled is true product is disabled', async () => {
  752. await adminClient.query(updateProductDocument, {
  753. input: {
  754. id: 'T_3',
  755. enabled: false,
  756. },
  757. });
  758. await awaitRunningJobs(adminClient);
  759. const result = await doAdminSearchQuery(adminClient, {
  760. groupByProduct: true,
  761. term: 'gaming',
  762. });
  763. const t3 = result.search.items.find(i => i.productId === 'T_3');
  764. expect(t3?.enabled).toEqual(false);
  765. });
  766. // https://github.com/vendure-ecommerce/vendure/issues/295
  767. it('enabled status survives reindex', async () => {
  768. await adminClient.query(reindexDocument);
  769. await awaitRunningJobs(adminClient);
  770. const result = await doAdminSearchQuery(adminClient, {
  771. term: 'laptop',
  772. groupByProduct: true,
  773. take: 3,
  774. });
  775. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  776. { productId: 'T_1', enabled: false },
  777. ]);
  778. });
  779. });
  780. // https://github.com/vendure-ecommerce/vendure/issues/609
  781. describe('Synthetic index items', () => {
  782. let createdProductId: string;
  783. it('creates synthetic index item for Product with no variants', async () => {
  784. const { createProduct } = await adminClient.query(createProductDocument, {
  785. input: {
  786. facetValueIds: ['T_1'],
  787. translations: [
  788. {
  789. languageCode: LanguageCode.en,
  790. name: 'Strawberry cheesecake',
  791. slug: 'strawberry-cheesecake',
  792. description: 'A yummy dessert',
  793. },
  794. ],
  795. },
  796. });
  797. await awaitRunningJobs(adminClient);
  798. const result = await doAdminSearchQuery(adminClient, {
  799. groupByProduct: true,
  800. term: 'strawberry',
  801. });
  802. expect(
  803. result.search.items.map(
  804. pick([
  805. 'productId',
  806. 'enabled',
  807. 'productName',
  808. 'productVariantName',
  809. 'slug',
  810. 'description',
  811. ]),
  812. ),
  813. ).toEqual([
  814. {
  815. productId: createProduct.id,
  816. enabled: false,
  817. productName: 'Strawberry cheesecake',
  818. productVariantName: 'Strawberry cheesecake',
  819. slug: 'strawberry-cheesecake',
  820. description: 'A yummy dessert',
  821. },
  822. ]);
  823. createdProductId = createProduct.id;
  824. });
  825. it('removes synthetic index item once a variant is created', async () => {
  826. await adminClient.query(createProductVariantsDocument, {
  827. input: [
  828. {
  829. productId: createdProductId,
  830. sku: 'SC01',
  831. price: 1399,
  832. translations: [
  833. { languageCode: LanguageCode.en, name: 'Strawberry Cheesecake Pie' },
  834. ],
  835. },
  836. ],
  837. });
  838. await awaitRunningJobs(adminClient);
  839. const result = await doAdminSearchQuery(adminClient, {
  840. groupByProduct: true,
  841. term: 'strawberry',
  842. });
  843. expect(result.search.items.map(pick(['productVariantName']))).toEqual([
  844. { productVariantName: 'Strawberry Cheesecake Pie' },
  845. ]);
  846. });
  847. });
  848. describe('channel handling', () => {
  849. const SECOND_CHANNEL_TOKEN = 'second-channel-token';
  850. let secondChannel: ChannelFragment;
  851. beforeAll(async () => {
  852. const { createChannel } = await adminClient.query(createChannelDocument, {
  853. input: {
  854. code: 'second-channel',
  855. token: SECOND_CHANNEL_TOKEN,
  856. defaultLanguageCode: LanguageCode.en,
  857. currencyCode: CurrencyCode.GBP,
  858. pricesIncludeTax: true,
  859. defaultTaxZoneId: 'T_2',
  860. defaultShippingZoneId: 'T_1',
  861. },
  862. });
  863. secondChannel = createChannel as ChannelFragment;
  864. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  865. await adminClient.query(reindexDocument);
  866. await awaitRunningJobs(adminClient);
  867. });
  868. it('new channel is initially empty', async () => {
  869. const { search: searchGrouped } = await doAdminSearchQuery(adminClient, {
  870. groupByProduct: true,
  871. });
  872. const { search: searchUngrouped } = await doAdminSearchQuery(adminClient, {
  873. groupByProduct: false,
  874. });
  875. expect(searchGrouped.totalItems).toEqual(0);
  876. expect(searchUngrouped.totalItems).toEqual(0);
  877. });
  878. it('adding product to channel', async () => {
  879. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  880. await adminClient.query(assignProductToChannelDocument, {
  881. input: { channelId: secondChannel.id, productIds: ['T_1', 'T_2'] },
  882. });
  883. await awaitRunningJobs(adminClient);
  884. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  885. const { search } = await doAdminSearchQuery(adminClient, { groupByProduct: true });
  886. expect(search.items.map(i => i.productId).sort()).toEqual(['T_1', 'T_2']);
  887. });
  888. it('removing product from channel', async () => {
  889. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  890. await adminClient.query(removeProductFromChannelDocument, {
  891. input: {
  892. productIds: ['T_2'],
  893. channelId: secondChannel.id,
  894. },
  895. });
  896. await awaitRunningJobs(adminClient);
  897. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  898. const { search } = await doAdminSearchQuery(adminClient, { groupByProduct: true });
  899. expect(search.items.map(i => i.productId)).toEqual(['T_1']);
  900. });
  901. it('reindexes in channel', async () => {
  902. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  903. const { reindex } = await adminClient.query(reindexDocument);
  904. await awaitRunningJobs(adminClient);
  905. const { job } = await adminClient.query(getJobInfoDocument, { id: reindex.id });
  906. expect(job!.state).toBe(JobState.COMPLETED);
  907. const { search } = await doAdminSearchQuery(adminClient, { groupByProduct: true });
  908. expect(search.items.map(i => i.productId).sort()).toEqual(['T_1']);
  909. });
  910. it('adding product variant to channel', async () => {
  911. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  912. await adminClient.query(assignProductVariantToChannelDocument, {
  913. input: { channelId: secondChannel.id, productVariantIds: ['T_10', 'T_15'] },
  914. });
  915. await awaitRunningJobs(adminClient);
  916. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  917. const { search: searchGrouped } = await doAdminSearchQuery(adminClient, {
  918. groupByProduct: true,
  919. });
  920. expect(searchGrouped.items.map(i => i.productId).sort()).toEqual(['T_1', 'T_3', 'T_4']);
  921. const { search: searchUngrouped } = await doAdminSearchQuery(adminClient, {
  922. groupByProduct: false,
  923. });
  924. expect(searchUngrouped.items.map(i => i.productVariantId).sort()).toEqual([
  925. 'T_1',
  926. 'T_10',
  927. 'T_15',
  928. 'T_2',
  929. 'T_3',
  930. 'T_4',
  931. ]);
  932. });
  933. it('removing product variant from channel', async () => {
  934. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  935. await adminClient.query(removeProductVariantFromChannelDocument, {
  936. input: { channelId: secondChannel.id, productVariantIds: ['T_1', 'T_15'] },
  937. });
  938. await awaitRunningJobs(adminClient);
  939. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  940. const { search: searchGrouped } = await doAdminSearchQuery(adminClient, {
  941. groupByProduct: true,
  942. });
  943. expect(searchGrouped.items.map(i => i.productId).sort()).toEqual(['T_1', 'T_3']);
  944. const { search: searchUngrouped } = await doAdminSearchQuery(adminClient, {
  945. groupByProduct: false,
  946. });
  947. expect(searchUngrouped.items.map(i => i.productVariantId).sort()).toEqual([
  948. 'T_10',
  949. 'T_2',
  950. 'T_3',
  951. 'T_4',
  952. ]);
  953. });
  954. it('updating product affects current channel', async () => {
  955. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  956. await adminClient.query(updateProductDocument, {
  957. input: {
  958. id: 'T_3',
  959. enabled: true,
  960. translations: [{ languageCode: LanguageCode.en, name: 'xyz' }],
  961. },
  962. });
  963. await awaitRunningJobs(adminClient);
  964. const { search: searchGrouped } = await doAdminSearchQuery(adminClient, {
  965. groupByProduct: true,
  966. term: 'xyz',
  967. });
  968. expect(searchGrouped.items.map(i => i.productName)).toEqual(['xyz']);
  969. });
  970. it('updating product affects other channels', async () => {
  971. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  972. const { search: searchGrouped } = await doAdminSearchQuery(adminClient, {
  973. groupByProduct: true,
  974. term: 'xyz',
  975. });
  976. expect(searchGrouped.items.map(i => i.productName)).toEqual(['xyz']);
  977. });
  978. // https://github.com/vendure-ecommerce/vendure/issues/896
  979. it('removing from channel with multiple languages', async () => {
  980. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  981. await adminClient.query(updateProductDocument, {
  982. input: {
  983. id: 'T_4',
  984. translations: [
  985. {
  986. languageCode: LanguageCode.en,
  987. name: 'product en',
  988. slug: 'product-en',
  989. description: 'en',
  990. },
  991. {
  992. languageCode: LanguageCode.de,
  993. name: 'product de',
  994. slug: 'product-de',
  995. description: 'de',
  996. },
  997. ],
  998. },
  999. });
  1000. await adminClient.query(assignProductToChannelDocument, {
  1001. input: { channelId: secondChannel.id, productIds: ['T_4'] },
  1002. });
  1003. await awaitRunningJobs(adminClient);
  1004. async function searchSecondChannelForDEProduct() {
  1005. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  1006. const { search } = await adminClient.query(
  1007. searchProductsAdminDocument,
  1008. {
  1009. input: { term: 'product', groupByProduct: true },
  1010. },
  1011. { languageCode: LanguageCode.de },
  1012. );
  1013. return search;
  1014. }
  1015. const search1 = await searchSecondChannelForDEProduct();
  1016. expect(search1.items.map(i => i.productName)).toEqual(['product de']);
  1017. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  1018. await adminClient.query(removeProductFromChannelDocument, {
  1019. input: {
  1020. productIds: ['T_4'],
  1021. channelId: secondChannel.id,
  1022. },
  1023. });
  1024. await awaitRunningJobs(adminClient);
  1025. const search2 = await searchSecondChannelForDEProduct();
  1026. expect(search2.items.map(i => i.productName)).toEqual([]);
  1027. });
  1028. });
  1029. describe('multiple language handling', () => {
  1030. function searchInLanguage(languageCode: LanguageCode, groupByProduct: boolean) {
  1031. return adminClient.query(
  1032. searchProductsAdminDocument,
  1033. {
  1034. input: {
  1035. take: 1,
  1036. term: 'laptop',
  1037. groupByProduct,
  1038. },
  1039. },
  1040. {
  1041. languageCode,
  1042. },
  1043. );
  1044. }
  1045. beforeAll(async () => {
  1046. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  1047. const { updateProduct } = await adminClient.query(updateProductDocument, {
  1048. input: {
  1049. id: 'T_1',
  1050. translations: [
  1051. {
  1052. languageCode: LanguageCode.de,
  1053. name: 'laptop name de',
  1054. slug: 'laptop-slug-de',
  1055. description: 'laptop description de',
  1056. },
  1057. {
  1058. languageCode: LanguageCode.zh,
  1059. name: 'laptop name zh',
  1060. slug: 'laptop-slug-zh',
  1061. description: 'laptop description zh',
  1062. },
  1063. ],
  1064. },
  1065. });
  1066. await adminClient.query(updateProductVariantsDocument, {
  1067. input: [
  1068. {
  1069. id: updateProduct.variants[0].id,
  1070. translations: [
  1071. {
  1072. languageCode: LanguageCode.fr,
  1073. name: 'laptop variant fr',
  1074. },
  1075. ],
  1076. },
  1077. ],
  1078. });
  1079. await awaitRunningJobs(adminClient);
  1080. });
  1081. it('indexes product-level languages', async () => {
  1082. const { search: search1 } = await searchInLanguage(LanguageCode.de, true);
  1083. expect(search1.items[0].productName).toBe('laptop name de');
  1084. expect(search1.items[0].slug).toBe('laptop-slug-de');
  1085. expect(search1.items[0].description).toBe('laptop description de');
  1086. const { search: search2 } = await searchInLanguage(LanguageCode.zh, true);
  1087. expect(search2.items[0].productName).toBe('laptop name zh');
  1088. expect(search2.items[0].slug).toBe('laptop-slug-zh');
  1089. expect(search2.items[0].description).toBe('laptop description zh');
  1090. });
  1091. it('indexes product variant-level languages', async () => {
  1092. const { search: search1 } = await searchInLanguage(LanguageCode.fr, false);
  1093. expect(search1.items.length ? search1.items[0].productName : undefined).toBe('Laptop');
  1094. expect(search1.items.length ? search1.items[0].productVariantName : undefined).toBe(
  1095. 'laptop variant fr',
  1096. );
  1097. });
  1098. });
  1099. });
  1100. describe('customMappings', () => {
  1101. it('variant mappings', async () => {
  1102. const query = `{
  1103. search(input: { take: 1, groupByProduct: false, sort: { name: ASC } }) {
  1104. items {
  1105. productVariantName
  1106. customProductVariantMappings {
  1107. inStock
  1108. }
  1109. customProductMappings {
  1110. answer
  1111. }
  1112. customMappings {
  1113. ...on CustomProductVariantMappings {
  1114. inStock
  1115. }
  1116. ...on CustomProductMappings {
  1117. answer
  1118. }
  1119. }
  1120. }
  1121. }
  1122. }`;
  1123. const { search } = await shopClient.query(gql(query));
  1124. expect(search.items[0]).toEqual({
  1125. productVariantName: 'Bonsai Tree',
  1126. customProductVariantMappings: {
  1127. inStock: false,
  1128. },
  1129. customProductMappings: {
  1130. answer: 42,
  1131. },
  1132. customMappings: {
  1133. inStock: false,
  1134. },
  1135. });
  1136. });
  1137. // https://github.com/vendure-ecommerce/vendure/issues/1638
  1138. it('hydrates variant custom field relation', async () => {
  1139. await adminClient.query(updateProductVariantsDocument, {
  1140. input: [
  1141. {
  1142. id: 'T_20',
  1143. customFields: {
  1144. materialId: 'T_1',
  1145. },
  1146. },
  1147. ],
  1148. });
  1149. await adminClient.query(reindexDocument);
  1150. await awaitRunningJobs(adminClient);
  1151. const query = `{
  1152. search(input: { groupByProduct: false, term: "tripod" }) {
  1153. items {
  1154. productVariantName
  1155. productVariantId
  1156. customMappings {
  1157. ...on CustomProductVariantMappings {
  1158. materialCode
  1159. }
  1160. }
  1161. }
  1162. }
  1163. }`;
  1164. const { search } = await shopClient.query(gql(query));
  1165. expect(search.items.map((i: any) => i.customMappings)).toEqual([{ materialCode: 'electronics' }]);
  1166. });
  1167. it('product mappings', async () => {
  1168. const query = `{
  1169. search(input: { take: 1, groupByProduct: true, sort: { name: ASC } }) {
  1170. items {
  1171. productName
  1172. customMappings {
  1173. ...on CustomProductMappings {
  1174. answer
  1175. }
  1176. }
  1177. }
  1178. }
  1179. }`;
  1180. const { search } = await shopClient.query(gql(query));
  1181. expect(search.items[0]).toEqual({
  1182. productName: 'Bonsai Tree',
  1183. customMappings: {
  1184. answer: 42,
  1185. },
  1186. });
  1187. });
  1188. it('private mappings', async () => {
  1189. const query = `{
  1190. search(input: { take: 1, groupByProduct: true, sort: { name: ASC } }) {
  1191. items {
  1192. customMappings {
  1193. ...on CustomProductMappings {
  1194. answer
  1195. hello
  1196. }
  1197. }
  1198. }
  1199. }
  1200. }`;
  1201. try {
  1202. await shopClient.query(gql(query));
  1203. } catch (error: any) {
  1204. expect(error).toBeDefined();
  1205. expect(error.message).toContain('Cannot query field "hello"');
  1206. return;
  1207. }
  1208. fail('should not be able to query field "hello"');
  1209. });
  1210. });
  1211. describe('scriptFields', () => {
  1212. it('script mapping', async () => {
  1213. const query = `{
  1214. search(input: { take: 1, groupByProduct: true, sort: { name: ASC } }) {
  1215. items {
  1216. productVariantName
  1217. customScriptFields {
  1218. answerMultiplied
  1219. }
  1220. }
  1221. }
  1222. }`;
  1223. const { search } = await shopClient.query(gql(query));
  1224. expect(search.items[0]).toEqual({
  1225. productVariantName: 'Bonsai Tree',
  1226. customScriptFields: {
  1227. answerMultiplied: 84,
  1228. },
  1229. });
  1230. });
  1231. it('can use the custom search input field', async () => {
  1232. const query = `{
  1233. search(input: { take: 1, groupByProduct: true, sort: { name: ASC }, factor: 10 }) {
  1234. items {
  1235. productVariantName
  1236. customScriptFields {
  1237. answerMultiplied
  1238. }
  1239. }
  1240. }
  1241. }`;
  1242. const { search } = await shopClient.query(gql(query));
  1243. expect(search.items[0]).toEqual({
  1244. productVariantName: 'Bonsai Tree',
  1245. customScriptFields: {
  1246. answerMultiplied: 420,
  1247. },
  1248. });
  1249. });
  1250. });
  1251. describe('sort', () => {
  1252. it('sort ASC', async () => {
  1253. const query = `{
  1254. search(input: { take: 1, groupByProduct: true, sort: { priority: ASC } }) {
  1255. items {
  1256. customMappings {
  1257. ...on CustomProductMappings {
  1258. priority
  1259. }
  1260. }
  1261. }
  1262. }
  1263. }`;
  1264. const { search } = await shopClient.query(gql(query));
  1265. expect(search.items[0]).toEqual({
  1266. customMappings: {
  1267. priority: 1,
  1268. },
  1269. });
  1270. });
  1271. it('sort DESC', async () => {
  1272. const query = `{
  1273. search(input: { take: 1, groupByProduct: true, sort: { priority: DESC } }) {
  1274. items {
  1275. customMappings {
  1276. ...on CustomProductMappings {
  1277. priority
  1278. }
  1279. }
  1280. }
  1281. }
  1282. }`;
  1283. const { search } = await shopClient.query(gql(query));
  1284. expect(search.items[0]).toEqual({
  1285. customMappings: {
  1286. priority: 2,
  1287. },
  1288. });
  1289. });
  1290. });
  1291. });
  1292. export const searchProductsAdminDocument = graphql(`
  1293. query SearchProductsAdmin($input: SearchInput!) {
  1294. search(input: $input) {
  1295. totalItems
  1296. items {
  1297. enabled
  1298. productId
  1299. productName
  1300. slug
  1301. description
  1302. productAsset {
  1303. id
  1304. preview
  1305. focalPoint {
  1306. x
  1307. y
  1308. }
  1309. }
  1310. productVariantId
  1311. productVariantName
  1312. productVariantAsset {
  1313. id
  1314. preview
  1315. focalPoint {
  1316. x
  1317. y
  1318. }
  1319. }
  1320. sku
  1321. }
  1322. }
  1323. }
  1324. `);
  1325. export const searchGetFacetValuesDocument = graphql(`
  1326. query SearchFacetValues($input: SearchInput!) {
  1327. search(input: $input) {
  1328. totalItems
  1329. facetValues {
  1330. count
  1331. facetValue {
  1332. id
  1333. name
  1334. }
  1335. }
  1336. }
  1337. }
  1338. `);
  1339. export const searchGetCollectionsDocument = graphql(`
  1340. query SearchCollections($input: SearchInput!) {
  1341. search(input: $input) {
  1342. totalItems
  1343. collections {
  1344. count
  1345. collection {
  1346. id
  1347. name
  1348. }
  1349. }
  1350. }
  1351. }
  1352. `);
  1353. export const searchGetPricesDocument = graphql(`
  1354. query SearchGetPrices($input: SearchInput!) {
  1355. search(input: $input) {
  1356. items {
  1357. price {
  1358. ... on PriceRange {
  1359. min
  1360. max
  1361. }
  1362. ... on SinglePrice {
  1363. value
  1364. }
  1365. }
  1366. priceWithTax {
  1367. ... on PriceRange {
  1368. min
  1369. max
  1370. }
  1371. ... on SinglePrice {
  1372. value
  1373. }
  1374. }
  1375. }
  1376. }
  1377. }
  1378. `);
  1379. const reindexDocument = graphql(`
  1380. mutation Reindex {
  1381. reindex {
  1382. id
  1383. queueName
  1384. state
  1385. progress
  1386. duration
  1387. result
  1388. }
  1389. }
  1390. `);
  1391. const getJobInfoDocument = graphql(`
  1392. query GetJobInfo($id: ID!) {
  1393. job(jobId: $id) {
  1394. id
  1395. queueName
  1396. state
  1397. progress
  1398. duration
  1399. result
  1400. }
  1401. }
  1402. `);