default-search-plugin.e2e-spec.ts 58 KB

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