elasticsearch-plugin.e2e-spec.ts 64 KB

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