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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /* tslint:disable:no-non-null-assertion */
  2. import { pick } from '@vendure/common/lib/pick';
  3. import {
  4. DefaultJobQueuePlugin,
  5. DefaultSearchPlugin,
  6. facetValueCollectionFilter,
  7. mergeConfig,
  8. } from '@vendure/core';
  9. import { createTestEnvironment, E2E_DEFAULT_CHANNEL_TOKEN, SimpleGraphQLClient } from '@vendure/testing';
  10. import gql from 'graphql-tag';
  11. import path from 'path';
  12. import { initialData } from '../../../e2e-common/e2e-initial-data';
  13. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  14. import {
  15. AssignProductsToChannel,
  16. CreateChannel,
  17. CreateCollection,
  18. CreateFacet,
  19. CurrencyCode,
  20. DeleteAsset,
  21. DeleteProduct,
  22. DeleteProductVariant,
  23. LanguageCode,
  24. Reindex,
  25. RemoveProductsFromChannel,
  26. SearchFacetValues,
  27. SearchGetAssets,
  28. SearchGetPrices,
  29. SearchInput,
  30. SortOrder,
  31. UpdateAsset,
  32. UpdateCollection,
  33. UpdateProduct,
  34. UpdateProductVariants,
  35. UpdateTaxRate,
  36. } from './graphql/generated-e2e-admin-types';
  37. import { LogicalOperator, SearchProductsShop } from './graphql/generated-e2e-shop-types';
  38. import {
  39. ASSIGN_PRODUCT_TO_CHANNEL,
  40. CREATE_CHANNEL,
  41. CREATE_COLLECTION,
  42. CREATE_FACET,
  43. DELETE_ASSET,
  44. DELETE_PRODUCT,
  45. DELETE_PRODUCT_VARIANT,
  46. REMOVE_PRODUCT_FROM_CHANNEL,
  47. UPDATE_ASSET,
  48. UPDATE_COLLECTION,
  49. UPDATE_PRODUCT,
  50. UPDATE_PRODUCT_VARIANTS,
  51. UPDATE_TAX_RATE,
  52. } from './graphql/shared-definitions';
  53. import { SEARCH_PRODUCTS_SHOP } from './graphql/shop-definitions';
  54. import { awaitRunningJobs } from './utils/await-running-jobs';
  55. describe('Default search plugin', () => {
  56. const { server, adminClient, shopClient } = createTestEnvironment(
  57. mergeConfig(testConfig, { plugins: [DefaultSearchPlugin, DefaultJobQueuePlugin] }),
  58. );
  59. beforeAll(async () => {
  60. await server.init({
  61. initialData,
  62. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  63. customerCount: 1,
  64. });
  65. await adminClient.asSuperAdmin();
  66. }, TEST_SETUP_TIMEOUT_MS);
  67. afterAll(async () => {
  68. await server.destroy();
  69. });
  70. function doAdminSearchQuery(input: SearchInput) {
  71. return adminClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(SEARCH_PRODUCTS, {
  72. input,
  73. });
  74. }
  75. async function testGroupByProduct(client: SimpleGraphQLClient) {
  76. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  77. SEARCH_PRODUCTS_SHOP,
  78. {
  79. input: {
  80. groupByProduct: true,
  81. },
  82. },
  83. );
  84. expect(result.search.totalItems).toBe(20);
  85. }
  86. async function testNoGrouping(client: SimpleGraphQLClient) {
  87. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  88. SEARCH_PRODUCTS_SHOP,
  89. {
  90. input: {
  91. groupByProduct: false,
  92. },
  93. },
  94. );
  95. expect(result.search.totalItems).toBe(34);
  96. }
  97. async function testMatchSearchTerm(client: SimpleGraphQLClient) {
  98. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  99. SEARCH_PRODUCTS_SHOP,
  100. {
  101. input: {
  102. term: 'camera',
  103. groupByProduct: true,
  104. sort: {
  105. name: SortOrder.ASC,
  106. },
  107. },
  108. },
  109. );
  110. expect(result.search.items.map(i => i.productName)).toEqual([
  111. 'Camera Lens',
  112. 'Instant Camera',
  113. 'Slr Camera',
  114. ]);
  115. }
  116. async function testMatchFacetIdsAnd(client: SimpleGraphQLClient) {
  117. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  118. SEARCH_PRODUCTS_SHOP,
  119. {
  120. input: {
  121. facetValueIds: ['T_1', 'T_2'],
  122. facetValueOperator: LogicalOperator.AND,
  123. groupByProduct: true,
  124. },
  125. },
  126. );
  127. expect(result.search.items.map(i => i.productName)).toEqual([
  128. 'Laptop',
  129. 'Curvy Monitor',
  130. 'Gaming PC',
  131. 'Hard Drive',
  132. 'Clacky Keyboard',
  133. 'USB Cable',
  134. ]);
  135. }
  136. async function testMatchFacetIdsOr(client: SimpleGraphQLClient) {
  137. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  138. SEARCH_PRODUCTS_SHOP,
  139. {
  140. input: {
  141. facetValueIds: ['T_1', 'T_5'],
  142. facetValueOperator: LogicalOperator.OR,
  143. groupByProduct: true,
  144. },
  145. },
  146. );
  147. expect(result.search.items.map(i => i.productName)).toEqual([
  148. 'Laptop',
  149. 'Curvy Monitor',
  150. 'Gaming PC',
  151. 'Hard Drive',
  152. 'Clacky Keyboard',
  153. 'USB Cable',
  154. 'Instant Camera',
  155. 'Camera Lens',
  156. 'Tripod',
  157. 'Slr Camera',
  158. 'Spiky Cactus',
  159. 'Orchid',
  160. 'Bonsai Tree',
  161. ]);
  162. }
  163. async function testMatchCollectionId(client: SimpleGraphQLClient) {
  164. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  165. SEARCH_PRODUCTS_SHOP,
  166. {
  167. input: {
  168. collectionId: 'T_2',
  169. groupByProduct: true,
  170. },
  171. },
  172. );
  173. expect(result.search.items.map(i => i.productName)).toEqual([
  174. 'Spiky Cactus',
  175. 'Orchid',
  176. 'Bonsai Tree',
  177. ]);
  178. }
  179. async function testMatchCollectionSlug(client: SimpleGraphQLClient) {
  180. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  181. SEARCH_PRODUCTS_SHOP,
  182. {
  183. input: {
  184. collectionSlug: 'plants',
  185. groupByProduct: true,
  186. },
  187. },
  188. );
  189. expect(result.search.items.map(i => i.productName)).toEqual([
  190. 'Spiky Cactus',
  191. 'Orchid',
  192. 'Bonsai Tree',
  193. ]);
  194. }
  195. async function testSinglePrices(client: SimpleGraphQLClient) {
  196. const result = await client.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
  197. SEARCH_GET_PRICES,
  198. {
  199. input: {
  200. groupByProduct: false,
  201. take: 3,
  202. } as SearchInput,
  203. },
  204. );
  205. expect(result.search.items).toEqual([
  206. {
  207. price: { value: 129900 },
  208. priceWithTax: { value: 155880 },
  209. },
  210. {
  211. price: { value: 139900 },
  212. priceWithTax: { value: 167880 },
  213. },
  214. {
  215. price: { value: 219900 },
  216. priceWithTax: { value: 263880 },
  217. },
  218. ]);
  219. }
  220. async function testPriceRanges(client: SimpleGraphQLClient) {
  221. const result = await client.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
  222. SEARCH_GET_PRICES,
  223. {
  224. input: {
  225. groupByProduct: true,
  226. take: 3,
  227. } as SearchInput,
  228. },
  229. );
  230. expect(result.search.items).toEqual([
  231. {
  232. price: { min: 129900, max: 229900 },
  233. priceWithTax: { min: 155880, max: 275880 },
  234. },
  235. {
  236. price: { min: 14374, max: 16994 },
  237. priceWithTax: { min: 17249, max: 20393 },
  238. },
  239. {
  240. price: { min: 93120, max: 109995 },
  241. priceWithTax: { min: 111744, max: 131994 },
  242. },
  243. ]);
  244. }
  245. describe('shop api', () => {
  246. it('group by product', () => testGroupByProduct(shopClient));
  247. it('no grouping', () => testNoGrouping(shopClient));
  248. it('matches search term', () => testMatchSearchTerm(shopClient));
  249. it('matches by facetId with AND operator', () => testMatchFacetIdsAnd(shopClient));
  250. it('matches by facetId with OR operator', () => testMatchFacetIdsOr(shopClient));
  251. it('matches by collectionId', () => testMatchCollectionId(shopClient));
  252. it('matches by collectionSlug', () => testMatchCollectionSlug(shopClient));
  253. it('single prices', () => testSinglePrices(shopClient));
  254. it('price ranges', () => testPriceRanges(shopClient));
  255. it('returns correct facetValues when not grouped by product', async () => {
  256. const result = await shopClient.query<SearchFacetValues.Query, SearchFacetValues.Variables>(
  257. SEARCH_GET_FACET_VALUES,
  258. {
  259. input: {
  260. groupByProduct: false,
  261. },
  262. },
  263. );
  264. expect(result.search.facetValues).toEqual([
  265. { count: 21, facetValue: { id: 'T_1', name: 'electronics' } },
  266. { count: 17, facetValue: { id: 'T_2', name: 'computers' } },
  267. { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
  268. { count: 10, facetValue: { id: 'T_4', name: 'sports equipment' } },
  269. { count: 3, facetValue: { id: 'T_5', name: 'home & garden' } },
  270. { count: 3, facetValue: { id: 'T_6', name: 'plants' } },
  271. ]);
  272. });
  273. it('returns correct facetValues when grouped by product', async () => {
  274. const result = await shopClient.query<SearchFacetValues.Query, SearchFacetValues.Variables>(
  275. SEARCH_GET_FACET_VALUES,
  276. {
  277. input: {
  278. groupByProduct: true,
  279. },
  280. },
  281. );
  282. expect(result.search.facetValues).toEqual([
  283. { count: 10, facetValue: { id: 'T_1', name: 'electronics' } },
  284. { count: 6, facetValue: { id: 'T_2', name: 'computers' } },
  285. { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
  286. { count: 7, facetValue: { id: 'T_4', name: 'sports equipment' } },
  287. { count: 3, facetValue: { id: 'T_5', name: 'home & garden' } },
  288. { count: 3, facetValue: { id: 'T_6', name: 'plants' } },
  289. ]);
  290. });
  291. it('omits facetValues of private facets', async () => {
  292. const { createFacet } = await adminClient.query<CreateFacet.Mutation, CreateFacet.Variables>(
  293. CREATE_FACET,
  294. {
  295. input: {
  296. code: 'profit-margin',
  297. isPrivate: true,
  298. translations: [{ languageCode: LanguageCode.en, name: 'Profit Margin' }],
  299. values: [
  300. {
  301. code: 'massive',
  302. translations: [{ languageCode: LanguageCode.en, name: 'massive' }],
  303. },
  304. ],
  305. },
  306. },
  307. );
  308. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  309. input: {
  310. id: 'T_2',
  311. // T_1 & T_2 are the existing facetValues (electronics & photo)
  312. facetValueIds: ['T_1', 'T_2', createFacet.values[0].id],
  313. },
  314. });
  315. await awaitRunningJobs(adminClient);
  316. const result = await shopClient.query<SearchFacetValues.Query, SearchFacetValues.Variables>(
  317. SEARCH_GET_FACET_VALUES,
  318. {
  319. input: {
  320. groupByProduct: true,
  321. },
  322. },
  323. );
  324. expect(result.search.facetValues).toEqual([
  325. { count: 10, facetValue: { id: 'T_1', name: 'electronics' } },
  326. { count: 6, facetValue: { id: 'T_2', name: 'computers' } },
  327. { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
  328. { count: 7, facetValue: { id: 'T_4', name: 'sports equipment' } },
  329. { count: 3, facetValue: { id: 'T_5', name: 'home & garden' } },
  330. { count: 3, facetValue: { id: 'T_6', name: 'plants' } },
  331. ]);
  332. });
  333. it('encodes the productId and productVariantId', async () => {
  334. const result = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  335. SEARCH_PRODUCTS_SHOP,
  336. {
  337. input: {
  338. groupByProduct: false,
  339. take: 1,
  340. },
  341. },
  342. );
  343. expect(pick(result.search.items[0], ['productId', 'productVariantId'])).toEqual({
  344. productId: 'T_1',
  345. productVariantId: 'T_1',
  346. });
  347. });
  348. it('omits results for disabled ProductVariants', async () => {
  349. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  350. UPDATE_PRODUCT_VARIANTS,
  351. {
  352. input: [{ id: 'T_3', enabled: false }],
  353. },
  354. );
  355. await awaitRunningJobs(adminClient);
  356. const result = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  357. SEARCH_PRODUCTS_SHOP,
  358. {
  359. input: {
  360. groupByProduct: false,
  361. take: 3,
  362. },
  363. },
  364. );
  365. expect(result.search.items.map(i => i.productVariantId)).toEqual(['T_1', 'T_2', 'T_4']);
  366. });
  367. it('encodes collectionIds', async () => {
  368. const result = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  369. SEARCH_PRODUCTS_SHOP,
  370. {
  371. input: {
  372. groupByProduct: false,
  373. term: 'cactus',
  374. take: 1,
  375. },
  376. },
  377. );
  378. expect(result.search.items[0].collectionIds).toEqual(['T_2']);
  379. });
  380. });
  381. describe('admin api', () => {
  382. it('group by product', () => testGroupByProduct(adminClient));
  383. it('no grouping', () => testNoGrouping(adminClient));
  384. it('matches search term', () => testMatchSearchTerm(adminClient));
  385. it('matches by facetId with AND operator', () => testMatchFacetIdsAnd(adminClient));
  386. it('matches by facetId with OR operator', () => testMatchFacetIdsOr(adminClient));
  387. it('matches by collectionId', () => testMatchCollectionId(adminClient));
  388. it('matches by collectionSlug', () => testMatchCollectionSlug(adminClient));
  389. it('single prices', () => testSinglePrices(adminClient));
  390. it('price ranges', () => testPriceRanges(adminClient));
  391. describe('updating the index', () => {
  392. it('updates index when ProductVariants are changed', async () => {
  393. await awaitRunningJobs(adminClient);
  394. const { search } = await doAdminSearchQuery({ term: 'drive', groupByProduct: false });
  395. expect(search.items.map(i => i.sku)).toEqual([
  396. 'IHD455T1',
  397. 'IHD455T2',
  398. 'IHD455T3',
  399. 'IHD455T4',
  400. 'IHD455T6',
  401. ]);
  402. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  403. UPDATE_PRODUCT_VARIANTS,
  404. {
  405. input: search.items.map(i => ({
  406. id: i.productVariantId,
  407. sku: i.sku + '_updated',
  408. })),
  409. },
  410. );
  411. await awaitRunningJobs(adminClient);
  412. const { search: search2 } = await doAdminSearchQuery({
  413. term: 'drive',
  414. groupByProduct: false,
  415. });
  416. expect(search2.items.map(i => i.sku)).toEqual([
  417. 'IHD455T1_updated',
  418. 'IHD455T2_updated',
  419. 'IHD455T3_updated',
  420. 'IHD455T4_updated',
  421. 'IHD455T6_updated',
  422. ]);
  423. });
  424. it('updates index when ProductVariants are deleted', async () => {
  425. await awaitRunningJobs(adminClient);
  426. const { search } = await doAdminSearchQuery({ term: 'drive', groupByProduct: false });
  427. await adminClient.query<DeleteProductVariant.Mutation, DeleteProductVariant.Variables>(
  428. DELETE_PRODUCT_VARIANT,
  429. {
  430. id: search.items[0].productVariantId,
  431. },
  432. );
  433. await awaitRunningJobs(adminClient);
  434. const { search: search2 } = await doAdminSearchQuery({
  435. term: 'drive',
  436. groupByProduct: false,
  437. });
  438. expect(search2.items.map(i => i.sku)).toEqual([
  439. 'IHD455T2_updated',
  440. 'IHD455T3_updated',
  441. 'IHD455T4_updated',
  442. 'IHD455T6_updated',
  443. ]);
  444. });
  445. it('updates index when a Product is changed', async () => {
  446. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  447. input: {
  448. id: 'T_1',
  449. facetValueIds: [],
  450. },
  451. });
  452. await awaitRunningJobs(adminClient);
  453. const result = await doAdminSearchQuery({ facetValueIds: ['T_2'], groupByProduct: true });
  454. expect(result.search.items.map(i => i.productName)).toEqual([
  455. 'Curvy Monitor',
  456. 'Gaming PC',
  457. 'Hard Drive',
  458. 'Clacky Keyboard',
  459. 'USB Cable',
  460. ]);
  461. });
  462. it('updates index when a Product is deleted', async () => {
  463. const { search } = await doAdminSearchQuery({ facetValueIds: ['T_2'], groupByProduct: true });
  464. expect(search.items.map(i => i.productId)).toEqual(['T_2', 'T_3', 'T_4', 'T_5', 'T_6']);
  465. await adminClient.query<DeleteProduct.Mutation, DeleteProduct.Variables>(DELETE_PRODUCT, {
  466. id: 'T_5',
  467. });
  468. await awaitRunningJobs(adminClient);
  469. const { search: search2 } = await doAdminSearchQuery({
  470. facetValueIds: ['T_2'],
  471. groupByProduct: true,
  472. });
  473. expect(search2.items.map(i => i.productId)).toEqual(['T_2', 'T_3', 'T_4', 'T_6']);
  474. });
  475. it('updates index when a Collection is changed', async () => {
  476. await adminClient.query<UpdateCollection.Mutation, UpdateCollection.Variables>(
  477. UPDATE_COLLECTION,
  478. {
  479. input: {
  480. id: 'T_2',
  481. filters: [
  482. {
  483. code: facetValueCollectionFilter.code,
  484. arguments: [
  485. {
  486. name: 'facetValueIds',
  487. value: `["T_4"]`,
  488. },
  489. {
  490. name: 'containsAny',
  491. value: `false`,
  492. },
  493. ],
  494. },
  495. ],
  496. },
  497. },
  498. );
  499. await awaitRunningJobs(adminClient);
  500. // add an additional check for the collection filters to update
  501. await awaitRunningJobs(adminClient);
  502. const result1 = await doAdminSearchQuery({ collectionId: 'T_2', groupByProduct: true });
  503. expect(result1.search.items.map(i => i.productName)).toEqual([
  504. 'Road Bike',
  505. 'Skipping Rope',
  506. 'Boxing Gloves',
  507. 'Tent',
  508. 'Cruiser Skateboard',
  509. 'Football',
  510. 'Running Shoe',
  511. ]);
  512. const result2 = await doAdminSearchQuery({ collectionSlug: 'plants', groupByProduct: true });
  513. expect(result2.search.items.map(i => i.productName)).toEqual([
  514. 'Road Bike',
  515. 'Skipping Rope',
  516. 'Boxing Gloves',
  517. 'Tent',
  518. 'Cruiser Skateboard',
  519. 'Football',
  520. 'Running Shoe',
  521. ]);
  522. });
  523. it('updates index when a Collection created', async () => {
  524. const { createCollection } = await adminClient.query<
  525. CreateCollection.Mutation,
  526. CreateCollection.Variables
  527. >(CREATE_COLLECTION, {
  528. input: {
  529. translations: [
  530. {
  531. languageCode: LanguageCode.en,
  532. name: 'Photo',
  533. description: '',
  534. slug: 'photo',
  535. },
  536. ],
  537. filters: [
  538. {
  539. code: facetValueCollectionFilter.code,
  540. arguments: [
  541. {
  542. name: 'facetValueIds',
  543. value: `["T_3"]`,
  544. },
  545. {
  546. name: 'containsAny',
  547. value: `false`,
  548. },
  549. ],
  550. },
  551. ],
  552. },
  553. });
  554. await awaitRunningJobs(adminClient);
  555. // add an additional check for the collection filters to update
  556. await awaitRunningJobs(adminClient);
  557. const result = await doAdminSearchQuery({
  558. collectionId: createCollection.id,
  559. groupByProduct: true,
  560. });
  561. expect(result.search.items.map(i => i.productName)).toEqual([
  562. 'Instant Camera',
  563. 'Camera Lens',
  564. 'Tripod',
  565. 'Slr Camera',
  566. ]);
  567. });
  568. it('updates index when a taxRate is changed', async () => {
  569. await adminClient.query<UpdateTaxRate.Mutation, UpdateTaxRate.Variables>(UPDATE_TAX_RATE, {
  570. input: {
  571. // Default Channel's defaultTaxZone is Europe (id 2) and the id of the standard TaxRate
  572. // to Europe is 2.
  573. id: 'T_2',
  574. value: 50,
  575. },
  576. });
  577. await awaitRunningJobs(adminClient);
  578. const result = await adminClient.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
  579. SEARCH_GET_PRICES,
  580. {
  581. input: {
  582. groupByProduct: true,
  583. term: 'laptop',
  584. } as SearchInput,
  585. },
  586. );
  587. expect(result.search.items).toEqual([
  588. {
  589. price: { min: 129900, max: 229900 },
  590. priceWithTax: { min: 194850, max: 344850 },
  591. },
  592. ]);
  593. });
  594. describe('asset changes', () => {
  595. function searchForLaptop() {
  596. return adminClient.query<SearchGetAssets.Query, SearchGetAssets.Variables>(
  597. SEARCH_GET_ASSETS,
  598. {
  599. input: {
  600. term: 'laptop',
  601. take: 1,
  602. },
  603. },
  604. );
  605. }
  606. it('updates index when asset focalPoint is changed', async () => {
  607. const { search: search1 } = await searchForLaptop();
  608. expect(search1.items[0].productAsset!.id).toBe('T_1');
  609. expect(search1.items[0].productAsset!.focalPoint).toBeNull();
  610. await adminClient.query<UpdateAsset.Mutation, UpdateAsset.Variables>(UPDATE_ASSET, {
  611. input: {
  612. id: 'T_1',
  613. focalPoint: {
  614. x: 0.42,
  615. y: 0.42,
  616. },
  617. },
  618. });
  619. await awaitRunningJobs(adminClient);
  620. const { search: search2 } = await searchForLaptop();
  621. expect(search2.items[0].productAsset!.id).toBe('T_1');
  622. expect(search2.items[0].productAsset!.focalPoint).toEqual({ x: 0.42, y: 0.42 });
  623. });
  624. it('updates index when asset deleted', async () => {
  625. const { search: search1 } = await searchForLaptop();
  626. const assetId = search1.items[0].productAsset?.id;
  627. expect(assetId).toBeTruthy();
  628. await adminClient.query<DeleteAsset.Mutation, DeleteAsset.Variables>(DELETE_ASSET, {
  629. id: assetId!,
  630. force: true,
  631. });
  632. await awaitRunningJobs(adminClient);
  633. const { search: search2 } = await searchForLaptop();
  634. expect(search2.items[0].productAsset).toBeNull();
  635. });
  636. });
  637. it('does not include deleted ProductVariants in index', async () => {
  638. const { search: s1 } = await doAdminSearchQuery({
  639. term: 'hard drive',
  640. groupByProduct: false,
  641. });
  642. const { deleteProductVariant } = await adminClient.query<
  643. DeleteProductVariant.Mutation,
  644. DeleteProductVariant.Variables
  645. >(DELETE_PRODUCT_VARIANT, { id: s1.items[0].productVariantId });
  646. await awaitRunningJobs(adminClient);
  647. const { search } = await adminClient.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
  648. SEARCH_GET_PRICES,
  649. { input: { term: 'hard drive', groupByProduct: true } },
  650. );
  651. expect(search.items[0].price).toEqual({
  652. min: 7896,
  653. max: 13435,
  654. });
  655. });
  656. it('returns enabled field when not grouped', async () => {
  657. const result = await doAdminSearchQuery({ groupByProduct: false, take: 3 });
  658. expect(result.search.items.map(pick(['productVariantId', 'enabled']))).toEqual([
  659. { productVariantId: 'T_1', enabled: true },
  660. { productVariantId: 'T_2', enabled: true },
  661. { productVariantId: 'T_3', enabled: false },
  662. ]);
  663. });
  664. it('when grouped, enabled is true if at least one variant is enabled', async () => {
  665. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  666. UPDATE_PRODUCT_VARIANTS,
  667. {
  668. input: [
  669. { id: 'T_1', enabled: false },
  670. { id: 'T_2', enabled: false },
  671. ],
  672. },
  673. );
  674. await awaitRunningJobs(adminClient);
  675. const result = await doAdminSearchQuery({ groupByProduct: true, take: 3 });
  676. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  677. { productId: 'T_1', enabled: true },
  678. { productId: 'T_2', enabled: true },
  679. { productId: 'T_3', enabled: true },
  680. ]);
  681. });
  682. it('when grouped, enabled is false if all variants are disabled', async () => {
  683. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  684. UPDATE_PRODUCT_VARIANTS,
  685. {
  686. input: [{ id: 'T_4', enabled: false }],
  687. },
  688. );
  689. await awaitRunningJobs(adminClient);
  690. const result = await doAdminSearchQuery({ groupByProduct: true, take: 3 });
  691. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  692. { productId: 'T_1', enabled: false },
  693. { productId: 'T_2', enabled: true },
  694. { productId: 'T_3', enabled: true },
  695. ]);
  696. });
  697. it('when grouped, enabled is false if product is disabled', async () => {
  698. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  699. input: {
  700. id: 'T_3',
  701. enabled: false,
  702. },
  703. });
  704. await awaitRunningJobs(adminClient);
  705. const result = await doAdminSearchQuery({ groupByProduct: true, take: 3 });
  706. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  707. { productId: 'T_1', enabled: false },
  708. { productId: 'T_2', enabled: true },
  709. { productId: 'T_3', enabled: false },
  710. ]);
  711. });
  712. // https://github.com/vendure-ecommerce/vendure/issues/295
  713. it('enabled status survives reindex', async () => {
  714. await adminClient.query<Reindex.Mutation>(REINDEX);
  715. await awaitRunningJobs(adminClient);
  716. const result = await doAdminSearchQuery({ groupByProduct: true, take: 3 });
  717. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  718. { productId: 'T_1', enabled: false },
  719. { productId: 'T_2', enabled: true },
  720. { productId: 'T_3', enabled: false },
  721. ]);
  722. });
  723. });
  724. describe('channel handling', () => {
  725. const SECOND_CHANNEL_TOKEN = 'second-channel-token';
  726. let secondChannel: CreateChannel.CreateChannel;
  727. beforeAll(async () => {
  728. const { createChannel } = await adminClient.query<
  729. CreateChannel.Mutation,
  730. CreateChannel.Variables
  731. >(CREATE_CHANNEL, {
  732. input: {
  733. code: 'second-channel',
  734. token: SECOND_CHANNEL_TOKEN,
  735. defaultLanguageCode: LanguageCode.en,
  736. currencyCode: CurrencyCode.GBP,
  737. pricesIncludeTax: true,
  738. defaultTaxZoneId: 'T_1',
  739. defaultShippingZoneId: 'T_1',
  740. },
  741. });
  742. secondChannel = createChannel;
  743. });
  744. it('adding product to channel', async () => {
  745. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  746. await adminClient.query<AssignProductsToChannel.Mutation, AssignProductsToChannel.Variables>(
  747. ASSIGN_PRODUCT_TO_CHANNEL,
  748. {
  749. input: { channelId: secondChannel.id, productIds: ['T_1', 'T_2'] },
  750. },
  751. );
  752. await awaitRunningJobs(adminClient);
  753. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  754. const { search } = await doAdminSearchQuery({ groupByProduct: true });
  755. expect(search.items.map(i => i.productId)).toEqual(['T_1', 'T_2']);
  756. }, 10000);
  757. it('removing product from channel', async () => {
  758. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  759. const { removeProductsFromChannel } = await adminClient.query<
  760. RemoveProductsFromChannel.Mutation,
  761. RemoveProductsFromChannel.Variables
  762. >(REMOVE_PRODUCT_FROM_CHANNEL, {
  763. input: {
  764. productIds: ['T_2'],
  765. channelId: secondChannel.id,
  766. },
  767. });
  768. await awaitRunningJobs(adminClient);
  769. adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
  770. const { search } = await doAdminSearchQuery({ groupByProduct: true });
  771. expect(search.items.map(i => i.productId)).toEqual(['T_1']);
  772. }, 10000);
  773. });
  774. });
  775. });
  776. export const REINDEX = gql`
  777. mutation Reindex {
  778. reindex {
  779. id
  780. }
  781. }
  782. `;
  783. export const SEARCH_PRODUCTS = gql`
  784. query SearchProductsAdmin($input: SearchInput!) {
  785. search(input: $input) {
  786. totalItems
  787. items {
  788. enabled
  789. productId
  790. productName
  791. productPreview
  792. productVariantId
  793. productVariantName
  794. productVariantPreview
  795. sku
  796. }
  797. }
  798. }
  799. `;
  800. export const SEARCH_GET_FACET_VALUES = gql`
  801. query SearchFacetValues($input: SearchInput!) {
  802. search(input: $input) {
  803. totalItems
  804. facetValues {
  805. count
  806. facetValue {
  807. id
  808. name
  809. }
  810. }
  811. }
  812. }
  813. `;
  814. export const SEARCH_GET_ASSETS = gql`
  815. query SearchGetAssets($input: SearchInput!) {
  816. search(input: $input) {
  817. totalItems
  818. items {
  819. productId
  820. productName
  821. productVariantName
  822. productAsset {
  823. id
  824. preview
  825. focalPoint {
  826. x
  827. y
  828. }
  829. }
  830. productVariantAsset {
  831. id
  832. preview
  833. focalPoint {
  834. x
  835. y
  836. }
  837. }
  838. }
  839. }
  840. }
  841. `;
  842. export const SEARCH_GET_PRICES = gql`
  843. query SearchGetPrices($input: SearchInput!) {
  844. search(input: $input) {
  845. items {
  846. price {
  847. ... on PriceRange {
  848. min
  849. max
  850. }
  851. ... on SinglePrice {
  852. value
  853. }
  854. }
  855. priceWithTax {
  856. ... on PriceRange {
  857. min
  858. max
  859. }
  860. ... on SinglePrice {
  861. value
  862. }
  863. }
  864. }
  865. }
  866. }
  867. `;