| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- import { Client } from '@elastic/elasticsearch';
- import { SortOrder } from '@vendure/common/lib/generated-types';
- import { SimpleGraphQLClient } from '@vendure/testing';
- import { expect } from 'vitest';
- import { SearchGetPricesQuery, SearchInput } from '../../core/e2e/graphql/generated-e2e-admin-types';
- import {
- LogicalOperator,
- SearchProductsShopQuery,
- SearchProductsShopQueryVariables,
- } from '../../core/e2e/graphql/generated-e2e-shop-types';
- import { SEARCH_PRODUCTS_SHOP } from '../../core/e2e/graphql/shop-definitions';
- import { deleteIndices } from '../src/indexing/indexing-utils';
- import { SEARCH_GET_PRICES, SEARCH_PRODUCTS } from './elasticsearch-plugin.e2e-spec';
- import {
- SearchGetPricesQueryVariables,
- SearchProductsAdminQuery,
- SearchProductsAdminQueryVariables,
- } from './graphql/generated-e2e-elasticsearch-plugin-types';
- // eslint-disable-next-line @typescript-eslint/no-var-requires
- const { elasticsearchHost, elasticsearchPort } = require('./constants');
- export function doAdminSearchQuery(client: SimpleGraphQLClient, input: SearchInput) {
- return client.query<SearchProductsAdminQuery, SearchProductsAdminQueryVariables>(SEARCH_PRODUCTS, {
- input,
- });
- }
- export async function testGroupByProduct(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: true,
- },
- },
- );
- expect(result.search.totalItems).toBe(21);
- }
- export async function testGroupBySKU(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- term: 'bonsai',
- groupBySKU: true,
- },
- },
- );
- expect(result.search.totalItems).toBe(1);
- }
- export async function testNoGrouping(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: false,
- groupBySKU: false,
- },
- },
- );
- expect(result.search.totalItems).toBe(35);
- }
- export async function testMatchSearchTerm(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- term: 'camera',
- groupByProduct: true,
- },
- },
- );
- expect(result.search.items.map(i => i.productName)).toEqual([
- 'Camera Lens',
- 'Instant Camera',
- 'SLR Camera',
- ]);
- }
- export async function testMatchFacetIdsAnd(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- facetValueIds: ['T_1', 'T_2'],
- facetValueOperator: LogicalOperator.AND,
- groupByProduct: true,
- sort: {
- name: SortOrder.ASC,
- },
- },
- },
- );
- expect(result.search.items.map(i => i.productName)).toEqual([
- 'Clacky Keyboard',
- 'Curvy Monitor',
- 'Gaming PC',
- 'Hard Drive',
- 'Laptop',
- 'USB Cable',
- ]);
- }
- export async function testMatchFacetIdsOr(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- facetValueIds: ['T_1', 'T_5'],
- facetValueOperator: LogicalOperator.OR,
- groupByProduct: true,
- sort: {
- name: SortOrder.ASC,
- },
- take: 20,
- },
- },
- );
- expect(result.search.items.map(i => i.productName)).toEqual([
- 'Bonsai Tree',
- 'Bonsai Tree (Ch2)',
- 'Camera Lens',
- 'Clacky Keyboard',
- 'Curvy Monitor',
- 'Gaming PC',
- 'Hard Drive',
- 'Instant Camera',
- 'Laptop',
- 'Orchid',
- 'SLR Camera',
- 'Spiky Cactus',
- 'Tripod',
- 'USB Cable',
- ]);
- }
- export async function testMatchFacetValueFiltersAnd(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: true,
- facetValueFilters: [{ and: 'T_1' }, { and: 'T_2' }],
- },
- },
- );
- expect(result.search.items.map(i => i.productName).sort()).toEqual(
- ['Laptop', 'Curvy Monitor', 'Gaming PC', 'Hard Drive', 'Clacky Keyboard', 'USB Cable'].sort(),
- );
- }
- export async function testMatchFacetValueFiltersOr(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: true,
- facetValueFilters: [{ or: ['T_1', 'T_5'] }],
- sort: {
- name: SortOrder.ASC,
- },
- take: 20,
- },
- },
- );
- expect(result.search.items.map(i => i.productName).sort()).toEqual(
- [
- 'Bonsai Tree',
- 'Bonsai Tree (Ch2)',
- 'Camera Lens',
- 'Clacky Keyboard',
- 'Curvy Monitor',
- 'Gaming PC',
- 'Hard Drive',
- 'Instant Camera',
- 'Laptop',
- 'Orchid',
- 'SLR Camera',
- 'Spiky Cactus',
- 'Tripod',
- 'USB Cable',
- ].sort(),
- );
- }
- export async function testMatchFacetValueFiltersOrWithAnd(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: true,
- facetValueFilters: [{ and: 'T_1' }, { or: ['T_2', 'T_3'] }],
- },
- },
- );
- expect(result.search.items.map(i => i.productName).sort()).toEqual(
- [
- 'Laptop',
- 'Curvy Monitor',
- 'Gaming PC',
- 'Hard Drive',
- 'Clacky Keyboard',
- 'USB Cable',
- 'Instant Camera',
- 'Camera Lens',
- 'Tripod',
- 'SLR Camera',
- ].sort(),
- );
- }
- export async function testMatchFacetValueFiltersWithFacetIdsOr(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- facetValueIds: ['T_2', 'T_3'],
- facetValueOperator: LogicalOperator.OR,
- facetValueFilters: [{ and: 'T_1' }],
- groupByProduct: true,
- },
- },
- );
- expect(result.search.items.map(i => i.productName).sort()).toEqual(
- [
- 'Laptop',
- 'Curvy Monitor',
- 'Gaming PC',
- 'Hard Drive',
- 'Clacky Keyboard',
- 'USB Cable',
- 'Instant Camera',
- 'Camera Lens',
- 'Tripod',
- 'SLR Camera',
- ].sort(),
- );
- }
- export async function testMatchFacetValueFiltersWithFacetIdsAnd(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- facetValueIds: ['T_1'],
- facetValueFilters: [{ and: 'T_3' }],
- facetValueOperator: LogicalOperator.AND,
- groupByProduct: true,
- },
- },
- );
- expect(result.search.items.map(i => i.productName).sort()).toEqual(
- ['Instant Camera', 'Camera Lens', 'Tripod', 'SLR Camera'].sort(),
- );
- }
- export async function testMatchCollectionId(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- collectionId: 'T_2',
- groupByProduct: true,
- },
- },
- );
- expect(result.search.items.map(i => i.productName).sort()).toEqual([
- 'Bonsai Tree',
- 'Bonsai Tree (Ch2)',
- 'Orchid',
- 'Spiky Cactus',
- ]);
- }
- export async function testMatchCollectionSlug(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- collectionSlug: 'plants',
- groupByProduct: true,
- },
- },
- );
- expect(result.search.items.map(i => i.productName).sort()).toEqual([
- 'Bonsai Tree',
- 'Bonsai Tree (Ch2)',
- 'Orchid',
- 'Spiky Cactus',
- ]);
- }
- export async function testSinglePrices(client: SimpleGraphQLClient) {
- const result = await client.query<SearchGetPricesQuery, SearchGetPricesQueryVariables>(
- SEARCH_GET_PRICES,
- {
- input: {
- groupByProduct: false,
- take: 3,
- sort: {
- price: SortOrder.ASC,
- },
- },
- },
- );
- expect(result.search.items).toEqual([
- {
- price: { value: 799 },
- priceWithTax: { value: 959 },
- },
- {
- price: { value: 1498 },
- priceWithTax: { value: 1798 },
- },
- {
- price: { value: 1550 },
- priceWithTax: { value: 1860 },
- },
- ]);
- }
- export async function testPriceRanges(client: SimpleGraphQLClient) {
- const result = await client.query<SearchGetPricesQuery, SearchGetPricesQueryVariables>(
- SEARCH_GET_PRICES,
- {
- input: {
- groupByProduct: true,
- take: 3,
- term: 'laptop',
- },
- },
- );
- expect(result.search.items).toEqual([
- {
- price: { min: 129900, max: 229900 },
- priceWithTax: { min: 155880, max: 275880 },
- },
- ]);
- }
- export async function dropElasticIndices(indexPrefix: string) {
- const esClient = new Client({
- node: `${elasticsearchHost as string}:${elasticsearchPort as string}`,
- });
- return deleteIndices(esClient, indexPrefix);
- }
|