| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- import path from 'path';
- import {
- CREATE_COLLECTION,
- UPDATE_COLLECTION,
- } from '../../admin-ui/src/app/data/definitions/collection-definitions';
- import { SEARCH_PRODUCTS, UPDATE_PRODUCT } from '../../admin-ui/src/app/data/definitions/product-definitions';
- import {
- ConfigArgType,
- CreateCollection,
- LanguageCode,
- SearchProducts,
- UpdateCollection,
- UpdateProduct,
- } from '../../shared/generated-types';
- import { SimpleGraphQLClient } from '../mock-data/simple-graphql-client';
- import { facetValueCollectionFilter } from '../src/config/collection/default-collection-filters';
- import { DefaultSearchPlugin } from '../src/plugin/default-search-plugin/default-search-plugin';
- import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
- import { TestAdminClient, TestShopClient } from './test-client';
- import { TestServer } from './test-server';
- describe('Default search plugin', () => {
- const adminClient = new TestAdminClient();
- const shopClient = new TestShopClient();
- const server = new TestServer();
- beforeAll(async () => {
- const token = await server.init(
- {
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
- customerCount: 1,
- },
- {
- plugins: [new DefaultSearchPlugin()],
- },
- );
- await adminClient.init();
- await shopClient.init();
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- async function testGroupByProduct(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
- input: {
- groupByProduct: true,
- },
- });
- expect(result.search.totalItems).toBe(20);
- }
- async function testNoGrouping(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
- input: {
- groupByProduct: false,
- },
- });
- expect(result.search.totalItems).toBe(34);
- }
- async function testMatchSearchTerm(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
- input: {
- term: 'camera',
- groupByProduct: true,
- },
- });
- expect(result.search.items.map(i => i.productName)).toEqual([
- 'Instant Camera',
- 'Camera Lens',
- 'SLR Camera',
- ]);
- }
- async function testMatchFacetIds(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
- input: {
- facetIds: ['T_1', 'T_2'],
- groupByProduct: true,
- },
- });
- expect(result.search.items.map(i => i.productName)).toEqual([
- 'Laptop',
- 'Curvy Monitor',
- 'Gaming PC',
- 'Hard Drive',
- 'Clacky Keyboard',
- 'USB Cable',
- ]);
- }
- async function testMatchCollectionId(client: SimpleGraphQLClient) {
- const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
- input: {
- collectionId: 'T_2',
- groupByProduct: true,
- },
- });
- expect(result.search.items.map(i => i.productName)).toEqual([
- 'Spiky Cactus',
- 'Orchid',
- 'Bonsai Tree',
- ]);
- }
- describe('shop api', () => {
- it('group by product', () => testGroupByProduct(shopClient));
- it('no grouping', () => testNoGrouping(shopClient));
- it('matches search term', () => testMatchSearchTerm(shopClient));
- it('matches by facetId', () => testMatchFacetIds(shopClient));
- it('matches by collectionId', () => testMatchCollectionId(shopClient));
- });
- describe('admin api', () => {
- it('group by product', () => testGroupByProduct(adminClient));
- it('no grouping', () => testNoGrouping(adminClient));
- it('matches search term', () => testMatchSearchTerm(adminClient));
- it('matches by facetId', () => testMatchFacetIds(adminClient));
- it('matches by collectionId', () => testMatchCollectionId(adminClient));
- it('updates index when a Product is changed', async () => {
- await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
- input: {
- id: 'T_1',
- facetValueIds: [],
- },
- });
- const result = await adminClient.query<SearchProducts.Query, SearchProducts.Variables>(
- SEARCH_PRODUCTS,
- {
- input: {
- facetIds: ['T_2'],
- groupByProduct: true,
- },
- },
- );
- expect(result.search.items.map(i => i.productName)).toEqual([
- 'Curvy Monitor',
- 'Gaming PC',
- 'Hard Drive',
- 'Clacky Keyboard',
- 'USB Cable',
- ]);
- });
- it('updates index when a Collection is changed', async () => {
- await adminClient.query<UpdateCollection.Mutation, UpdateCollection.Variables>(
- UPDATE_COLLECTION,
- {
- input: {
- id: 'T_2',
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["T_4"]`,
- type: ConfigArgType.FACET_VALUE_IDS,
- },
- ],
- },
- ],
- },
- },
- );
- const result = await adminClient.query<SearchProducts.Query, SearchProducts.Variables>(
- SEARCH_PRODUCTS,
- {
- input: {
- collectionId: 'T_2',
- groupByProduct: true,
- },
- },
- );
- expect(result.search.items.map(i => i.productName)).toEqual([
- 'Road Bike',
- 'Skipping Rope',
- 'Boxing Gloves',
- 'Tent',
- 'Cruiser Skateboard',
- 'Football',
- 'Running Shoe',
- ]);
- });
- it('updates index when a Collection created', async () => {
- const { createCollection } = await adminClient.query<
- CreateCollection.Mutation,
- CreateCollection.Variables
- >(CREATE_COLLECTION, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Photo',
- description: '',
- },
- ],
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["T_3"]`,
- type: ConfigArgType.FACET_VALUE_IDS,
- },
- ],
- },
- ],
- },
- });
- const result = await adminClient.query<SearchProducts.Query, SearchProducts.Variables>(
- SEARCH_PRODUCTS,
- {
- input: {
- collectionId: createCollection.id,
- groupByProduct: true,
- },
- },
- );
- expect(result.search.items.map(i => i.productName)).toEqual([
- 'Instant Camera',
- 'Camera Lens',
- 'Tripod',
- 'SLR Camera',
- ]);
- });
- });
- });
|