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

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