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

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