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

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