| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680 |
- /* eslint-disable @typescript-eslint/no-non-null-assertion, no-console */
- import { CurrencyCode, SortOrder } from '@vendure/common/lib/generated-types';
- import { pick } from '@vendure/common/lib/pick';
- import {
- DefaultJobQueuePlugin,
- FacetValue,
- facetValueCollectionFilter,
- LanguageCode,
- mergeConfig,
- } from '@vendure/core';
- import { createTestEnvironment, E2E_DEFAULT_CHANNEL_TOKEN } from '@vendure/testing';
- import { fail } from 'assert';
- import gql from 'graphql-tag';
- import path from 'path';
- import { afterAll, beforeAll, describe, expect, it } from 'vitest';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
- import * as Codegen from '../../core/e2e/graphql/generated-e2e-admin-types';
- import {
- SearchProductsShopQuery,
- SearchProductsShopQueryVariables,
- } from '../../core/e2e/graphql/generated-e2e-shop-types';
- import {
- ASSIGN_PRODUCT_TO_CHANNEL,
- ASSIGN_PRODUCTVARIANT_TO_CHANNEL,
- CREATE_CHANNEL,
- CREATE_COLLECTION,
- CREATE_FACET,
- CREATE_PRODUCT,
- CREATE_PRODUCT_VARIANTS,
- DELETE_ASSET,
- DELETE_PRODUCT,
- DELETE_PRODUCT_VARIANT,
- REMOVE_PRODUCT_FROM_CHANNEL,
- REMOVE_PRODUCTVARIANT_FROM_CHANNEL,
- UPDATE_ASSET,
- UPDATE_COLLECTION,
- UPDATE_PRODUCT,
- UPDATE_PRODUCT_VARIANTS,
- UPDATE_TAX_RATE,
- } from '../../core/e2e/graphql/shared-definitions';
- import { SEARCH_PRODUCTS_SHOP } from '../../core/e2e/graphql/shop-definitions';
- import { awaitRunningJobs } from '../../core/e2e/utils/await-running-jobs';
- import { ElasticsearchPlugin } from '../src/plugin';
- import {
- doAdminSearchQuery,
- dropElasticIndices,
- testGroupByProduct,
- testGroupBySKU,
- testMatchCollectionId,
- testMatchCollectionSlug,
- testMatchFacetIdsAnd,
- testMatchFacetIdsOr,
- testMatchFacetValueFiltersAnd,
- testMatchFacetValueFiltersOr,
- testMatchFacetValueFiltersOrWithAnd,
- testMatchFacetValueFiltersWithFacetIdsAnd,
- testMatchFacetValueFiltersWithFacetIdsOr,
- testMatchSearchTerm,
- testNoGrouping,
- testPriceRanges,
- testSinglePrices,
- } from './e2e-helpers';
- import {
- GetJobInfoQuery,
- GetJobInfoQueryVariables,
- JobState,
- } from './graphql/generated-e2e-elasticsearch-plugin-types';
- // eslint-disable-next-line @typescript-eslint/no-var-requires
- const { elasticsearchHost, elasticsearchPort } = require('./constants');
- interface SearchProductShopVariables extends SearchProductsShopQueryVariables {
- input: SearchProductsShopQueryVariables['input'] & {
- // This input field is dynamically added only when the `indexStockStatus` init option
- // of DefaultSearchPlugin is set to `true`, and therefore not included in the generated type. Therefore
- // we need to manually patch it here.
- inStock?: boolean;
- };
- }
- const INDEX_PREFIX = 'e2e-tests';
- describe('Elasticsearch plugin', () => {
- const { server, adminClient, shopClient } = createTestEnvironment(
- mergeConfig(testConfig(), {
- customFields: {
- ProductVariant: [
- {
- name: 'material',
- type: 'relation',
- entity: FacetValue,
- },
- ],
- },
- plugins: [
- ElasticsearchPlugin.init({
- indexPrefix: INDEX_PREFIX,
- port: elasticsearchPort,
- host: elasticsearchHost,
- hydrateProductVariantRelations: ['customFields.material', 'stockLevels'],
- customProductVariantMappings: {
- inStock: {
- graphQlType: 'Boolean!',
- valueFn: variant => {
- return variant.stockLevels[0]?.stockOnHand > 0;
- },
- },
- materialCode: {
- graphQlType: 'String!',
- valueFn: variant => {
- const materialFacetValue: FacetValue | undefined = (
- variant.customFields as any
- ).material;
- let result = '';
- if (materialFacetValue) {
- result = `${materialFacetValue.code}`;
- }
- return result;
- },
- },
- },
- customProductMappings: {
- answer: {
- graphQlType: 'Int!',
- valueFn: args => {
- return 42;
- },
- },
- hello: {
- graphQlType: 'String!',
- public: false,
- valueFn: args => {
- return 'World';
- },
- },
- priority: {
- graphQlType: 'Int!',
- valueFn: args => {
- return ((args.id as number) % 2) + 1; // only 1 or 2
- },
- },
- },
- searchConfig: {
- scriptFields: {
- answerMultiplied: {
- graphQlType: 'Int!',
- context: 'product',
- scriptFn: input => {
- const factor = input.factor ?? 2;
- return { script: `doc['product-answer'].value * ${factor as string}` };
- },
- },
- },
- mapSort: (sort, input) => {
- const priority = (input.sort as any)?.priority;
- if (priority) {
- return [
- ...sort,
- {
- ['product-priority']: {
- order: priority === SortOrder.ASC ? 'asc' : 'desc',
- },
- },
- ];
- }
- return sort;
- },
- },
- extendSearchInputType: {
- factor: 'Int',
- },
- extendSearchSortType: ['priority'],
- }),
- DefaultJobQueuePlugin,
- ],
- }),
- );
- beforeAll(async () => {
- await dropElasticIndices(INDEX_PREFIX);
- await server.init({
- initialData,
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
- customerCount: 1,
- });
- await adminClient.asSuperAdmin();
- // We have extra time here because a lot of jobs are
- // triggered from all the product updates
- await awaitRunningJobs(adminClient, 10_000, 1000);
- await adminClient.query(REINDEX);
- await awaitRunningJobs(adminClient);
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await awaitRunningJobs(adminClient);
- await server.destroy();
- });
- describe('shop api', () => {
- it('group by product', () => testGroupByProduct(shopClient));
- it('group by SKU', () => testGroupBySKU(shopClient));
- it('no grouping', () => testNoGrouping(shopClient));
- it('matches search term', () => testMatchSearchTerm(shopClient));
- it('matches by facetValueId with AND operator', () => testMatchFacetIdsAnd(shopClient));
- it('matches by facetValueId with OR operator', () => testMatchFacetIdsOr(shopClient));
- it('matches by FacetValueFilters AND', () => testMatchFacetValueFiltersAnd(shopClient));
- it('matches by FacetValueFilters OR', () => testMatchFacetValueFiltersOr(shopClient));
- it('matches by FacetValueFilters OR and AND', () => testMatchFacetValueFiltersOrWithAnd(shopClient));
- it('matches by FacetValueFilters with facetId OR operator', () =>
- testMatchFacetValueFiltersWithFacetIdsOr(shopClient));
- it('matches by FacetValueFilters with facetId AND operator', () =>
- testMatchFacetValueFiltersWithFacetIdsAnd(shopClient));
- it('matches by collectionId', () => testMatchCollectionId(shopClient));
- it('matches by collectionSlug', () => testMatchCollectionSlug(shopClient));
- it('single prices', () => testSinglePrices(shopClient));
- it('price ranges', () => testPriceRanges(shopClient));
- it('returns correct facetValues when not grouped by product', async () => {
- const result = await shopClient.query<
- Codegen.SearchFacetValuesQuery,
- Codegen.SearchFacetValuesQueryVariables
- >(SEARCH_GET_FACET_VALUES, {
- input: {
- groupByProduct: false,
- },
- });
- expect(result.search.facetValues).toEqual([
- { count: 21, facetValue: { id: 'T_1', name: 'electronics' } },
- { count: 17, facetValue: { id: 'T_2', name: 'computers' } },
- { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
- { count: 10, facetValue: { id: 'T_4', name: 'sports equipment' } },
- { count: 4, facetValue: { id: 'T_5', name: 'home & garden' } },
- { count: 4, facetValue: { id: 'T_6', name: 'plants' } },
- ]);
- });
- it('returns correct facetValues when grouped by product', async () => {
- const result = await shopClient.query<
- Codegen.SearchFacetValuesQuery,
- Codegen.SearchFacetValuesQueryVariables
- >(SEARCH_GET_FACET_VALUES, {
- input: {
- groupByProduct: true,
- },
- });
- expect(result.search.facetValues).toEqual([
- { count: 10, facetValue: { id: 'T_1', name: 'electronics' } },
- { count: 6, facetValue: { id: 'T_2', name: 'computers' } },
- { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
- { count: 7, facetValue: { id: 'T_4', name: 'sports equipment' } },
- { count: 4, facetValue: { id: 'T_5', name: 'home & garden' } },
- { count: 4, facetValue: { id: 'T_6', name: 'plants' } },
- ]);
- });
- it('omits facetValues of private facets', async () => {
- const { createFacet } = await adminClient.query<
- Codegen.CreateFacetMutation,
- Codegen.CreateFacetMutationVariables
- >(CREATE_FACET, {
- input: {
- code: 'profit-margin',
- isPrivate: true,
- translations: [{ languageCode: LanguageCode.en, name: 'Profit Margin' }],
- values: [
- {
- code: 'massive',
- translations: [{ languageCode: LanguageCode.en, name: 'massive' }],
- },
- ],
- },
- });
- await adminClient.query<Codegen.UpdateProductMutation, Codegen.UpdateProductMutationVariables>(
- UPDATE_PRODUCT,
- {
- input: {
- id: 'T_2',
- // T_1 & T_2 are the existing facetValues (electronics & photo)
- facetValueIds: ['T_1', 'T_2', createFacet.values[0].id],
- },
- },
- );
- await awaitRunningJobs(adminClient);
- const result = await shopClient.query<
- Codegen.SearchFacetValuesQuery,
- Codegen.SearchFacetValuesQueryVariables
- >(SEARCH_GET_FACET_VALUES, {
- input: {
- groupByProduct: true,
- },
- });
- expect(result.search.facetValues).toEqual([
- { count: 10, facetValue: { id: 'T_1', name: 'electronics' } },
- { count: 6, facetValue: { id: 'T_2', name: 'computers' } },
- { count: 4, facetValue: { id: 'T_3', name: 'photo' } },
- { count: 7, facetValue: { id: 'T_4', name: 'sports equipment' } },
- { count: 4, facetValue: { id: 'T_5', name: 'home & garden' } },
- { count: 4, facetValue: { id: 'T_6', name: 'plants' } },
- ]);
- });
- it('returns correct collections when not grouped by product', async () => {
- const result = await shopClient.query<
- Codegen.SearchCollectionsQuery,
- Codegen.SearchCollectionsQueryVariables
- >(SEARCH_GET_COLLECTIONS, {
- input: {
- groupByProduct: false,
- },
- });
- expect(result.search.collections).toEqual([
- { collection: { id: 'T_2', name: 'Plants' }, count: 4 },
- ]);
- });
- it('returns correct collections when grouped by product', async () => {
- const result = await shopClient.query<
- Codegen.SearchCollectionsQuery,
- Codegen.SearchCollectionsQueryVariables
- >(SEARCH_GET_COLLECTIONS, {
- input: {
- groupByProduct: true,
- },
- });
- expect(result.search.collections).toEqual([
- { collection: { id: 'T_2', name: 'Plants' }, count: 4 },
- ]);
- });
- it('encodes the productId and productVariantId', async () => {
- const result = await shopClient.query<SearchProductsShopQuery, SearchProductShopVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- term: 'Laptop 13 inch 8GB',
- groupByProduct: false,
- take: 1,
- },
- },
- );
- expect(pick(result.search.items[0], ['productId', 'productVariantId'])).toEqual({
- productId: 'T_1',
- productVariantId: 'T_1',
- });
- });
- it('omits results for disabled ProductVariants', async () => {
- await adminClient.query<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [{ id: 'T_3', enabled: false }],
- });
- await awaitRunningJobs(adminClient);
- const result = await shopClient.query<SearchProductsShopQuery, SearchProductShopVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: false,
- take: 100,
- },
- },
- );
- expect(result.search.items.map(i => i.productVariantId).includes('T_3')).toBe(false);
- });
- it('encodes collectionIds', async () => {
- const result = await shopClient.query<SearchProductsShopQuery, SearchProductShopVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: false,
- term: 'cactus',
- take: 1,
- },
- },
- );
- expect(result.search.items[0].collectionIds).toEqual(['T_2']);
- });
- it('inStock is false and not grouped by product', async () => {
- const result = await shopClient.query<SearchProductsShopQuery, SearchProductShopVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: false,
- inStock: false,
- },
- },
- );
- expect(result.search.totalItems).toBe(3);
- });
- it('inStock is false and grouped by product', async () => {
- const result = await shopClient.query<SearchProductsShopQuery, SearchProductShopVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: true,
- inStock: false,
- },
- },
- );
- expect(result.search.totalItems).toBe(2);
- });
- it('inStock is true and not grouped by product', async () => {
- const result = await shopClient.query<SearchProductsShopQuery, SearchProductShopVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: false,
- inStock: true,
- },
- },
- );
- expect(result.search.totalItems).toBe(31);
- });
- it('inStock is true and grouped by product', async () => {
- const result = await shopClient.query<SearchProductsShopQuery, SearchProductShopVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: true,
- inStock: true,
- },
- },
- );
- expect(result.search.totalItems).toBe(19);
- });
- it('inStock is undefined and not grouped by product', async () => {
- const result = await shopClient.query<SearchProductsShopQuery, SearchProductShopVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: false,
- inStock: undefined,
- },
- },
- );
- expect(result.search.totalItems).toBe(34);
- });
- it('inStock is undefined and grouped by product', async () => {
- const result = await shopClient.query<SearchProductsShopQuery, SearchProductShopVariables>(
- SEARCH_PRODUCTS_SHOP,
- {
- input: {
- groupByProduct: true,
- inStock: undefined,
- },
- },
- );
- expect(result.search.totalItems).toBe(21);
- });
- });
- describe('admin api', () => {
- it('group by product', () => testGroupByProduct(adminClient));
- it('group by SKU', () => testGroupBySKU(adminClient));
- it('no grouping', () => testNoGrouping(adminClient));
- it('matches search term', () => testMatchSearchTerm(adminClient));
- it('matches by facetValueId with AND operator', () => testMatchFacetIdsAnd(adminClient));
- it('matches by facetValueId with OR operator', () => testMatchFacetIdsOr(adminClient));
- it('matches by FacetValueFilters AND', () => testMatchFacetValueFiltersAnd(shopClient));
- it('matches by FacetValueFilters OR', () => testMatchFacetValueFiltersOr(shopClient));
- it('matches by FacetValueFilters OR and AND', () => testMatchFacetValueFiltersOrWithAnd(shopClient));
- it('matches by FacetValueFilters with facetId OR operator', () =>
- testMatchFacetValueFiltersWithFacetIdsOr(shopClient));
- it('matches by FacetValueFilters with facetId AND operator', () =>
- testMatchFacetValueFiltersWithFacetIdsAnd(shopClient));
- it('matches by collectionId', () => testMatchCollectionId(adminClient));
- it('matches by collectionSlug', () => testMatchCollectionSlug(adminClient));
- it('single prices', () => testSinglePrices(adminClient));
- it('price ranges', () => testPriceRanges(adminClient));
- describe('updating the index', () => {
- it('updates index when ProductVariants are changed', async () => {
- await awaitRunningJobs(adminClient);
- const { search } = await doAdminSearchQuery(adminClient, {
- term: 'drive',
- groupByProduct: false,
- });
- expect(search.items.map(i => i.sku)).toEqual([
- 'IHD455T1',
- 'IHD455T2',
- 'IHD455T3',
- 'IHD455T4',
- 'IHD455T6',
- ]);
- await adminClient.query<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: search.items.map(i => ({
- id: i.productVariantId,
- sku: i.sku + '_updated',
- })),
- });
- await awaitRunningJobs(adminClient);
- const { search: search2 } = await doAdminSearchQuery(adminClient, {
- term: 'drive',
- groupByProduct: false,
- });
- expect(search2.items.map(i => i.sku)).toEqual([
- 'IHD455T1_updated',
- 'IHD455T2_updated',
- 'IHD455T3_updated',
- 'IHD455T4_updated',
- 'IHD455T6_updated',
- ]);
- });
- it('updates index when ProductVariants are deleted', async () => {
- await awaitRunningJobs(adminClient);
- const { search } = await doAdminSearchQuery(adminClient, {
- term: 'drive',
- groupByProduct: false,
- });
- await adminClient.query<
- Codegen.DeleteProductVariantMutation,
- Codegen.DeleteProductVariantMutationVariables
- >(DELETE_PRODUCT_VARIANT, {
- id: search.items[0].productVariantId,
- });
- await awaitRunningJobs(adminClient);
- await awaitRunningJobs(adminClient);
- const { search: search2 } = await doAdminSearchQuery(adminClient, {
- term: 'drive',
- groupByProduct: false,
- });
- expect(search2.items.map(i => i.sku).sort()).toEqual([
- 'IHD455T2_updated',
- 'IHD455T3_updated',
- 'IHD455T4_updated',
- 'IHD455T6_updated',
- ]);
- });
- it('updates index when a Product is changed', async () => {
- await adminClient.query<
- Codegen.UpdateProductMutation,
- Codegen.UpdateProductMutationVariables
- >(UPDATE_PRODUCT, {
- input: {
- id: 'T_1',
- facetValueIds: [],
- },
- });
- await awaitRunningJobs(adminClient);
- const result = await doAdminSearchQuery(adminClient, {
- facetValueIds: ['T_2'],
- groupByProduct: true,
- });
- expect(result.search.items.map(i => i.productName).sort()).toEqual([
- 'Clacky Keyboard',
- 'Curvy Monitor',
- 'Gaming PC',
- 'Hard Drive',
- 'USB Cable',
- ]);
- });
- it('updates index when a Product is deleted', async () => {
- const { search } = await doAdminSearchQuery(adminClient, {
- facetValueIds: ['T_2'],
- groupByProduct: true,
- });
- expect(search.items.map(i => i.productId).sort()).toEqual([
- 'T_2',
- 'T_3',
- 'T_4',
- 'T_5',
- 'T_6',
- ]);
- await adminClient.query<
- Codegen.DeleteProductMutation,
- Codegen.DeleteProductMutationVariables
- >(DELETE_PRODUCT, {
- id: 'T_5',
- });
- await awaitRunningJobs(adminClient);
- const { search: search2 } = await doAdminSearchQuery(adminClient, {
- facetValueIds: ['T_2'],
- groupByProduct: true,
- });
- expect(search2.items.map(i => i.productId).sort()).toEqual(['T_2', 'T_3', 'T_4', 'T_6']);
- });
- it('updates index when a Collection is changed', async () => {
- await adminClient.query<
- Codegen.UpdateCollectionMutation,
- Codegen.UpdateCollectionMutationVariables
- >(UPDATE_COLLECTION, {
- input: {
- id: 'T_2',
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: '["T_4"]',
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- },
- });
- await awaitRunningJobs(adminClient);
- // add an additional check for the collection filters to update
- await awaitRunningJobs(adminClient);
- const result1 = await doAdminSearchQuery(adminClient, {
- collectionId: 'T_2',
- groupByProduct: true,
- });
- expect(result1.search.items.map(i => i.productName).sort()).toEqual([
- 'Boxing Gloves',
- 'Cruiser Skateboard',
- 'Football',
- 'Road Bike',
- 'Running Shoe',
- 'Skipping Rope',
- 'Tent',
- ]);
- const result2 = await doAdminSearchQuery(adminClient, {
- collectionSlug: 'plants',
- groupByProduct: true,
- });
- expect(result2.search.items.map(i => i.productName).sort()).toEqual([
- 'Boxing Gloves',
- 'Cruiser Skateboard',
- 'Football',
- 'Road Bike',
- 'Running Shoe',
- 'Skipping Rope',
- 'Tent',
- ]);
- });
- it('updates index when a Collection created', async () => {
- const { createCollection } = await adminClient.query<
- Codegen.CreateCollectionMutation,
- Codegen.CreateCollectionMutationVariables
- >(CREATE_COLLECTION, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Photo',
- description: '',
- slug: 'photo',
- },
- ],
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: '["T_3"]',
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- },
- });
- await awaitRunningJobs(adminClient);
- // add an additional check for the collection filters to update
- await awaitRunningJobs(adminClient);
- const result = await doAdminSearchQuery(adminClient, {
- collectionId: createCollection.id,
- groupByProduct: true,
- });
- expect(result.search.items.map(i => i.productName).sort()).toEqual([
- 'Camera Lens',
- 'Instant Camera',
- 'SLR Camera',
- 'Tripod',
- ]);
- });
- it('updates index when a taxRate is changed', async () => {
- await adminClient.query<
- Codegen.UpdateTaxRateMutation,
- Codegen.UpdateTaxRateMutationVariables
- >(UPDATE_TAX_RATE, {
- input: {
- // Default Channel's defaultTaxZone is Europe (id 2) and the id of the standard TaxRate
- // to Europe is 2.
- id: 'T_2',
- value: 50,
- },
- });
- await awaitRunningJobs(adminClient);
- const result = await adminClient.query<
- Codegen.SearchGetPricesQuery,
- Codegen.SearchGetPricesQueryVariables
- >(SEARCH_GET_PRICES, {
- input: {
- groupByProduct: true,
- term: 'laptop',
- } as Codegen.SearchInput,
- });
- expect(result.search.items).toEqual([
- {
- price: { min: 129900, max: 229900 },
- priceWithTax: { min: 194850, max: 344850 },
- },
- ]);
- });
- describe('asset changes', () => {
- function searchForLaptop() {
- return doAdminSearchQuery(adminClient, {
- term: 'laptop',
- groupByProduct: true,
- take: 1,
- sort: {
- name: SortOrder.ASC,
- },
- });
- }
- it('updates index when asset focalPoint is changed', async () => {
- const { search: search1 } = await searchForLaptop();
- expect(search1.items[0].productAsset!.id).toBe('T_1');
- expect(search1.items[0].productAsset!.focalPoint).toBeNull();
- await adminClient.query<
- Codegen.UpdateAssetMutation,
- Codegen.UpdateAssetMutationVariables
- >(UPDATE_ASSET, {
- input: {
- id: 'T_1',
- focalPoint: {
- x: 0.42,
- y: 0.42,
- },
- },
- });
- await awaitRunningJobs(adminClient);
- const { search: search2 } = await searchForLaptop();
- expect(search2.items[0].productAsset!.id).toBe('T_1');
- expect(search2.items[0].productAsset!.focalPoint).toEqual({ x: 0.42, y: 0.42 });
- });
- it('updates index when asset deleted', async () => {
- const { search: search1 } = await searchForLaptop();
- const assetId = search1.items[0].productAsset?.id;
- expect(assetId).toBeTruthy();
- await adminClient.query<
- Codegen.DeleteAssetMutation,
- Codegen.DeleteAssetMutationVariables
- >(DELETE_ASSET, {
- input: {
- assetId: assetId!,
- force: true,
- },
- });
- await awaitRunningJobs(adminClient);
- const { search: search2 } = await searchForLaptop();
- expect(search2.items[0].productAsset).toBeNull();
- });
- });
- it('does not include deleted ProductVariants in index', async () => {
- const { search: s1 } = await doAdminSearchQuery(adminClient, {
- term: 'hard drive',
- groupByProduct: false,
- });
- const variantToDelete = s1.items.find(i => i.sku === 'IHD455T2_updated')!;
- const { deleteProductVariant } = await adminClient.query<
- Codegen.DeleteProductVariantMutation,
- Codegen.DeleteProductVariantMutationVariables
- >(DELETE_PRODUCT_VARIANT, { id: variantToDelete.productVariantId });
- await awaitRunningJobs(adminClient);
- const { search } = await adminClient.query<
- Codegen.SearchGetPricesQuery,
- Codegen.SearchGetPricesQueryVariables
- >(SEARCH_GET_PRICES, { input: { term: 'hard drive', groupByProduct: true } });
- expect(search.items[0].price).toEqual({
- min: 7896,
- max: 13435,
- });
- });
- it('returns disabled field when not grouped', async () => {
- const result = await doAdminSearchQuery(adminClient, {
- groupByProduct: false,
- term: 'laptop',
- });
- expect(result.search.items.map(pick(['productVariantId', 'enabled']))).toEqual([
- { productVariantId: 'T_1', enabled: true },
- { productVariantId: 'T_2', enabled: true },
- { productVariantId: 'T_3', enabled: false },
- { productVariantId: 'T_4', enabled: true },
- ]);
- });
- it('when grouped, disabled is false if at least one variant is enabled', async () => {
- await adminClient.query<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- { id: 'T_1', enabled: false },
- { id: 'T_2', enabled: false },
- ],
- });
- await awaitRunningJobs(adminClient);
- const result = await doAdminSearchQuery(adminClient, {
- groupByProduct: true,
- term: 'laptop',
- });
- expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
- { productId: 'T_1', enabled: true },
- ]);
- });
- it('when grouped, disabled is true if all variants are disabled', async () => {
- await adminClient.query<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [{ id: 'T_4', enabled: false }],
- });
- await awaitRunningJobs(adminClient);
- const result = await doAdminSearchQuery(adminClient, {
- groupByProduct: true,
- take: 3,
- term: 'laptop',
- });
- expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
- { productId: 'T_1', enabled: false },
- ]);
- });
- it('when grouped, disabled is true product is disabled', async () => {
- await adminClient.query<
- Codegen.UpdateProductMutation,
- Codegen.UpdateProductMutationVariables
- >(UPDATE_PRODUCT, {
- input: {
- id: 'T_3',
- enabled: false,
- },
- });
- await awaitRunningJobs(adminClient);
- const result = await doAdminSearchQuery(adminClient, {
- groupByProduct: true,
- term: 'gaming',
- });
- const t3 = result.search.items.find(i => i.productId === 'T_3');
- expect(t3?.enabled).toEqual(false);
- });
- // https://github.com/vendurehq/vendure/issues/295
- it('enabled status survives reindex', async () => {
- await adminClient.query<Codegen.ReindexMutation>(REINDEX);
- await awaitRunningJobs(adminClient);
- const result = await doAdminSearchQuery(adminClient, {
- term: 'laptop',
- groupByProduct: true,
- take: 3,
- });
- expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
- { productId: 'T_1', enabled: false },
- ]);
- });
- });
- // https://github.com/vendurehq/vendure/issues/609
- describe('Synthetic index items', () => {
- let createdProductId: string;
- it('creates synthetic index item for Product with no variants', async () => {
- const { createProduct } = await adminClient.query<
- Codegen.CreateProductMutation,
- Codegen.CreateProductMutationVariables
- >(CREATE_PRODUCT, {
- input: {
- facetValueIds: ['T_1'],
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Strawberry cheesecake',
- slug: 'strawberry-cheesecake',
- description: 'A yummy dessert',
- },
- ],
- },
- });
- await awaitRunningJobs(adminClient);
- const result = await doAdminSearchQuery(adminClient, {
- groupByProduct: true,
- term: 'strawberry',
- });
- expect(
- result.search.items.map(
- pick([
- 'productId',
- 'enabled',
- 'productName',
- 'productVariantName',
- 'slug',
- 'description',
- ]),
- ),
- ).toEqual([
- {
- productId: createProduct.id,
- enabled: false,
- productName: 'Strawberry cheesecake',
- productVariantName: 'Strawberry cheesecake',
- slug: 'strawberry-cheesecake',
- description: 'A yummy dessert',
- },
- ]);
- createdProductId = createProduct.id;
- });
- it('removes synthetic index item once a variant is created', async () => {
- const { createProductVariants } = await adminClient.query<
- Codegen.CreateProductVariantsMutation,
- Codegen.CreateProductVariantsMutationVariables
- >(CREATE_PRODUCT_VARIANTS, {
- input: [
- {
- productId: createdProductId,
- sku: 'SC01',
- price: 1399,
- translations: [
- { languageCode: LanguageCode.en, name: 'Strawberry Cheesecake Pie' },
- ],
- },
- ],
- });
- await awaitRunningJobs(adminClient);
- const result = await doAdminSearchQuery(adminClient, {
- groupByProduct: true,
- term: 'strawberry',
- });
- expect(result.search.items.map(pick(['productVariantName']))).toEqual([
- { productVariantName: 'Strawberry Cheesecake Pie' },
- ]);
- });
- });
- describe('channel handling', () => {
- const SECOND_CHANNEL_TOKEN = 'second-channel-token';
- let secondChannel: Codegen.ChannelFragment;
- beforeAll(async () => {
- const { createChannel } = await adminClient.query<
- Codegen.CreateChannelMutation,
- Codegen.CreateChannelMutationVariables
- >(CREATE_CHANNEL, {
- input: {
- code: 'second-channel',
- token: SECOND_CHANNEL_TOKEN,
- defaultLanguageCode: LanguageCode.en,
- currencyCode: CurrencyCode.GBP,
- pricesIncludeTax: true,
- defaultTaxZoneId: 'T_2',
- defaultShippingZoneId: 'T_1',
- },
- });
- secondChannel = createChannel as Codegen.ChannelFragment;
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- await adminClient.query<Codegen.ReindexMutation>(REINDEX);
- await awaitRunningJobs(adminClient);
- });
- it('new channel is initially empty', async () => {
- const { search: searchGrouped } = await doAdminSearchQuery(adminClient, {
- groupByProduct: true,
- });
- const { search: searchUngrouped } = await doAdminSearchQuery(adminClient, {
- groupByProduct: false,
- });
- expect(searchGrouped.totalItems).toEqual(0);
- expect(searchUngrouped.totalItems).toEqual(0);
- });
- it('adding product to channel', async () => {
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- await adminClient.query<
- Codegen.AssignProductsToChannelMutation,
- Codegen.AssignProductsToChannelMutationVariables
- >(ASSIGN_PRODUCT_TO_CHANNEL, {
- input: { channelId: secondChannel.id, productIds: ['T_1', 'T_2'] },
- });
- await awaitRunningJobs(adminClient);
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { search } = await doAdminSearchQuery(adminClient, { groupByProduct: true });
- expect(search.items.map(i => i.productId).sort()).toEqual(['T_1', 'T_2']);
- });
- it('removing product from channel', async () => {
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const { removeProductsFromChannel } = await adminClient.query<
- Codegen.RemoveProductsFromChannelMutation,
- Codegen.RemoveProductsFromChannelMutationVariables
- >(REMOVE_PRODUCT_FROM_CHANNEL, {
- input: {
- productIds: ['T_2'],
- channelId: secondChannel.id,
- },
- });
- await awaitRunningJobs(adminClient);
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { search } = await doAdminSearchQuery(adminClient, { groupByProduct: true });
- expect(search.items.map(i => i.productId)).toEqual(['T_1']);
- });
- it('reindexes in channel', async () => {
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { reindex } = await adminClient.query<Codegen.ReindexMutation>(REINDEX);
- await awaitRunningJobs(adminClient);
- const { job } = await adminClient.query<GetJobInfoQuery, GetJobInfoQueryVariables>(
- GET_JOB_INFO,
- { id: reindex.id },
- );
- expect(job!.state).toBe(JobState.COMPLETED);
- const { search } = await doAdminSearchQuery(adminClient, { groupByProduct: true });
- expect(search.items.map(i => i.productId).sort()).toEqual(['T_1']);
- });
- it('adding product variant to channel', async () => {
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- await adminClient.query<
- Codegen.AssignProductVariantsToChannelMutation,
- Codegen.AssignProductVariantsToChannelMutationVariables
- >(ASSIGN_PRODUCTVARIANT_TO_CHANNEL, {
- input: { channelId: secondChannel.id, productVariantIds: ['T_10', 'T_15'] },
- });
- await awaitRunningJobs(adminClient);
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { search: searchGrouped } = await doAdminSearchQuery(adminClient, {
- groupByProduct: true,
- });
- expect(searchGrouped.items.map(i => i.productId).sort()).toEqual(['T_1', 'T_3', 'T_4']);
- const { search: searchUngrouped } = await doAdminSearchQuery(adminClient, {
- groupByProduct: false,
- });
- expect(searchUngrouped.items.map(i => i.productVariantId).sort()).toEqual([
- 'T_1',
- 'T_10',
- 'T_15',
- 'T_2',
- 'T_3',
- 'T_4',
- ]);
- });
- it('removing product variant from channel', async () => {
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- await adminClient.query<
- Codegen.RemoveProductVariantsFromChannelMutation,
- Codegen.RemoveProductVariantsFromChannelMutationVariables
- >(REMOVE_PRODUCTVARIANT_FROM_CHANNEL, {
- input: { channelId: secondChannel.id, productVariantIds: ['T_1', 'T_15'] },
- });
- await awaitRunningJobs(adminClient);
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { search: searchGrouped } = await doAdminSearchQuery(adminClient, {
- groupByProduct: true,
- });
- expect(searchGrouped.items.map(i => i.productId).sort()).toEqual(['T_1', 'T_3']);
- const { search: searchUngrouped } = await doAdminSearchQuery(adminClient, {
- groupByProduct: false,
- });
- expect(searchUngrouped.items.map(i => i.productVariantId).sort()).toEqual([
- 'T_10',
- 'T_2',
- 'T_3',
- 'T_4',
- ]);
- });
- it('updating product affects current channel', async () => {
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { updateProduct } = await adminClient.query<
- Codegen.UpdateProductMutation,
- Codegen.UpdateProductMutationVariables
- >(UPDATE_PRODUCT, {
- input: {
- id: 'T_3',
- enabled: true,
- translations: [{ languageCode: LanguageCode.en, name: 'xyz' }],
- },
- });
- await awaitRunningJobs(adminClient);
- const { search: searchGrouped } = await doAdminSearchQuery(adminClient, {
- groupByProduct: true,
- term: 'xyz',
- });
- expect(searchGrouped.items.map(i => i.productName)).toEqual(['xyz']);
- });
- it('updating product affects other channels', async () => {
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const { search: searchGrouped } = await doAdminSearchQuery(adminClient, {
- groupByProduct: true,
- term: 'xyz',
- });
- expect(searchGrouped.items.map(i => i.productName)).toEqual(['xyz']);
- });
- // https://github.com/vendurehq/vendure/issues/896
- it('removing from channel with multiple languages', async () => {
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- await adminClient.query<
- Codegen.UpdateProductMutation,
- Codegen.UpdateProductMutationVariables
- >(UPDATE_PRODUCT, {
- input: {
- id: 'T_4',
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'product en',
- slug: 'product-en',
- description: 'en',
- },
- {
- languageCode: LanguageCode.de,
- name: 'product de',
- slug: 'product-de',
- description: 'de',
- },
- ],
- },
- });
- await adminClient.query<
- Codegen.AssignProductsToChannelMutation,
- Codegen.AssignProductsToChannelMutationVariables
- >(ASSIGN_PRODUCT_TO_CHANNEL, {
- input: { channelId: secondChannel.id, productIds: ['T_4'] },
- });
- await awaitRunningJobs(adminClient);
- async function searchSecondChannelForDEProduct() {
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { search } = await adminClient.query<
- SearchProductsShopQuery,
- SearchProductShopVariables
- >(
- SEARCH_PRODUCTS,
- {
- input: { term: 'product', groupByProduct: true },
- },
- { languageCode: LanguageCode.de },
- );
- return search;
- }
- const search1 = await searchSecondChannelForDEProduct();
- expect(search1.items.map(i => i.productName)).toEqual(['product de']);
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const { removeProductsFromChannel } = await adminClient.query<
- Codegen.RemoveProductsFromChannelMutation,
- Codegen.RemoveProductsFromChannelMutationVariables
- >(REMOVE_PRODUCT_FROM_CHANNEL, {
- input: {
- productIds: ['T_4'],
- channelId: secondChannel.id,
- },
- });
- await awaitRunningJobs(adminClient);
- const search2 = await searchSecondChannelForDEProduct();
- expect(search2.items.map(i => i.productName)).toEqual([]);
- });
- });
- describe('multiple language handling', () => {
- function searchInLanguage(languageCode: LanguageCode, groupByProduct: boolean) {
- return adminClient.query<
- Codegen.SearchProductsAdminQuery,
- Codegen.SearchProductsAdminQueryVariables
- >(
- SEARCH_PRODUCTS,
- {
- input: {
- take: 1,
- term: 'laptop',
- groupByProduct,
- },
- },
- {
- languageCode,
- },
- );
- }
- beforeAll(async () => {
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const { updateProduct } = await adminClient.query<
- Codegen.UpdateProductMutation,
- Codegen.UpdateProductMutationVariables
- >(UPDATE_PRODUCT, {
- input: {
- id: 'T_1',
- translations: [
- {
- languageCode: LanguageCode.de,
- name: 'laptop name de',
- slug: 'laptop-slug-de',
- description: 'laptop description de',
- },
- {
- languageCode: LanguageCode.zh,
- name: 'laptop name zh',
- slug: 'laptop-slug-zh',
- description: 'laptop description zh',
- },
- ],
- },
- });
- await adminClient.query<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: updateProduct.variants[0].id,
- translations: [
- {
- languageCode: LanguageCode.fr,
- name: 'laptop variant fr',
- },
- ],
- },
- ],
- });
- await awaitRunningJobs(adminClient);
- });
- it('indexes product-level languages', async () => {
- const { search: search1 } = await searchInLanguage(LanguageCode.de, true);
- expect(search1.items[0].productName).toBe('laptop name de');
- expect(search1.items[0].slug).toBe('laptop-slug-de');
- expect(search1.items[0].description).toBe('laptop description de');
- const { search: search2 } = await searchInLanguage(LanguageCode.zh, true);
- expect(search2.items[0].productName).toBe('laptop name zh');
- expect(search2.items[0].slug).toBe('laptop-slug-zh');
- expect(search2.items[0].description).toBe('laptop description zh');
- });
- it('indexes product variant-level languages', async () => {
- const { search: search1 } = await searchInLanguage(LanguageCode.fr, false);
- expect(search1.items.length ? search1.items[0].productName : undefined).toBe('Laptop');
- expect(search1.items.length ? search1.items[0].productVariantName : undefined).toBe(
- 'laptop variant fr',
- );
- });
- });
- });
- describe('customMappings', () => {
- it('variant mappings', async () => {
- const query = `{
- search(input: { take: 1, groupByProduct: false, sort: { name: ASC } }) {
- items {
- productVariantName
- customProductVariantMappings {
- inStock
- }
- customProductMappings {
- answer
- }
- customMappings {
- ...on CustomProductVariantMappings {
- inStock
- }
- ...on CustomProductMappings {
- answer
- }
- }
- }
- }
- }`;
- const { search } = await shopClient.query(gql(query));
- expect(search.items[0]).toEqual({
- productVariantName: 'Bonsai Tree',
- customProductVariantMappings: {
- inStock: false,
- },
- customProductMappings: {
- answer: 42,
- },
- customMappings: {
- inStock: false,
- },
- });
- });
- // https://github.com/vendurehq/vendure/issues/1638
- it('hydrates variant custom field relation', async () => {
- const { updateProductVariants } = await adminClient.query<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: 'T_20',
- customFields: {
- materialId: 'T_1',
- },
- },
- ],
- });
- expect(updateProductVariants[0]!.id).toBe('T_20');
- await adminClient.query<Codegen.ReindexMutation>(REINDEX);
- await awaitRunningJobs(adminClient);
- const query = `{
- search(input: { groupByProduct: false, term: "tripod" }) {
- items {
- productVariantName
- productVariantId
- customMappings {
- ...on CustomProductVariantMappings {
- materialCode
- }
- }
- }
- }
- }`;
- const { search } = await shopClient.query(gql(query));
- expect(search.items.map((i: any) => i.customMappings)).toEqual([{ materialCode: 'electronics' }]);
- });
- it('product mappings', async () => {
- const query = `{
- search(input: { take: 1, groupByProduct: true, sort: { name: ASC } }) {
- items {
- productName
- customMappings {
- ...on CustomProductMappings {
- answer
- }
- }
- }
- }
- }`;
- const { search } = await shopClient.query(gql(query));
- expect(search.items[0]).toEqual({
- productName: 'Bonsai Tree',
- customMappings: {
- answer: 42,
- },
- });
- });
- it('private mappings', async () => {
- const query = `{
- search(input: { take: 1, groupByProduct: true, sort: { name: ASC } }) {
- items {
- customMappings {
- ...on CustomProductMappings {
- answer
- hello
- }
- }
- }
- }
- }`;
- try {
- await shopClient.query(gql(query));
- } catch (error: any) {
- expect(error).toBeDefined();
- expect(error.message).toContain('Cannot query field "hello"');
- return;
- }
- fail('should not be able to query field "hello"');
- });
- });
- describe('scriptFields', () => {
- it('script mapping', async () => {
- const query = `{
- search(input: { take: 1, groupByProduct: true, sort: { name: ASC } }) {
- items {
- productVariantName
- customScriptFields {
- answerMultiplied
- }
- }
- }
- }`;
- const { search } = await shopClient.query(gql(query));
- expect(search.items[0]).toEqual({
- productVariantName: 'Bonsai Tree',
- customScriptFields: {
- answerMultiplied: 84,
- },
- });
- });
- it('can use the custom search input field', async () => {
- const query = `{
- search(input: { take: 1, groupByProduct: true, sort: { name: ASC }, factor: 10 }) {
- items {
- productVariantName
- customScriptFields {
- answerMultiplied
- }
- }
- }
- }`;
- const { search } = await shopClient.query(gql(query));
- expect(search.items[0]).toEqual({
- productVariantName: 'Bonsai Tree',
- customScriptFields: {
- answerMultiplied: 420,
- },
- });
- });
- });
- describe('sort', () => {
- it('sort ASC', async () => {
- const query = `{
- search(input: { take: 1, groupByProduct: true, sort: { priority: ASC } }) {
- items {
- customMappings {
- ...on CustomProductMappings {
- priority
- }
- }
- }
- }
- }`;
- const { search } = await shopClient.query(gql(query));
- expect(search.items[0]).toEqual({
- customMappings: {
- priority: 1,
- },
- });
- });
- it('sort DESC', async () => {
- const query = `{
- search(input: { take: 1, groupByProduct: true, sort: { priority: DESC } }) {
- items {
- customMappings {
- ...on CustomProductMappings {
- priority
- }
- }
- }
- }
- }`;
- const { search } = await shopClient.query(gql(query));
- expect(search.items[0]).toEqual({
- customMappings: {
- priority: 2,
- },
- });
- });
- });
- });
- export const SEARCH_PRODUCTS = gql`
- query SearchProductsAdmin($input: SearchInput!) {
- search(input: $input) {
- totalItems
- items {
- enabled
- productId
- productName
- slug
- description
- productAsset {
- id
- preview
- focalPoint {
- x
- y
- }
- }
- productVariantId
- productVariantName
- productVariantAsset {
- id
- preview
- focalPoint {
- x
- y
- }
- }
- sku
- }
- }
- }
- `;
- export const SEARCH_GET_FACET_VALUES = gql`
- query SearchFacetValues($input: SearchInput!) {
- search(input: $input) {
- totalItems
- facetValues {
- count
- facetValue {
- id
- name
- }
- }
- }
- }
- `;
- export const SEARCH_GET_COLLECTIONS = gql`
- query SearchCollections($input: SearchInput!) {
- search(input: $input) {
- totalItems
- collections {
- count
- collection {
- id
- name
- }
- }
- }
- }
- `;
- export const SEARCH_GET_PRICES = gql`
- query SearchGetPrices($input: SearchInput!) {
- search(input: $input) {
- items {
- price {
- ... on PriceRange {
- min
- max
- }
- ... on SinglePrice {
- value
- }
- }
- priceWithTax {
- ... on PriceRange {
- min
- max
- }
- ... on SinglePrice {
- value
- }
- }
- }
- }
- }
- `;
- const REINDEX = gql`
- mutation Reindex {
- reindex {
- id
- queueName
- state
- progress
- duration
- result
- }
- }
- `;
- const GET_JOB_INFO = gql`
- query GetJobInfo($id: ID!) {
- job(jobId: $id) {
- id
- queueName
- state
- progress
- duration
- result
- }
- }
- `;
|