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

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