elasticsearch-plugin.e2e-spec.ts 64 KB

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