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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. import { pick } from '@vendure/common/lib/pick';
  2. import gql from 'graphql-tag';
  3. import path from 'path';
  4. import { SimpleGraphQLClient } from '../mock-data/simple-graphql-client';
  5. import { facetValueCollectionFilter } from '../src/config/collection/default-collection-filters';
  6. import { DefaultSearchPlugin } from '../src/plugin/default-search-plugin/default-search-plugin';
  7. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  8. import {
  9. CreateCollection,
  10. CreateFacet,
  11. LanguageCode,
  12. SearchFacetValues,
  13. SearchGetPrices,
  14. SearchInput,
  15. UpdateCollection,
  16. UpdateProduct,
  17. UpdateProductVariants,
  18. UpdateTaxRate,
  19. } from './graphql/generated-e2e-admin-types';
  20. import { SearchProductsShop } from './graphql/generated-e2e-shop-types';
  21. import {
  22. CREATE_COLLECTION,
  23. CREATE_FACET,
  24. UPDATE_COLLECTION,
  25. UPDATE_PRODUCT,
  26. UPDATE_PRODUCT_VARIANTS,
  27. UPDATE_TAX_RATE,
  28. } from './graphql/shared-definitions';
  29. import { SEARCH_PRODUCTS_SHOP } from './graphql/shop-definitions';
  30. import { TestAdminClient, TestShopClient } from './test-client';
  31. import { TestServer } from './test-server';
  32. import { awaitRunningJobs } from './utils/await-running-jobs';
  33. describe('Default search plugin', () => {
  34. const adminClient = new TestAdminClient();
  35. const shopClient = new TestShopClient();
  36. const server = new TestServer();
  37. beforeAll(async () => {
  38. const token = await server.init(
  39. {
  40. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  41. customerCount: 1,
  42. },
  43. {
  44. plugins: [DefaultSearchPlugin],
  45. },
  46. );
  47. await adminClient.init();
  48. await shopClient.init();
  49. }, TEST_SETUP_TIMEOUT_MS);
  50. afterAll(async () => {
  51. await server.destroy();
  52. });
  53. async function testGroupByProduct(client: SimpleGraphQLClient) {
  54. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  55. SEARCH_PRODUCTS_SHOP,
  56. {
  57. input: {
  58. groupByProduct: true,
  59. },
  60. },
  61. );
  62. expect(result.search.totalItems).toBe(20);
  63. }
  64. async function testNoGrouping(client: SimpleGraphQLClient) {
  65. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  66. SEARCH_PRODUCTS_SHOP,
  67. {
  68. input: {
  69. groupByProduct: false,
  70. },
  71. },
  72. );
  73. expect(result.search.totalItems).toBe(34);
  74. }
  75. async function testMatchSearchTerm(client: SimpleGraphQLClient) {
  76. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  77. SEARCH_PRODUCTS_SHOP,
  78. {
  79. input: {
  80. term: 'camera',
  81. groupByProduct: true,
  82. },
  83. },
  84. );
  85. expect(result.search.items.map(i => i.productName)).toEqual([
  86. 'Instant Camera',
  87. 'Camera Lens',
  88. 'SLR Camera',
  89. ]);
  90. }
  91. async function testMatchFacetIds(client: SimpleGraphQLClient) {
  92. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  93. SEARCH_PRODUCTS_SHOP,
  94. {
  95. input: {
  96. facetValueIds: ['T_1', 'T_2'],
  97. groupByProduct: true,
  98. },
  99. },
  100. );
  101. expect(result.search.items.map(i => i.productName)).toEqual([
  102. 'Laptop',
  103. 'Curvy Monitor',
  104. 'Gaming PC',
  105. 'Hard Drive',
  106. 'Clacky Keyboard',
  107. 'USB Cable',
  108. ]);
  109. }
  110. async function testMatchCollectionId(client: SimpleGraphQLClient) {
  111. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  112. SEARCH_PRODUCTS_SHOP,
  113. {
  114. input: {
  115. collectionId: 'T_2',
  116. groupByProduct: true,
  117. },
  118. },
  119. );
  120. expect(result.search.items.map(i => i.productName)).toEqual([
  121. 'Spiky Cactus',
  122. 'Orchid',
  123. 'Bonsai Tree',
  124. ]);
  125. }
  126. async function testSinglePrices(client: SimpleGraphQLClient) {
  127. const result = await client.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
  128. SEARCH_GET_PRICES,
  129. {
  130. input: {
  131. groupByProduct: false,
  132. take: 3,
  133. } as SearchInput,
  134. },
  135. );
  136. expect(result.search.items).toEqual([
  137. {
  138. price: { value: 129900 },
  139. priceWithTax: { value: 155880 },
  140. },
  141. {
  142. price: { value: 139900 },
  143. priceWithTax: { value: 167880 },
  144. },
  145. {
  146. price: { value: 219900 },
  147. priceWithTax: { value: 263880 },
  148. },
  149. ]);
  150. }
  151. async function testPriceRanges(client: SimpleGraphQLClient) {
  152. const result = await client.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
  153. SEARCH_GET_PRICES,
  154. {
  155. input: {
  156. groupByProduct: true,
  157. take: 3,
  158. } as SearchInput,
  159. },
  160. );
  161. expect(result.search.items).toEqual([
  162. {
  163. price: { min: 129900, max: 229900 },
  164. priceWithTax: { min: 155880, max: 275880 },
  165. },
  166. {
  167. price: { min: 14374, max: 16994 },
  168. priceWithTax: { min: 17249, max: 20393 },
  169. },
  170. {
  171. price: { min: 93120, max: 109995 },
  172. priceWithTax: { min: 111744, max: 131994 },
  173. },
  174. ]);
  175. }
  176. describe('shop api', () => {
  177. it('group by product', () => testGroupByProduct(shopClient));
  178. it('no grouping', () => testNoGrouping(shopClient));
  179. it('matches search term', () => testMatchSearchTerm(shopClient));
  180. it('matches by facetId', () => testMatchFacetIds(shopClient));
  181. it('matches by collectionId', () => testMatchCollectionId(shopClient));
  182. it('single prices', () => testSinglePrices(shopClient));
  183. it('price ranges', () => testPriceRanges(shopClient));
  184. it('returns correct facetValues when not grouped by product', async () => {
  185. const result = await shopClient.query<SearchFacetValues.Query, SearchFacetValues.Variables>(
  186. SEARCH_GET_FACET_VALUES,
  187. {
  188. input: {
  189. groupByProduct: false,
  190. },
  191. },
  192. );
  193. expect(result.search.facetValues).toEqual([
  194. { count: 21, facetValue: { id: 'T_1', name: 'electronics' } },
  195. { count: 17, facetValue: { id: 'T_2', name: 'computers' } },
  196. { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
  197. { count: 10, facetValue: { id: 'T_4', name: 'sports equipment' } },
  198. { count: 3, facetValue: { id: 'T_5', name: 'home & garden' } },
  199. { count: 3, facetValue: { id: 'T_6', name: 'plants' } },
  200. ]);
  201. });
  202. it('returns correct facetValues when grouped by product', async () => {
  203. const result = await shopClient.query<SearchFacetValues.Query, SearchFacetValues.Variables>(
  204. SEARCH_GET_FACET_VALUES,
  205. {
  206. input: {
  207. groupByProduct: true,
  208. },
  209. },
  210. );
  211. expect(result.search.facetValues).toEqual([
  212. { count: 10, facetValue: { id: 'T_1', name: 'electronics' } },
  213. { count: 6, facetValue: { id: 'T_2', name: 'computers' } },
  214. { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
  215. { count: 7, facetValue: { id: 'T_4', name: 'sports equipment' } },
  216. { count: 3, facetValue: { id: 'T_5', name: 'home & garden' } },
  217. { count: 3, facetValue: { id: 'T_6', name: 'plants' } },
  218. ]);
  219. });
  220. it('omits facetValues of private facets', async () => {
  221. const { createFacet } = await adminClient.query<CreateFacet.Mutation, CreateFacet.Variables>(
  222. CREATE_FACET,
  223. {
  224. input: {
  225. code: 'profit-margin',
  226. isPrivate: true,
  227. translations: [{ languageCode: LanguageCode.en, name: 'Profit Margin' }],
  228. values: [
  229. {
  230. code: 'massive',
  231. translations: [{ languageCode: LanguageCode.en, name: 'massive' }],
  232. },
  233. ],
  234. },
  235. },
  236. );
  237. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  238. input: {
  239. id: 'T_2',
  240. // T_1 & T_2 are the existing facetValues (electronics & photo)
  241. facetValueIds: ['T_1', 'T_2', createFacet.values[0].id],
  242. },
  243. });
  244. const result = await shopClient.query<SearchFacetValues.Query, SearchFacetValues.Variables>(
  245. SEARCH_GET_FACET_VALUES,
  246. {
  247. input: {
  248. groupByProduct: true,
  249. },
  250. },
  251. );
  252. expect(result.search.facetValues).toEqual([
  253. { count: 10, facetValue: { id: 'T_1', name: 'electronics' } },
  254. { count: 6, facetValue: { id: 'T_2', name: 'computers' } },
  255. { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
  256. { count: 7, facetValue: { id: 'T_4', name: 'sports equipment' } },
  257. { count: 3, facetValue: { id: 'T_5', name: 'home & garden' } },
  258. { count: 3, facetValue: { id: 'T_6', name: 'plants' } },
  259. ]);
  260. });
  261. it('encodes the productId and productVariantId', async () => {
  262. const result = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  263. SEARCH_PRODUCTS_SHOP,
  264. {
  265. input: {
  266. groupByProduct: false,
  267. take: 1,
  268. },
  269. },
  270. );
  271. expect(pick(result.search.items[0], ['productId', 'productVariantId'])).toEqual({
  272. productId: 'T_1',
  273. productVariantId: 'T_1',
  274. });
  275. });
  276. it('omits results for disabled ProductVariants', async () => {
  277. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  278. UPDATE_PRODUCT_VARIANTS,
  279. {
  280. input: [{ id: 'T_3', enabled: false }],
  281. },
  282. );
  283. await awaitRunningJobs(adminClient);
  284. const result = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  285. SEARCH_PRODUCTS_SHOP,
  286. {
  287. input: {
  288. groupByProduct: false,
  289. take: 3,
  290. },
  291. },
  292. );
  293. expect(result.search.items.map(i => i.productVariantId)).toEqual(['T_1', 'T_2', 'T_4']);
  294. });
  295. it('encodes collectionIds', async () => {
  296. const result = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  297. SEARCH_PRODUCTS_SHOP,
  298. {
  299. input: {
  300. groupByProduct: false,
  301. term: 'cactus',
  302. take: 1,
  303. },
  304. },
  305. );
  306. expect(result.search.items[0].collectionIds).toEqual(['T_2']);
  307. });
  308. });
  309. describe('admin api', () => {
  310. it('group by product', () => testGroupByProduct(adminClient));
  311. it('no grouping', () => testNoGrouping(adminClient));
  312. it('matches search term', () => testMatchSearchTerm(adminClient));
  313. it('matches by facetId', () => testMatchFacetIds(adminClient));
  314. it('matches by collectionId', () => testMatchCollectionId(adminClient));
  315. it('single prices', () => testSinglePrices(adminClient));
  316. it('price ranges', () => testPriceRanges(adminClient));
  317. it('updates index when a Product is changed', async () => {
  318. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  319. input: {
  320. id: 'T_1',
  321. facetValueIds: [],
  322. },
  323. });
  324. await awaitRunningJobs(adminClient);
  325. const result = await adminClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  326. SEARCH_PRODUCTS,
  327. {
  328. input: {
  329. facetValueIds: ['T_2'],
  330. groupByProduct: true,
  331. },
  332. },
  333. );
  334. expect(result.search.items.map(i => i.productName)).toEqual([
  335. 'Curvy Monitor',
  336. 'Gaming PC',
  337. 'Hard Drive',
  338. 'Clacky Keyboard',
  339. 'USB Cable',
  340. ]);
  341. });
  342. it('updates index when a Collection is changed', async () => {
  343. await adminClient.query<UpdateCollection.Mutation, UpdateCollection.Variables>(
  344. UPDATE_COLLECTION,
  345. {
  346. input: {
  347. id: 'T_2',
  348. filters: [
  349. {
  350. code: facetValueCollectionFilter.code,
  351. arguments: [
  352. {
  353. name: 'facetValueIds',
  354. value: `["T_4"]`,
  355. type: 'facetValueIds',
  356. },
  357. {
  358. name: 'containsAny',
  359. value: `false`,
  360. type: 'boolean',
  361. },
  362. ],
  363. },
  364. ],
  365. },
  366. },
  367. );
  368. await awaitRunningJobs(adminClient);
  369. const result = await adminClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  370. SEARCH_PRODUCTS,
  371. {
  372. input: {
  373. collectionId: 'T_2',
  374. groupByProduct: true,
  375. },
  376. },
  377. );
  378. expect(result.search.items.map(i => i.productName)).toEqual([
  379. 'Road Bike',
  380. 'Skipping Rope',
  381. 'Boxing Gloves',
  382. 'Tent',
  383. 'Cruiser Skateboard',
  384. 'Football',
  385. 'Running Shoe',
  386. ]);
  387. });
  388. it('updates index when a Collection created', async () => {
  389. const { createCollection } = await adminClient.query<
  390. CreateCollection.Mutation,
  391. CreateCollection.Variables
  392. >(CREATE_COLLECTION, {
  393. input: {
  394. translations: [
  395. {
  396. languageCode: LanguageCode.en,
  397. name: 'Photo',
  398. description: '',
  399. },
  400. ],
  401. filters: [
  402. {
  403. code: facetValueCollectionFilter.code,
  404. arguments: [
  405. {
  406. name: 'facetValueIds',
  407. value: `["T_3"]`,
  408. type: 'facetValueIds',
  409. },
  410. {
  411. name: 'containsAny',
  412. value: `false`,
  413. type: 'boolean',
  414. },
  415. ],
  416. },
  417. ],
  418. },
  419. });
  420. await awaitRunningJobs(adminClient);
  421. const result = await adminClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  422. SEARCH_PRODUCTS,
  423. {
  424. input: {
  425. collectionId: createCollection.id,
  426. groupByProduct: true,
  427. },
  428. },
  429. );
  430. expect(result.search.items.map(i => i.productName)).toEqual([
  431. 'Instant Camera',
  432. 'Camera Lens',
  433. 'Tripod',
  434. 'SLR Camera',
  435. ]);
  436. });
  437. it('updates index when a taxRate is changed', async () => {
  438. await adminClient.query<UpdateTaxRate.Mutation, UpdateTaxRate.Variables>(UPDATE_TAX_RATE, {
  439. input: {
  440. // Default Channel's defaultTaxZone is Europe (id 2) and the id of the standard TaxRate
  441. // to Europe is 2.
  442. id: 'T_2',
  443. value: 50,
  444. },
  445. });
  446. await awaitRunningJobs(adminClient);
  447. const result = await adminClient.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
  448. SEARCH_GET_PRICES,
  449. {
  450. input: {
  451. groupByProduct: true,
  452. term: 'laptop',
  453. } as SearchInput,
  454. },
  455. );
  456. expect(result.search.items).toEqual([
  457. {
  458. price: { min: 129900, max: 229900 },
  459. priceWithTax: { min: 194850, max: 344850 },
  460. },
  461. ]);
  462. });
  463. it('returns disabled field when not grouped', async () => {
  464. const result = await adminClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  465. SEARCH_PRODUCTS,
  466. {
  467. input: {
  468. groupByProduct: false,
  469. take: 3,
  470. },
  471. },
  472. );
  473. expect(result.search.items.map(pick(['productVariantId', 'enabled']))).toEqual([
  474. { productVariantId: 'T_1', enabled: true },
  475. { productVariantId: 'T_2', enabled: true },
  476. { productVariantId: 'T_3', enabled: false },
  477. ]);
  478. });
  479. it('when grouped, disabled is false if at least one variant is enabled', async () => {
  480. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  481. UPDATE_PRODUCT_VARIANTS,
  482. {
  483. input: [{ id: 'T_1', enabled: false }, { id: 'T_2', enabled: false }],
  484. },
  485. );
  486. await awaitRunningJobs(adminClient);
  487. const result = await adminClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  488. SEARCH_PRODUCTS,
  489. {
  490. input: {
  491. groupByProduct: true,
  492. take: 3,
  493. },
  494. },
  495. );
  496. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  497. { productId: 'T_1', enabled: true },
  498. { productId: 'T_2', enabled: true },
  499. { productId: 'T_3', enabled: true },
  500. ]);
  501. });
  502. it('when grouped, disabled is true if all variants are disabled', async () => {
  503. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  504. UPDATE_PRODUCT_VARIANTS,
  505. {
  506. input: [{ id: 'T_4', enabled: false }],
  507. },
  508. );
  509. await awaitRunningJobs(adminClient);
  510. const result = await adminClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  511. SEARCH_PRODUCTS,
  512. {
  513. input: {
  514. groupByProduct: true,
  515. take: 3,
  516. },
  517. },
  518. );
  519. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  520. { productId: 'T_1', enabled: false },
  521. { productId: 'T_2', enabled: true },
  522. { productId: 'T_3', enabled: true },
  523. ]);
  524. });
  525. it('when grouped, disabled is true product is disabled', async () => {
  526. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  527. input: {
  528. id: 'T_3',
  529. enabled: false,
  530. },
  531. });
  532. await awaitRunningJobs(adminClient);
  533. const result = await adminClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  534. SEARCH_PRODUCTS,
  535. {
  536. input: {
  537. groupByProduct: true,
  538. take: 3,
  539. },
  540. },
  541. );
  542. expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
  543. { productId: 'T_1', enabled: false },
  544. { productId: 'T_2', enabled: true },
  545. { productId: 'T_3', enabled: false },
  546. ]);
  547. });
  548. });
  549. });
  550. export const SEARCH_PRODUCTS = gql`
  551. query SearchProductsAdmin($input: SearchInput!) {
  552. search(input: $input) {
  553. totalItems
  554. items {
  555. enabled
  556. productId
  557. productName
  558. productPreview
  559. productVariantId
  560. productVariantName
  561. productVariantPreview
  562. sku
  563. }
  564. }
  565. }
  566. `;
  567. export const SEARCH_GET_FACET_VALUES = gql`
  568. query SearchFacetValues($input: SearchInput!) {
  569. search(input: $input) {
  570. totalItems
  571. facetValues {
  572. count
  573. facetValue {
  574. id
  575. name
  576. }
  577. }
  578. }
  579. }
  580. `;
  581. export const SEARCH_GET_PRICES = gql`
  582. query SearchGetPrices($input: SearchInput!) {
  583. search(input: $input) {
  584. items {
  585. price {
  586. ... on PriceRange {
  587. min
  588. max
  589. }
  590. ... on SinglePrice {
  591. value
  592. }
  593. }
  594. priceWithTax {
  595. ... on PriceRange {
  596. min
  597. max
  598. }
  599. ... on SinglePrice {
  600. value
  601. }
  602. }
  603. }
  604. }
  605. }
  606. `;