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

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