1
0

e2e-helpers.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { Client } from '@elastic/elasticsearch';
  2. import { SortOrder } from '@vendure/common/lib/generated-types';
  3. import { SimpleGraphQLClient } from '@vendure/testing';
  4. import { SearchGetPrices, SearchInput } from '../../core/e2e/graphql/generated-e2e-admin-types';
  5. import { LogicalOperator, SearchProductsShop } from '../../core/e2e/graphql/generated-e2e-shop-types';
  6. import { SEARCH_PRODUCTS_SHOP } from '../../core/e2e/graphql/shop-definitions';
  7. import { deleteIndices } from '../src/indexing-utils';
  8. import { SEARCH_GET_PRICES, SEARCH_PRODUCTS } from './elasticsearch-plugin.e2e-spec';
  9. import { SearchProductsAdmin } from './graphql/generated-e2e-elasticsearch-plugin-types';
  10. // tslint:disable-next-line:no-var-requires
  11. const { elasticsearchHost, elasticsearchPort } = require('./constants');
  12. export function doAdminSearchQuery(client: SimpleGraphQLClient, input: SearchInput) {
  13. return client.query<SearchProductsAdmin.Query, SearchProductsAdmin.Variables>(SEARCH_PRODUCTS, {
  14. input,
  15. });
  16. }
  17. export async function testGroupByProduct(client: SimpleGraphQLClient) {
  18. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  19. SEARCH_PRODUCTS_SHOP,
  20. {
  21. input: {
  22. groupByProduct: true,
  23. },
  24. },
  25. );
  26. expect(result.search.totalItems).toBe(20);
  27. }
  28. export async function testNoGrouping(client: SimpleGraphQLClient) {
  29. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  30. SEARCH_PRODUCTS_SHOP,
  31. {
  32. input: {
  33. groupByProduct: false,
  34. },
  35. },
  36. );
  37. expect(result.search.totalItems).toBe(34);
  38. }
  39. export async function testMatchSearchTerm(client: SimpleGraphQLClient) {
  40. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  41. SEARCH_PRODUCTS_SHOP,
  42. {
  43. input: {
  44. term: 'camera',
  45. groupByProduct: true,
  46. },
  47. },
  48. );
  49. expect(result.search.items.map(i => i.productName)).toEqual([
  50. 'Instant Camera',
  51. 'Camera Lens',
  52. 'SLR Camera',
  53. ]);
  54. }
  55. export async function testMatchFacetIdsAnd(client: SimpleGraphQLClient) {
  56. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  57. SEARCH_PRODUCTS_SHOP,
  58. {
  59. input: {
  60. facetValueIds: ['T_1', 'T_2'],
  61. facetValueOperator: LogicalOperator.AND,
  62. groupByProduct: true,
  63. sort: {
  64. name: SortOrder.ASC,
  65. },
  66. },
  67. },
  68. );
  69. expect(result.search.items.map(i => i.productName)).toEqual([
  70. 'Clacky Keyboard',
  71. 'Curvy Monitor',
  72. 'Gaming PC',
  73. 'Hard Drive',
  74. 'Laptop',
  75. 'USB Cable',
  76. ]);
  77. }
  78. export async function testMatchFacetIdsOr(client: SimpleGraphQLClient) {
  79. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  80. SEARCH_PRODUCTS_SHOP,
  81. {
  82. input: {
  83. facetValueIds: ['T_1', 'T_5'],
  84. facetValueOperator: LogicalOperator.OR,
  85. groupByProduct: true,
  86. sort: {
  87. name: SortOrder.ASC,
  88. },
  89. take: 20,
  90. },
  91. },
  92. );
  93. expect(result.search.items.map(i => i.productName)).toEqual([
  94. 'Bonsai Tree',
  95. 'Camera Lens',
  96. 'Clacky Keyboard',
  97. 'Curvy Monitor',
  98. 'Gaming PC',
  99. 'Hard Drive',
  100. 'Instant Camera',
  101. 'Laptop',
  102. 'Orchid',
  103. 'SLR Camera',
  104. 'Spiky Cactus',
  105. 'Tripod',
  106. 'USB Cable',
  107. ]);
  108. }
  109. export async function testMatchCollectionId(client: SimpleGraphQLClient) {
  110. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  111. SEARCH_PRODUCTS_SHOP,
  112. {
  113. input: {
  114. collectionId: 'T_2',
  115. groupByProduct: true,
  116. },
  117. },
  118. );
  119. expect(result.search.items.map(i => i.productName)).toEqual(['Spiky Cactus', 'Orchid', 'Bonsai Tree']);
  120. }
  121. export async function testMatchCollectionSlug(client: SimpleGraphQLClient) {
  122. const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  123. SEARCH_PRODUCTS_SHOP,
  124. {
  125. input: {
  126. collectionSlug: 'plants',
  127. groupByProduct: true,
  128. },
  129. },
  130. );
  131. expect(result.search.items.map(i => i.productName)).toEqual(['Spiky Cactus', 'Orchid', 'Bonsai Tree']);
  132. }
  133. export async function testSinglePrices(client: SimpleGraphQLClient) {
  134. const result = await client.query<SearchGetPrices.Query, SearchGetPrices.Variables>(SEARCH_GET_PRICES, {
  135. input: {
  136. groupByProduct: false,
  137. take: 3,
  138. sort: {
  139. price: SortOrder.ASC,
  140. },
  141. },
  142. });
  143. expect(result.search.items).toEqual([
  144. {
  145. price: { value: 799 },
  146. priceWithTax: { value: 959 },
  147. },
  148. {
  149. price: { value: 1498 },
  150. priceWithTax: { value: 1798 },
  151. },
  152. {
  153. price: { value: 1550 },
  154. priceWithTax: { value: 1860 },
  155. },
  156. ]);
  157. }
  158. export async function testPriceRanges(client: SimpleGraphQLClient) {
  159. const result = await client.query<SearchGetPrices.Query, SearchGetPrices.Variables>(SEARCH_GET_PRICES, {
  160. input: {
  161. groupByProduct: true,
  162. take: 3,
  163. term: 'laptop',
  164. },
  165. });
  166. expect(result.search.items).toEqual([
  167. {
  168. price: { min: 129900, max: 229900 },
  169. priceWithTax: { min: 155880, max: 275880 },
  170. },
  171. ]);
  172. }
  173. export async function dropElasticIndices(indexPrefix: string) {
  174. const esClient = new Client({
  175. node: `${elasticsearchHost}:${elasticsearchPort}`,
  176. });
  177. return deleteIndices(esClient, indexPrefix);
  178. }