e2e-helpers.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import { Client } from '@elastic/elasticsearch';
  2. import { SortOrder } from '@vendure/common/lib/generated-types';
  3. import { SimpleGraphQLClient } from '@vendure/testing';
  4. import { expect } from 'vitest';
  5. import { SearchGetPricesQuery, SearchInput } from '../../core/e2e/graphql/generated-e2e-admin-types';
  6. import {
  7. LogicalOperator,
  8. SearchProductsShopQuery,
  9. SearchProductsShopQueryVariables,
  10. } from '../../core/e2e/graphql/generated-e2e-shop-types';
  11. import { SEARCH_PRODUCTS_SHOP } from '../../core/e2e/graphql/shop-definitions';
  12. import { deleteIndices } from '../src/indexing/indexing-utils';
  13. import { SEARCH_GET_PRICES, SEARCH_PRODUCTS } from './elasticsearch-plugin.e2e-spec';
  14. import {
  15. SearchGetPricesQueryVariables,
  16. SearchProductsAdminQuery,
  17. SearchProductsAdminQueryVariables,
  18. } from './graphql/generated-e2e-elasticsearch-plugin-types';
  19. // tslint:disable-next-line:no-var-requires
  20. const { elasticsearchHost, elasticsearchPort } = require('./constants');
  21. export function doAdminSearchQuery(client: SimpleGraphQLClient, input: SearchInput) {
  22. return client.query<SearchProductsAdminQuery, SearchProductsAdminQueryVariables>(SEARCH_PRODUCTS, {
  23. input,
  24. });
  25. }
  26. export async function testGroupByProduct(client: SimpleGraphQLClient) {
  27. const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  28. SEARCH_PRODUCTS_SHOP,
  29. {
  30. input: {
  31. groupByProduct: true,
  32. },
  33. },
  34. );
  35. expect(result.search.totalItems).toBe(20);
  36. }
  37. export async function testNoGrouping(client: SimpleGraphQLClient) {
  38. const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  39. SEARCH_PRODUCTS_SHOP,
  40. {
  41. input: {
  42. groupByProduct: false,
  43. },
  44. },
  45. );
  46. expect(result.search.totalItems).toBe(34);
  47. }
  48. export async function testMatchSearchTerm(client: SimpleGraphQLClient) {
  49. const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  50. SEARCH_PRODUCTS_SHOP,
  51. {
  52. input: {
  53. term: 'camera',
  54. groupByProduct: true,
  55. },
  56. },
  57. );
  58. expect(result.search.items.map(i => i.productName)).toEqual([
  59. 'Instant Camera',
  60. 'Camera Lens',
  61. 'SLR Camera',
  62. ]);
  63. }
  64. export async function testMatchFacetIdsAnd(client: SimpleGraphQLClient) {
  65. const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  66. SEARCH_PRODUCTS_SHOP,
  67. {
  68. input: {
  69. facetValueIds: ['T_1', 'T_2'],
  70. facetValueOperator: LogicalOperator.AND,
  71. groupByProduct: true,
  72. sort: {
  73. name: SortOrder.ASC,
  74. },
  75. },
  76. },
  77. );
  78. expect(result.search.items.map(i => i.productName)).toEqual([
  79. 'Clacky Keyboard',
  80. 'Curvy Monitor',
  81. 'Gaming PC',
  82. 'Hard Drive',
  83. 'Laptop',
  84. 'USB Cable',
  85. ]);
  86. }
  87. export async function testMatchFacetIdsOr(client: SimpleGraphQLClient) {
  88. const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  89. SEARCH_PRODUCTS_SHOP,
  90. {
  91. input: {
  92. facetValueIds: ['T_1', 'T_5'],
  93. facetValueOperator: LogicalOperator.OR,
  94. groupByProduct: true,
  95. sort: {
  96. name: SortOrder.ASC,
  97. },
  98. take: 20,
  99. },
  100. },
  101. );
  102. expect(result.search.items.map(i => i.productName)).toEqual([
  103. 'Bonsai Tree',
  104. 'Camera Lens',
  105. 'Clacky Keyboard',
  106. 'Curvy Monitor',
  107. 'Gaming PC',
  108. 'Hard Drive',
  109. 'Instant Camera',
  110. 'Laptop',
  111. 'Orchid',
  112. 'SLR Camera',
  113. 'Spiky Cactus',
  114. 'Tripod',
  115. 'USB Cable',
  116. ]);
  117. }
  118. export async function testMatchFacetValueFiltersAnd(client: SimpleGraphQLClient) {
  119. const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  120. SEARCH_PRODUCTS_SHOP,
  121. {
  122. input: {
  123. groupByProduct: true,
  124. facetValueFilters: [{ and: 'T_1' }, { and: 'T_2' }],
  125. },
  126. },
  127. );
  128. expect(result.search.items.map(i => i.productName).sort()).toEqual(
  129. ['Laptop', 'Curvy Monitor', 'Gaming PC', 'Hard Drive', 'Clacky Keyboard', 'USB Cable'].sort(),
  130. );
  131. }
  132. export async function testMatchFacetValueFiltersOr(client: SimpleGraphQLClient) {
  133. const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  134. SEARCH_PRODUCTS_SHOP,
  135. {
  136. input: {
  137. groupByProduct: true,
  138. facetValueFilters: [{ or: ['T_1', 'T_5'] }],
  139. sort: {
  140. name: SortOrder.ASC,
  141. },
  142. take: 20,
  143. },
  144. },
  145. );
  146. expect(result.search.items.map(i => i.productName).sort()).toEqual(
  147. [
  148. 'Bonsai Tree',
  149. 'Camera Lens',
  150. 'Clacky Keyboard',
  151. 'Curvy Monitor',
  152. 'Gaming PC',
  153. 'Hard Drive',
  154. 'Instant Camera',
  155. 'Laptop',
  156. 'Orchid',
  157. 'SLR Camera',
  158. 'Spiky Cactus',
  159. 'Tripod',
  160. 'USB Cable',
  161. ].sort(),
  162. );
  163. }
  164. export async function testMatchFacetValueFiltersOrWithAnd(client: SimpleGraphQLClient) {
  165. const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  166. SEARCH_PRODUCTS_SHOP,
  167. {
  168. input: {
  169. groupByProduct: true,
  170. facetValueFilters: [{ and: 'T_1' }, { or: ['T_2', 'T_3'] }],
  171. },
  172. },
  173. );
  174. expect(result.search.items.map(i => i.productName).sort()).toEqual(
  175. [
  176. 'Laptop',
  177. 'Curvy Monitor',
  178. 'Gaming PC',
  179. 'Hard Drive',
  180. 'Clacky Keyboard',
  181. 'USB Cable',
  182. 'Instant Camera',
  183. 'Camera Lens',
  184. 'Tripod',
  185. 'SLR Camera',
  186. ].sort(),
  187. );
  188. }
  189. export async function testMatchFacetValueFiltersWithFacetIdsOr(client: SimpleGraphQLClient) {
  190. const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  191. SEARCH_PRODUCTS_SHOP,
  192. {
  193. input: {
  194. facetValueIds: ['T_2', 'T_3'],
  195. facetValueOperator: LogicalOperator.OR,
  196. facetValueFilters: [{ and: 'T_1' }],
  197. groupByProduct: true,
  198. },
  199. },
  200. );
  201. expect(result.search.items.map(i => i.productName).sort()).toEqual(
  202. [
  203. 'Laptop',
  204. 'Curvy Monitor',
  205. 'Gaming PC',
  206. 'Hard Drive',
  207. 'Clacky Keyboard',
  208. 'USB Cable',
  209. 'Instant Camera',
  210. 'Camera Lens',
  211. 'Tripod',
  212. 'SLR Camera',
  213. ].sort(),
  214. );
  215. }
  216. export async function testMatchFacetValueFiltersWithFacetIdsAnd(client: SimpleGraphQLClient) {
  217. const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  218. SEARCH_PRODUCTS_SHOP,
  219. {
  220. input: {
  221. facetValueIds: ['T_1'],
  222. facetValueFilters: [{ and: 'T_3' }],
  223. facetValueOperator: LogicalOperator.AND,
  224. groupByProduct: true,
  225. },
  226. },
  227. );
  228. expect(result.search.items.map(i => i.productName).sort()).toEqual(
  229. ['Instant Camera', 'Camera Lens', 'Tripod', 'SLR Camera'].sort(),
  230. );
  231. }
  232. export async function testMatchCollectionId(client: SimpleGraphQLClient) {
  233. const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  234. SEARCH_PRODUCTS_SHOP,
  235. {
  236. input: {
  237. collectionId: 'T_2',
  238. groupByProduct: true,
  239. },
  240. },
  241. );
  242. expect(result.search.items.map(i => i.productName).sort()).toEqual([
  243. 'Bonsai Tree',
  244. 'Orchid',
  245. 'Spiky Cactus',
  246. ]);
  247. }
  248. export async function testMatchCollectionSlug(client: SimpleGraphQLClient) {
  249. const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  250. SEARCH_PRODUCTS_SHOP,
  251. {
  252. input: {
  253. collectionSlug: 'plants',
  254. groupByProduct: true,
  255. },
  256. },
  257. );
  258. expect(result.search.items.map(i => i.productName).sort()).toEqual([
  259. 'Bonsai Tree',
  260. 'Orchid',
  261. 'Spiky Cactus',
  262. ]);
  263. }
  264. export async function testSinglePrices(client: SimpleGraphQLClient) {
  265. const result = await client.query<SearchGetPricesQuery, SearchGetPricesQueryVariables>(
  266. SEARCH_GET_PRICES,
  267. {
  268. input: {
  269. groupByProduct: false,
  270. take: 3,
  271. sort: {
  272. price: SortOrder.ASC,
  273. },
  274. },
  275. },
  276. );
  277. expect(result.search.items).toEqual([
  278. {
  279. price: { value: 799 },
  280. priceWithTax: { value: 959 },
  281. },
  282. {
  283. price: { value: 1498 },
  284. priceWithTax: { value: 1798 },
  285. },
  286. {
  287. price: { value: 1550 },
  288. priceWithTax: { value: 1860 },
  289. },
  290. ]);
  291. }
  292. export async function testPriceRanges(client: SimpleGraphQLClient) {
  293. const result = await client.query<SearchGetPricesQuery, SearchGetPricesQueryVariables>(
  294. SEARCH_GET_PRICES,
  295. {
  296. input: {
  297. groupByProduct: true,
  298. take: 3,
  299. term: 'laptop',
  300. },
  301. },
  302. );
  303. expect(result.search.items).toEqual([
  304. {
  305. price: { min: 129900, max: 229900 },
  306. priceWithTax: { min: 155880, max: 275880 },
  307. },
  308. ]);
  309. }
  310. export async function dropElasticIndices(indexPrefix: string) {
  311. const esClient = new Client({
  312. node: `${elasticsearchHost}:${elasticsearchPort}`,
  313. });
  314. return deleteIndices(esClient, indexPrefix);
  315. }