| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376 |
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
- import { CurrencyCode, DeletionResult, LanguageCode, SortOrder } from '@vendure/common/lib/generated-types';
- import { ROOT_COLLECTION_NAME } from '@vendure/common/lib/shared-constants';
- import {
- DefaultJobQueuePlugin,
- facetValueCollectionFilter,
- productIdCollectionFilter,
- variantIdCollectionFilter,
- variantNameCollectionFilter,
- } from '@vendure/core';
- import {
- createErrorResultGuard,
- createTestEnvironment,
- E2E_DEFAULT_CHANNEL_TOKEN,
- ErrorResultGuard,
- } from '@vendure/testing';
- import path from 'node: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 { pick } from '../../common/lib/pick';
- import { channelFragment, collectionFragment, facetValueFragment } from './graphql/fragments-admin';
- import { FragmentOf, graphql, ResultOf } from './graphql/graphql-admin';
- import {
- assignCollectionsToChannelDocument,
- createChannelDocument,
- createCollectionDocument,
- deleteProductDocument,
- deleteProductVariantDocument,
- getAssetListDocument,
- getCollectionDocument,
- getCollectionsDocument,
- updateCollectionDocument,
- updateProductDocument,
- updateProductVariantsDocument,
- } from './graphql/shared-definitions';
- import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
- import { awaitRunningJobs } from './utils/await-running-jobs';
- import { sortById } from './utils/test-order-utils';
- describe('Collection resolver', () => {
- const { server, adminClient, shopClient } = createTestEnvironment({
- ...testConfig(),
- plugins: [DefaultJobQueuePlugin],
- });
- let assets: ResultOf<typeof getAssetListDocument>['assets']['items'];
- let facetValues: Array<FragmentOf<typeof facetValueFragment>>;
- let electronicsCollection: FragmentOf<typeof collectionFragment>;
- let computersCollection: FragmentOf<typeof collectionFragment>;
- let pearCollection: FragmentOf<typeof collectionFragment>;
- let electronicsBreadcrumbsCollection: FragmentOf<typeof collectionFragment>;
- let computersBreadcrumbsCollection: FragmentOf<typeof collectionFragment>;
- let pearBreadcrumbsCollection: FragmentOf<typeof collectionFragment>;
- const SECOND_CHANNEL_TOKEN = 'second_channel_token';
- let secondChannel: FragmentOf<typeof channelFragment>;
- const channelGuard: ErrorResultGuard<FragmentOf<typeof channelFragment>> = createErrorResultGuard(
- input => !!input && !('errorCode' in input),
- );
- // Guard for collection query results
- type CollectionResult = NonNullable<ResultOf<typeof getCollectionProductVariantsDocument>['collection']>;
- const collectionResultGuard: ErrorResultGuard<CollectionResult> = createErrorResultGuard(
- input => !!input,
- );
- // Guard for product query results
- type ProductResult = NonNullable<ResultOf<typeof getProductCollectionsDocument>['product']>;
- const productResultGuard: ErrorResultGuard<ProductResult> = createErrorResultGuard(input => !!input);
- beforeAll(async () => {
- await server.init({
- initialData,
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-collections.csv'),
- customerCount: 1,
- });
- await adminClient.asSuperAdmin();
- const assetsResult = await adminClient.query(getAssetListDocument, {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- },
- });
- assets = assetsResult.assets.items;
- const facetValuesResult = await adminClient.query(getFacetValuesDocument);
- facetValues = facetValuesResult.facets.items.reduce(
- (values, facet) => [...values, ...facet.values],
- [] as Array<FragmentOf<typeof facetValueFragment>>,
- );
- const { createChannel } = await adminClient.query(createChannelDocument, {
- input: {
- code: 'second-channel',
- token: SECOND_CHANNEL_TOKEN,
- defaultLanguageCode: LanguageCode.en,
- currencyCode: CurrencyCode.USD,
- pricesIncludeTax: true,
- defaultShippingZoneId: 'T_1',
- defaultTaxZoneId: 'T_1',
- },
- });
- channelGuard.assertSuccess(createChannel);
- secondChannel = createChannel;
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- /**
- * Test case for https://github.com/vendure-ecommerce/vendure/issues/97
- */
- it('collection breadcrumbs works after bootstrap', async () => {
- const result = await adminClient.query(getCollectionBreadcrumbsDocument, {
- id: 'T_1',
- });
- expect(result.collection?.breadcrumbs[0].name).toBe(ROOT_COLLECTION_NAME);
- });
- describe('createCollection', () => {
- it('creates a root collection', async () => {
- const result = await adminClient.query(createCollectionDocument, {
- input: {
- assetIds: [assets[0].id, assets[1].id],
- featuredAssetId: assets[1].id,
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('electronics')}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Electronics',
- description: '',
- slug: 'electronics',
- },
- ],
- },
- });
- electronicsCollection = result.createCollection;
- expect(electronicsCollection).toMatchSnapshot();
- expect(electronicsCollection.parent!.name).toBe(ROOT_COLLECTION_NAME);
- });
- it('creates a nested collection', async () => {
- const result = await adminClient.query(createCollectionDocument, {
- input: {
- parentId: electronicsCollection.id,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Computers',
- description: '',
- slug: 'computers',
- },
- ],
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('computers')}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- },
- });
- computersCollection = result.createCollection;
- expect(computersCollection.parent!.name).toBe(electronicsCollection.name);
- });
- it('creates a 2nd level nested collection', async () => {
- const result = await adminClient.query(createCollectionDocument, {
- input: {
- parentId: computersCollection.id,
- translations: [
- { languageCode: LanguageCode.en, name: 'Pear', description: '', slug: 'pear' },
- ],
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('pear')}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- },
- });
- pearCollection = result.createCollection;
- expect(pearCollection.parent!.name).toBe(computersCollection.name);
- });
- it('slug is normalized to be url-safe', async () => {
- const { createCollection } = await adminClient.query(createCollectionDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Accessories',
- description: '',
- slug: 'Accessories!',
- },
- {
- languageCode: LanguageCode.de,
- name: 'Zubehör',
- description: '',
- slug: 'Zubehör!',
- },
- ],
- filters: [],
- },
- });
- expect(createCollection.slug).toBe('accessories');
- expect(createCollection.translations.find(t => t.languageCode === LanguageCode.en)?.slug).toBe(
- 'accessories',
- );
- expect(createCollection.translations.find(t => t.languageCode === LanguageCode.de)?.slug).toBe(
- 'zubehoer',
- );
- });
- it('create with duplicate slug is renamed to be unique', async () => {
- const { createCollection } = await adminClient.query(createCollectionDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Accessories',
- description: '',
- slug: 'Accessories',
- },
- {
- languageCode: LanguageCode.de,
- name: 'Zubehör',
- description: '',
- slug: 'Zubehör',
- },
- ],
- filters: [],
- },
- });
- expect(createCollection.translations.find(t => t.languageCode === LanguageCode.en)?.slug).toBe(
- 'accessories-2',
- );
- expect(createCollection.translations.find(t => t.languageCode === LanguageCode.de)?.slug).toBe(
- 'zubehoer-2',
- );
- });
- it('creates the duplicate slug without suffix in another channel', async () => {
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { createCollection } = await adminClient.query(createCollectionDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Accessories',
- description: '',
- slug: 'Accessories',
- },
- {
- languageCode: LanguageCode.de,
- name: 'Zubehör',
- description: '',
- slug: 'Zubehör',
- },
- ],
- filters: [],
- },
- });
- expect(createCollection.translations.find(t => t.languageCode === LanguageCode.en)?.slug).toBe(
- 'accessories',
- );
- expect(createCollection.translations.find(t => t.languageCode === LanguageCode.de)?.slug).toBe(
- 'zubehoer',
- );
- });
- it('creates a root collection to become a 1st level collection later #779', async () => {
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const result = await adminClient.query(createCollectionDocument, {
- input: {
- assetIds: [assets[0].id, assets[1].id],
- featuredAssetId: assets[1].id,
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('computers')}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Computers Breadcrumbs',
- description: '',
- slug: 'computers_breadcrumbs',
- },
- ],
- },
- });
- computersBreadcrumbsCollection = result.createCollection;
- expect(computersBreadcrumbsCollection.parent!.name).toBe(ROOT_COLLECTION_NAME);
- });
- it('creates a root collection to be a parent collection for 1st level collection with id greater than child collection #779', async () => {
- const result = await adminClient.query(createCollectionDocument, {
- input: {
- assetIds: [assets[0].id, assets[1].id],
- featuredAssetId: assets[1].id,
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('electronics')}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Electronics Breadcrumbs',
- description: '',
- slug: 'electronics_breadcrumbs',
- },
- ],
- },
- });
- electronicsBreadcrumbsCollection = result.createCollection;
- expect(electronicsBreadcrumbsCollection.parent!.name).toBe(ROOT_COLLECTION_NAME);
- });
- it('creates a 2nd level nested collection #779', async () => {
- const result = await adminClient.query(createCollectionDocument, {
- input: {
- parentId: computersBreadcrumbsCollection.id,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Pear Breadcrumbs',
- description: '',
- slug: 'pear_breadcrumbs',
- },
- ],
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('pear')}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- },
- });
- pearBreadcrumbsCollection = result.createCollection;
- expect(pearBreadcrumbsCollection.parent!.name).toBe(computersBreadcrumbsCollection.name);
- });
- });
- describe('updateCollection', () => {
- it('updates with assets', async () => {
- const { updateCollection } = await adminClient.query(updateCollectionDocument, {
- input: {
- id: pearCollection.id,
- assetIds: [assets[1].id, assets[2].id],
- featuredAssetId: assets[1].id,
- translations: [
- { languageCode: LanguageCode.en, description: 'Apple stuff ', slug: 'apple-stuff' },
- ],
- },
- });
- await awaitRunningJobs(adminClient, 5000);
- expect(updateCollection).toMatchSnapshot();
- pearCollection = updateCollection;
- });
- it('updating existing assets', async () => {
- const { updateCollection } = await adminClient.query(updateCollectionDocument, {
- input: {
- id: pearCollection.id,
- assetIds: [assets[3].id, assets[0].id],
- featuredAssetId: assets[3].id,
- },
- });
- await awaitRunningJobs(adminClient, 5000);
- expect(updateCollection.assets.map(a => a.id)).toEqual([assets[3].id, assets[0].id]);
- });
- it('removes all assets', async () => {
- const { updateCollection } = await adminClient.query(updateCollectionDocument, {
- input: {
- id: pearCollection.id,
- assetIds: [],
- },
- });
- await awaitRunningJobs(adminClient, 5000);
- expect(updateCollection.assets).toEqual([]);
- expect(updateCollection.featuredAsset).toBeNull();
- });
- });
- describe('querying', () => {
- it('collection by id', async () => {
- const result = await adminClient.query(getCollectionDocument, {
- id: computersCollection.id,
- });
- if (!result.collection) {
- fail('did not return the collection');
- return;
- }
- expect(result.collection.id).toBe(computersCollection.id);
- });
- it('collection by slug', async () => {
- const result = await adminClient.query(getCollectionDocument, {
- slug: computersCollection.slug,
- });
- if (!result.collection) {
- fail('did not return the collection');
- return;
- }
- expect(result.collection.id).toBe(computersCollection.id);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/538
- it('falls back to default language slug', async () => {
- const result = await adminClient.query(
- getCollectionDocument,
- {
- slug: computersCollection.slug,
- },
- { languageCode: LanguageCode.de },
- );
- if (!result.collection) {
- fail('did not return the collection');
- return;
- }
- expect(result.collection.id).toBe(computersCollection.id);
- });
- it(
- 'throws if neither id nor slug provided',
- assertThrowsWithMessage(async () => {
- await adminClient.query(getCollectionDocument, {});
- }, 'Either the Collection id or slug must be provided'),
- );
- it(
- 'throws if id and slug do not refer to the same Product',
- assertThrowsWithMessage(async () => {
- await adminClient.query(getCollectionDocument, {
- id: computersCollection.id,
- slug: pearCollection.slug,
- });
- }, 'The provided id and slug refer to different Collections'),
- );
- it('parent field', async () => {
- const result = await adminClient.query(getCollectionDocument, {
- id: computersCollection.id,
- });
- if (!result.collection) {
- fail('did not return the collection');
- return;
- }
- expect(result.collection.parent!.name).toBe('Electronics');
- });
- // Tests fix for https://github.com/vendure-ecommerce/vendure/issues/361
- it('parent field resolved by CollectionEntityResolver', async () => {
- const { product } = await adminClient.query(getProductCollectionsWithParentDocument, {
- id: 'T_1',
- });
- expect(product?.collections.length).toBe(6);
- expect(product?.collections.sort(sortById)).toEqual([
- {
- id: 'T_10',
- name: 'Electronics Breadcrumbs',
- parent: {
- id: 'T_1',
- name: '__root_collection__',
- },
- },
- {
- id: 'T_11',
- name: 'Pear Breadcrumbs',
- parent: {
- id: 'T_9',
- name: 'Computers Breadcrumbs',
- },
- },
- {
- id: 'T_3',
- name: 'Electronics',
- parent: {
- id: 'T_1',
- name: '__root_collection__',
- },
- },
- {
- id: 'T_4',
- name: 'Computers',
- parent: {
- id: 'T_3',
- name: 'Electronics',
- },
- },
- {
- id: 'T_5',
- name: 'Pear',
- parent: {
- id: 'T_4',
- name: 'Computers',
- },
- },
- {
- id: 'T_9',
- name: 'Computers Breadcrumbs',
- parent: {
- id: 'T_1',
- name: '__root_collection__',
- },
- },
- ]);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/981
- it('nested parent field in shop API', async () => {
- const { collections } = await shopClient.query(getCollectionNestedParentsDocument);
- expect(collections.items[0].parent?.name).toBe(ROOT_COLLECTION_NAME);
- });
- it('children field', async () => {
- const result = await adminClient.query(getCollectionDocument, {
- id: electronicsCollection.id,
- });
- if (!result.collection) {
- fail('did not return the collection');
- return;
- }
- expect(result.collection.children!.length).toBe(1);
- expect(result.collection.children![0].name).toBe('Computers');
- });
- it('breadcrumbs', async () => {
- const result = await adminClient.query(getCollectionBreadcrumbsDocument, {
- id: pearCollection.id,
- });
- if (!result.collection) {
- fail('did not return the collection');
- return;
- }
- expect(result.collection.breadcrumbs).toEqual([
- { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
- {
- id: electronicsCollection.id,
- name: electronicsCollection.name,
- slug: electronicsCollection.slug,
- },
- {
- id: computersCollection.id,
- name: computersCollection.name,
- slug: computersCollection.slug,
- },
- { id: pearCollection.id, name: pearCollection.name, slug: pearCollection.slug },
- ]);
- });
- it('breadcrumbs for root collection', async () => {
- const result = await adminClient.query(getCollectionBreadcrumbsDocument, {
- id: 'T_1',
- });
- if (!result.collection) {
- fail('did not return the collection');
- return;
- }
- expect(result.collection.breadcrumbs).toEqual([
- { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
- ]);
- });
- it('collections.assets', async () => {
- const { collections } = await adminClient.query(
- graphql(`
- query GetCollectionsWithAssets {
- collections {
- items {
- assets {
- name
- }
- }
- }
- }
- `),
- );
- expect(collections.items[0].assets).toBeDefined();
- });
- // https://github.com/vendure-ecommerce/vendure/issues/642
- it('sorting on Collection.productVariants.price', async () => {
- const { collection } = await adminClient.query(getCollectionDocument, {
- id: computersCollection.id,
- variantListOptions: {
- sort: {
- price: SortOrder.ASC,
- },
- },
- });
- collectionResultGuard.assertSuccess(collection);
- expect(collection.productVariants.items.map(i => i.price)).toEqual([
- 3799, 5374, 6900, 7489, 7896, 9299, 13435, 14374, 16994, 93120, 94920, 108720, 109995, 129900,
- 139900, 219900, 229900,
- ]);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/3107
- it('collection list with translations, filtered by name', async () => {
- const { collections } = await adminClient.query(getCollectionListWithTranslationsDocument, {
- options: {
- filter: {
- name: {
- eq: 'Electronics',
- },
- },
- },
- });
- expect(collections.items.length).toBeDefined();
- });
- });
- describe('moveCollection', () => {
- it('moves a collection to a new parent', async () => {
- const result = await adminClient.query(moveCollectionDocument, {
- input: {
- collectionId: pearCollection.id,
- parentId: electronicsCollection.id,
- index: 0,
- },
- });
- expect(result.moveCollection.parent!.id).toBe(electronicsCollection.id);
- const positions = await getChildrenOf(electronicsCollection.id);
- expect(positions.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
- });
- it('re-evaluates Collection contents on move', async () => {
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: pearCollection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Instant Camera',
- ]);
- });
- it('moves a 1st level collection to a new parent to check breadcrumbs', async () => {
- const result = await adminClient.query(moveCollectionDocument, {
- input: {
- collectionId: computersBreadcrumbsCollection.id,
- parentId: electronicsBreadcrumbsCollection.id,
- index: 0,
- },
- });
- expect(result.moveCollection.parent!.id).toBe(electronicsBreadcrumbsCollection.id);
- const positions = await getChildrenOf(electronicsBreadcrumbsCollection.id);
- expect(positions.map(i => i.id)).toEqual([computersBreadcrumbsCollection.id]);
- });
- it('breadcrumbs for collection with ids out of order', async () => {
- const result = await adminClient.query(getCollectionBreadcrumbsDocument, {
- id: pearBreadcrumbsCollection.id,
- });
- if (!result.collection) {
- fail('did not return the collection');
- return;
- }
- expect(result.collection.breadcrumbs).toEqual([
- { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
- {
- id: electronicsBreadcrumbsCollection.id,
- name: electronicsBreadcrumbsCollection.name,
- slug: electronicsBreadcrumbsCollection.slug,
- },
- {
- id: computersBreadcrumbsCollection.id,
- name: computersBreadcrumbsCollection.name,
- slug: computersBreadcrumbsCollection.slug,
- },
- {
- id: pearBreadcrumbsCollection.id,
- name: pearBreadcrumbsCollection.name,
- slug: pearBreadcrumbsCollection.slug,
- },
- ]);
- });
- it('alters the position in the current parent 1', async () => {
- await adminClient.query(moveCollectionDocument, {
- input: {
- collectionId: computersCollection.id,
- parentId: electronicsCollection.id,
- index: 0,
- },
- });
- const afterResult = await getChildrenOf(electronicsCollection.id);
- expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
- });
- it('alters the position in the current parent 2', async () => {
- await adminClient.query(moveCollectionDocument, {
- input: {
- collectionId: pearCollection.id,
- parentId: electronicsCollection.id,
- index: 0,
- },
- });
- const afterResult = await getChildrenOf(electronicsCollection.id);
- expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
- });
- it('corrects an out-of-bounds negative index value', async () => {
- await adminClient.query(moveCollectionDocument, {
- input: {
- collectionId: pearCollection.id,
- parentId: electronicsCollection.id,
- index: -3,
- },
- });
- const afterResult = await getChildrenOf(electronicsCollection.id);
- expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
- });
- it('corrects an out-of-bounds positive index value', async () => {
- await adminClient.query(moveCollectionDocument, {
- input: {
- collectionId: pearCollection.id,
- parentId: electronicsCollection.id,
- index: 10,
- },
- });
- const afterResult = await getChildrenOf(electronicsCollection.id);
- expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
- });
- it(
- 'throws if attempting to move into self',
- assertThrowsWithMessage(
- () =>
- adminClient.query(moveCollectionDocument, {
- input: {
- collectionId: pearCollection.id,
- parentId: pearCollection.id,
- index: 0,
- },
- }),
- 'Cannot move a Collection into itself',
- ),
- );
- it(
- 'throws if attempting to move into a descendant of self',
- assertThrowsWithMessage(
- () =>
- adminClient.query(moveCollectionDocument, {
- input: {
- collectionId: pearCollection.id,
- parentId: pearCollection.id,
- index: 0,
- },
- }),
- 'Cannot move a Collection into itself',
- ),
- );
- // https://github.com/vendure-ecommerce/vendure/issues/1595
- it('children correctly ordered', async () => {
- await adminClient.query(moveCollectionDocument, {
- input: {
- collectionId: computersCollection.id,
- parentId: 'T_1',
- index: 4,
- },
- });
- const result = await adminClient.query(getCollectionDocument, {
- id: 'T_1',
- });
- if (!result.collection) {
- fail('did not return the collection');
- return;
- }
- expect(result.collection.children?.map(c => (c as any).position)).toEqual([0, 1, 2, 3, 4, 5, 6]);
- });
- async function getChildrenOf(parentId: string): Promise<Array<{ name: string; id: string }>> {
- const result = await adminClient.query(getCollectionsDocument);
- return result.collections.items.filter(i => i.parent!.id === parentId);
- }
- });
- describe('deleteCollection', () => {
- let collectionToDeleteParent: ResultOf<typeof createCollectionDocument>['createCollection'];
- let collectionToDeleteChild: ResultOf<typeof createCollectionDocument>['createCollection'];
- let laptopProductId: string;
- beforeAll(async () => {
- const result1 = await adminClient.query(createCollectionDocument, {
- input: {
- filters: [
- {
- code: variantNameCollectionFilter.code,
- arguments: [
- {
- name: 'operator',
- value: 'contains',
- },
- {
- name: 'term',
- value: 'laptop',
- },
- ],
- },
- ],
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Delete Me Parent',
- description: '',
- slug: 'delete-me-parent',
- },
- ],
- assetIds: ['T_1'],
- },
- });
- collectionToDeleteParent = result1.createCollection;
- const result2 = await adminClient.query(createCollectionDocument, {
- input: {
- filters: [],
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Delete Me Child',
- description: '',
- slug: 'delete-me-child',
- },
- ],
- parentId: collectionToDeleteParent.id,
- assetIds: ['T_2'],
- },
- });
- collectionToDeleteChild = result2.createCollection;
- await awaitRunningJobs(adminClient, 5000);
- });
- it(
- 'throws for invalid collection id',
- assertThrowsWithMessage(async () => {
- await adminClient.query(deleteCollectionDocument, {
- id: 'T_999',
- });
- }, 'No Collection with the id "999" could be found'),
- );
- it('collection and product related prior to deletion', async () => {
- const { collection } = await adminClient.query(getCollectionProductVariantsDocument, {
- id: collectionToDeleteParent.id,
- });
- collectionResultGuard.assertSuccess(collection);
- expect(collection.productVariants.items.map(pick(['name']))).toEqual([
- { name: 'Laptop 13 inch 8GB' },
- { name: 'Laptop 15 inch 8GB' },
- { name: 'Laptop 13 inch 16GB' },
- { name: 'Laptop 15 inch 16GB' },
- ]);
- laptopProductId = collection.productVariants.items[0].productId;
- const { product } = await adminClient.query(getProductCollectionsDocument, {
- id: laptopProductId,
- });
- productResultGuard.assertSuccess(product);
- expect(product.collections).toEqual([
- {
- id: 'T_3',
- name: 'Electronics',
- },
- {
- id: 'T_4',
- name: 'Computers',
- },
- {
- id: 'T_5',
- name: 'Pear',
- },
- {
- id: 'T_9',
- name: 'Computers Breadcrumbs',
- },
- {
- id: 'T_10',
- name: 'Electronics Breadcrumbs',
- },
- {
- id: 'T_11',
- name: 'Pear Breadcrumbs',
- },
- {
- id: 'T_12',
- name: 'Delete Me Parent',
- },
- {
- id: 'T_13',
- name: 'Delete Me Child',
- },
- ]);
- });
- it('deleteCollection works', async () => {
- const { deleteCollection } = await adminClient.query(deleteCollectionDocument, {
- id: collectionToDeleteParent.id,
- });
- expect(deleteCollection.result).toBe(DeletionResult.DELETED);
- });
- it('deleted parent collection is null', async () => {
- const { collection } = await adminClient.query(getCollectionDocument, {
- id: collectionToDeleteParent.id,
- });
- expect(collection).toBeNull();
- });
- it('deleted child collection is null', async () => {
- const { collection } = await adminClient.query(getCollectionDocument, {
- id: collectionToDeleteChild.id,
- });
- expect(collection).toBeNull();
- });
- it('product no longer lists collection', async () => {
- const { product } = await adminClient.query(getProductCollectionsDocument, {
- id: laptopProductId,
- });
- productResultGuard.assertSuccess(product);
- expect(product.collections).toEqual([
- { id: 'T_3', name: 'Electronics' },
- { id: 'T_4', name: 'Computers' },
- { id: 'T_5', name: 'Pear' },
- {
- id: 'T_9',
- name: 'Computers Breadcrumbs',
- },
- {
- id: 'T_10',
- name: 'Electronics Breadcrumbs',
- },
- {
- id: 'T_11',
- name: 'Pear Breadcrumbs',
- },
- ]);
- });
- });
- describe('filters', () => {
- it('Collection with no filters has no productVariants', async () => {
- const result = await adminClient.query(createCollectionSelectVariantsDocument, {
- input: {
- translations: [
- { languageCode: LanguageCode.en, name: 'Empty', description: '', slug: 'empty' },
- ],
- filters: [],
- },
- });
- expect(result.createCollection.productVariants.totalItems).toBe(0);
- });
- describe('facetValue filter', () => {
- it('electronics', async () => {
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: electronicsCollection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Curvy Monitor 24 inch',
- 'Curvy Monitor 27 inch',
- 'Gaming PC i7-8700 240GB SSD',
- 'Gaming PC R7-2700 240GB SSD',
- 'Gaming PC i7-8700 120GB SSD',
- 'Gaming PC R7-2700 120GB SSD',
- 'Hard Drive 1TB',
- 'Hard Drive 2TB',
- 'Hard Drive 3TB',
- 'Hard Drive 4TB',
- 'Hard Drive 6TB',
- 'Clacky Keyboard',
- 'USB Cable',
- 'Instant Camera',
- 'Camera Lens',
- 'Tripod',
- 'SLR Camera',
- ]);
- });
- it('computers', async () => {
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: computersCollection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Curvy Monitor 24 inch',
- 'Curvy Monitor 27 inch',
- 'Gaming PC i7-8700 240GB SSD',
- 'Gaming PC R7-2700 240GB SSD',
- 'Gaming PC i7-8700 120GB SSD',
- 'Gaming PC R7-2700 120GB SSD',
- 'Hard Drive 1TB',
- 'Hard Drive 2TB',
- 'Hard Drive 3TB',
- 'Hard Drive 4TB',
- 'Hard Drive 6TB',
- 'Clacky Keyboard',
- 'USB Cable',
- ]);
- });
- it('photo AND pear', async () => {
- const result = await adminClient.query(createCollectionSelectVariantsDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Photo AND Pear',
- description: '',
- slug: 'photo-and-pear',
- },
- ],
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('pear')}", "${getFacetValueId(
- 'photo',
- )}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- },
- });
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await adminClient.query(getCollectionDocument, {
- id: result.createCollection.id,
- });
- collectionResultGuard.assertSuccess(collection);
- expect(collection.productVariants.items.map(i => i.name)).toEqual(['Instant Camera']);
- });
- it('photo OR pear', async () => {
- const result = await adminClient.query(createCollectionSelectVariantsDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Photo OR Pear',
- description: '',
- slug: 'photo-or-pear',
- },
- ],
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('pear')}", "${getFacetValueId(
- 'photo',
- )}"]`,
- },
- {
- name: 'containsAny',
- value: 'true',
- },
- ],
- },
- ],
- },
- });
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await adminClient.query(getCollectionDocument, {
- id: result.createCollection.id,
- });
- collectionResultGuard.assertSuccess(collection);
- expect(collection.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Instant Camera',
- 'Camera Lens',
- 'Tripod',
- 'SLR Camera',
- 'Hat',
- ]);
- });
- it('bell OR pear in computers', async () => {
- const result = await adminClient.query(createCollectionSelectVariantsDocument, {
- input: {
- parentId: computersCollection.id,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Bell OR Pear Computers',
- description: '',
- slug: 'bell-or-pear',
- },
- ],
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('pear')}", "${getFacetValueId('bell')}"]`,
- },
- {
- name: 'containsAny',
- value: 'true',
- },
- ],
- },
- ],
- },
- });
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await adminClient.query(getCollectionDocument, {
- id: result.createCollection.id,
- });
- collectionResultGuard.assertSuccess(collection);
- expect(collection.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Curvy Monitor 24 inch',
- 'Curvy Monitor 27 inch',
- ]);
- });
- });
- describe('variantName filter', () => {
- async function createVariantNameFilteredCollection(
- operator: string,
- term: string,
- parentId?: string,
- ): Promise<FragmentOf<typeof collectionFragment>> {
- const { createCollection } = await adminClient.query(createCollectionDocument, {
- input: {
- parentId,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: `${operator} ${term}`,
- description: '',
- slug: `${operator} ${term}`,
- },
- ],
- filters: [
- {
- code: variantNameCollectionFilter.code,
- arguments: [
- {
- name: 'operator',
- value: operator,
- },
- {
- name: 'term',
- value: term,
- },
- ],
- },
- ],
- },
- });
- await awaitRunningJobs(adminClient, 5000);
- return createCollection;
- }
- it('contains operator', async () => {
- const collection = await createVariantNameFilteredCollection('contains', 'camera');
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: collection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual([
- 'Instant Camera',
- 'Camera Lens',
- 'SLR Camera',
- ]);
- });
- it('startsWith operator', async () => {
- const collection = await createVariantNameFilteredCollection('startsWith', 'camera');
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: collection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual(['Camera Lens']);
- });
- it('endsWith operator', async () => {
- const collection = await createVariantNameFilteredCollection('endsWith', 'camera');
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: collection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual([
- 'Instant Camera',
- 'SLR Camera',
- ]);
- });
- it('doesNotContain operator', async () => {
- const collection = await createVariantNameFilteredCollection('doesNotContain', 'camera');
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: collection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Curvy Monitor 24 inch',
- 'Curvy Monitor 27 inch',
- 'Gaming PC i7-8700 240GB SSD',
- 'Gaming PC R7-2700 240GB SSD',
- 'Gaming PC i7-8700 120GB SSD',
- 'Gaming PC R7-2700 120GB SSD',
- 'Hard Drive 1TB',
- 'Hard Drive 2TB',
- 'Hard Drive 3TB',
- 'Hard Drive 4TB',
- 'Hard Drive 6TB',
- 'Clacky Keyboard',
- 'USB Cable',
- 'Tripod',
- 'Hat',
- 'Boots',
- ]);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/927
- it('nested variantName filter', async () => {
- const parent = await createVariantNameFilteredCollection('contains', 'lap');
- const parentResult = await adminClient.query(getCollectionProductVariantsDocument, {
- id: parent.id,
- });
- expect(parentResult.collection?.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- ]);
- const child = await createVariantNameFilteredCollection('contains', 'GB', parent.id);
- const childResult = await adminClient.query(getCollectionProductVariantsDocument, {
- id: child.id,
- });
- expect(childResult.collection?.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- ]);
- });
- });
- describe('variantId filter', () => {
- it('contains expects variants', async () => {
- const { createCollection } = await adminClient.query(createCollectionDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'variantId filter test',
- description: '',
- slug: 'variantId-filter-test',
- },
- ],
- filters: [
- {
- code: variantIdCollectionFilter.code,
- arguments: [
- {
- name: 'variantIds',
- value: '["T_1", "T_4"]',
- },
- ],
- },
- ],
- },
- });
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: createCollection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.id).sort()).toEqual(['T_1', 'T_4']);
- });
- });
- describe('productId filter', () => {
- it('contains expects variants', async () => {
- const { createCollection } = await adminClient.query(createCollectionDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'productId filter test',
- description: '',
- slug: 'productId-filter-test',
- },
- ],
- filters: [
- {
- code: productIdCollectionFilter.code,
- arguments: [
- {
- name: 'productIds',
- value: '["T_2"]',
- },
- ],
- },
- ],
- },
- });
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: createCollection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.id).sort()).toEqual(['T_5', 'T_6']);
- });
- });
- describe('re-evaluation of contents on changes', () => {
- const getProductsWithVariantIdsDocument = graphql(`
- query GetProductsWithVariantIds {
- products(options: { sort: { id: ASC } }) {
- items {
- id
- name
- variants {
- id
- name
- }
- }
- }
- }
- `);
- let products: ResultOf<typeof getProductsWithVariantIdsDocument>['products']['items'];
- beforeAll(async () => {
- const result = await adminClient.query(getProductsWithVariantIdsDocument);
- products = result.products.items;
- });
- it('updates contents when Product is updated', async () => {
- await adminClient.query(updateProductDocument, {
- input: {
- id: products[1].id,
- facetValueIds: [
- getFacetValueId('electronics'),
- getFacetValueId('computers'),
- getFacetValueId('pear'),
- ],
- },
- });
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: pearCollection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Curvy Monitor 24 inch',
- 'Curvy Monitor 27 inch',
- 'Instant Camera',
- ]);
- });
- it('updates contents when ProductVariant is updated', async () => {
- const gamingPc240GB = products
- .find(p => p.name === 'Gaming PC')!
- .variants.find(v => v.name.includes('240GB'))!;
- await adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: gamingPc240GB.id,
- facetValueIds: [getFacetValueId('pear')],
- },
- ],
- });
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: pearCollection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Curvy Monitor 24 inch',
- 'Curvy Monitor 27 inch',
- 'Gaming PC i7-8700 240GB SSD',
- 'Instant Camera',
- ]);
- });
- it('correctly filters when ProductVariant and Product both have matching FacetValue', async () => {
- const gamingPc240GB = products
- .find(p => p.name === 'Gaming PC')!
- .variants.find(v => v.name.includes('240GB'))!;
- await adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: gamingPc240GB.id,
- facetValueIds: [getFacetValueId('electronics'), getFacetValueId('pear')],
- },
- ],
- });
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: pearCollection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Curvy Monitor 24 inch',
- 'Curvy Monitor 27 inch',
- 'Gaming PC i7-8700 240GB SSD',
- 'Instant Camera',
- ]);
- });
- });
- describe('filter inheritance', () => {
- let clothesCollectionId: string;
- it('filter inheritance of nested collections (issue #158)', async () => {
- const { createCollection: pearElectronics } = await adminClient.query(
- createCollectionSelectVariantsDocument,
- {
- input: {
- parentId: electronicsCollection.id,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'pear electronics',
- description: '',
- slug: 'pear-electronics',
- },
- ],
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('pear')}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- },
- },
- );
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: pearElectronics.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Curvy Monitor 24 inch',
- 'Curvy Monitor 27 inch',
- 'Gaming PC i7-8700 240GB SSD',
- 'Instant Camera',
- // no "Hat"
- ]);
- });
- it('child collection with no inheritance', async () => {
- const { createCollection: clothesCollection } = await adminClient.query(
- createCollectionSelectVariantsDocument,
- {
- input: {
- parentId: electronicsCollection.id,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'clothes',
- description: '',
- slug: 'clothes',
- },
- ],
- inheritFilters: false,
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('clothing')}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- },
- },
- );
- await awaitRunningJobs(adminClient, 5000);
- clothesCollectionId = clothesCollection.id;
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: clothesCollection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual(['Hat', 'Boots']);
- });
- it('grandchild collection with inheritance (root -> no inherit -> inherit', async () => {
- const { createCollection: footwearCollection } = await adminClient.query(
- createCollectionSelectVariantsDocument,
- {
- input: {
- parentId: clothesCollectionId,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'footwear',
- description: '',
- slug: 'footwear',
- },
- ],
- inheritFilters: true,
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('footwear')}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- },
- },
- );
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query(getCollectionProductVariantsDocument, {
- id: footwearCollection.id,
- });
- collectionResultGuard.assertSuccess(result.collection);
- expect(result.collection.productVariants.items.map(i => i.name)).toEqual(['Boots']);
- });
- });
- describe('previewCollectionVariants', () => {
- it('returns correct contents', async () => {
- const { previewCollectionVariants } = await adminClient.query(
- previewCollectionVariantsDocument,
- {
- input: {
- parentId: electronicsCollection.parent?.id,
- inheritFilters: true,
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('electronics')}","${getFacetValueId(
- 'pear',
- )}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- },
- },
- );
- expect(previewCollectionVariants.items.map(i => i.name).sort()).toEqual([
- 'Curvy Monitor 24 inch',
- 'Curvy Monitor 27 inch',
- 'Gaming PC i7-8700 240GB SSD',
- 'Instant Camera',
- 'Laptop 13 inch 16GB',
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 16GB',
- 'Laptop 15 inch 8GB',
- ]);
- });
- it('works with list options', async () => {
- const { previewCollectionVariants } = await adminClient.query(
- previewCollectionVariantsDocument,
- {
- input: {
- parentId: electronicsCollection.parent?.id,
- inheritFilters: true,
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('electronics')}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- },
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- filter: {
- name: {
- contains: 'mon',
- },
- },
- take: 5,
- },
- },
- );
- expect(previewCollectionVariants.items).toEqual([
- { id: 'T_5', name: 'Curvy Monitor 24 inch' },
- { id: 'T_6', name: 'Curvy Monitor 27 inch' },
- ]);
- });
- it('takes parent filters into account', async () => {
- const { previewCollectionVariants } = await adminClient.query(
- previewCollectionVariantsDocument,
- {
- input: {
- parentId: electronicsCollection.id,
- inheritFilters: true,
- filters: [
- {
- code: variantNameCollectionFilter.code,
- arguments: [
- {
- name: 'operator',
- value: 'startsWith',
- },
- {
- name: 'term',
- value: 'h',
- },
- ],
- },
- ],
- },
- },
- );
- expect(previewCollectionVariants.items.map(i => i.name).sort()).toEqual([
- 'Hard Drive 1TB',
- 'Hard Drive 2TB',
- 'Hard Drive 3TB',
- 'Hard Drive 4TB',
- 'Hard Drive 6TB',
- ]);
- });
- it('ignores parent filters id inheritFilters set to false', async () => {
- const { previewCollectionVariants } = await adminClient.query(
- previewCollectionVariantsDocument,
- {
- input: {
- parentId: electronicsCollection.id,
- inheritFilters: false,
- filters: [
- {
- code: variantNameCollectionFilter.code,
- arguments: [
- {
- name: 'operator',
- value: 'startsWith',
- },
- {
- name: 'term',
- value: 'h',
- },
- ],
- },
- ],
- },
- },
- );
- expect(previewCollectionVariants.items.map(i => i.name).sort()).toEqual([
- 'Hard Drive 1TB',
- 'Hard Drive 2TB',
- 'Hard Drive 3TB',
- 'Hard Drive 4TB',
- 'Hard Drive 6TB',
- 'Hat',
- ]);
- });
- it('with no parentId, operates at the root level', async () => {
- const { previewCollectionVariants } = await adminClient.query(
- previewCollectionVariantsDocument,
- {
- input: {
- inheritFilters: true,
- filters: [
- {
- code: variantNameCollectionFilter.code,
- arguments: [
- {
- name: 'operator',
- value: 'startsWith',
- },
- {
- name: 'term',
- value: 'h',
- },
- ],
- },
- ],
- },
- },
- );
- expect(previewCollectionVariants.items.map(i => i.name).sort()).toEqual([
- 'Hard Drive 1TB',
- 'Hard Drive 2TB',
- 'Hard Drive 3TB',
- 'Hard Drive 4TB',
- 'Hard Drive 6TB',
- 'Hat',
- ]);
- });
- });
- });
- describe('Product collections property', () => {
- it('returns all collections to which the Product belongs', async () => {
- const result = await adminClient.query(getCollectionsForProductsDocument, { term: 'camera' });
- expect(result.products.items[0].collections).toEqual([
- {
- id: 'T_3',
- name: 'Electronics',
- },
- {
- id: 'T_5',
- name: 'Pear',
- },
- {
- id: 'T_10',
- name: 'Electronics Breadcrumbs',
- },
- {
- id: 'T_15',
- name: 'Photo AND Pear',
- },
- {
- id: 'T_16',
- name: 'Photo OR Pear',
- },
- {
- id: 'T_18',
- name: 'contains camera',
- },
- {
- id: 'T_20',
- name: 'endsWith camera',
- },
- {
- id: 'T_26',
- name: 'pear electronics',
- },
- ]);
- });
- });
- describe('productVariants list', () => {
- it('does not list variants from deleted products', async () => {
- await adminClient.query(deleteProductDocument, {
- id: 'T_2', // curvy monitor
- });
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await adminClient.query(getCollectionProductVariantsDocument, {
- id: pearCollection.id,
- });
- collectionResultGuard.assertSuccess(collection);
- expect(collection.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Gaming PC i7-8700 240GB SSD',
- 'Instant Camera',
- ]);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1213
- it('does not list deleted variants', async () => {
- await adminClient.query(deleteProductVariantDocument, {
- id: 'T_18', // Instant Camera
- });
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await adminClient.query(getCollectionProductVariantsDocument, {
- id: pearCollection.id,
- });
- collectionResultGuard.assertSuccess(collection);
- expect(collection.productVariants.items.map(i => i.name)).toEqual([
- 'Laptop 13 inch 8GB',
- 'Laptop 15 inch 8GB',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Gaming PC i7-8700 240GB SSD',
- // 'Instant Camera',
- ]);
- });
- it('does not list disabled variants in Shop API', async () => {
- await adminClient.query(updateProductVariantsDocument, {
- input: [{ id: 'T_1', enabled: false }],
- });
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await shopClient.query(getCollectionProductVariantsDocument, {
- id: pearCollection.id,
- });
- collectionResultGuard.assertSuccess(collection);
- expect(collection.productVariants.items.map(i => i.id).includes('T_1')).toBe(false);
- });
- it('does not list variants of disabled products in Shop API', async () => {
- await adminClient.query(updateProductDocument, {
- input: { id: 'T_1', enabled: false },
- });
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await shopClient.query(getCollectionProductVariantsDocument, {
- id: pearCollection.id,
- });
- collectionResultGuard.assertSuccess(collection);
- expect(collection.productVariants.items.map(i => i.id).includes('T_1')).toBe(false);
- expect(collection.productVariants.items.map(i => i.id).includes('T_2')).toBe(false);
- expect(collection.productVariants.items.map(i => i.id).includes('T_3')).toBe(false);
- expect(collection.productVariants.items.map(i => i.id).includes('T_4')).toBe(false);
- });
- it('handles other languages', async () => {
- await adminClient.query(updateProductDocument, {
- input: { id: 'T_1', enabled: true },
- });
- await adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: 'T_2',
- translations: [{ languageCode: LanguageCode.de, name: 'Taschenrechner 15 Zoll' }],
- },
- ],
- });
- const { collection } = await shopClient.query(
- getCollectionProductVariantsDocument,
- {
- id: pearCollection.id,
- },
- { languageCode: LanguageCode.de },
- );
- collectionResultGuard.assertSuccess(collection);
- expect(collection.productVariants.items.map(i => i.name)).toEqual([
- 'Taschenrechner 15 Zoll',
- 'Laptop 13 inch 16GB',
- 'Laptop 15 inch 16GB',
- 'Gaming PC i7-8700 240GB SSD',
- ]);
- });
- });
- describe('channel assignment & removal', () => {
- let testCollection: FragmentOf<typeof collectionFragment>;
- beforeAll(async () => {
- const { createCollection } = await adminClient.query(createCollectionDocument, {
- input: {
- filters: [
- {
- code: facetValueCollectionFilter.code,
- arguments: [
- {
- name: 'facetValueIds',
- value: `["${getFacetValueId('electronics')}"]`,
- },
- {
- name: 'containsAny',
- value: 'false',
- },
- ],
- },
- ],
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Channels Test Collection',
- description: '',
- slug: 'channels-test-collection',
- },
- ],
- },
- });
- testCollection = createCollection;
- });
- it('assign to channel', async () => {
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { collections: before } = await adminClient.query(getCollectionListDocument);
- expect(before.items.length).toBe(1);
- expect(before.items.map(i => i.id).includes(testCollection.id)).toBe(false);
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const { assignCollectionsToChannel } = await adminClient.query(
- assignCollectionsToChannelDocument,
- {
- input: {
- channelId: secondChannel.id,
- collectionIds: [testCollection.id],
- },
- },
- );
- expect(assignCollectionsToChannel.map(c => c.id)).toEqual([testCollection.id]);
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { collections: after } = await adminClient.query(getCollectionListDocument);
- expect(after.items.length).toBe(2);
- expect(after.items.map(i => i.id).includes(testCollection.id)).toBe(true);
- });
- it('remove from channel', async () => {
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const { removeCollectionsFromChannel } = await adminClient.query(
- removeCollectionsFromChannelDocument,
- {
- input: {
- channelId: secondChannel.id,
- collectionIds: [testCollection.id],
- },
- },
- );
- expect(removeCollectionsFromChannel.map(c => c.id)).toEqual([testCollection.id]);
- adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
- const { collections: after } = await adminClient.query(getCollectionListDocument);
- expect(after.items.length).toBe(1);
- expect(after.items.map(i => i.id).includes(testCollection.id)).toBe(false);
- });
- });
- describe('deleteCollections (multiple)', () => {
- let top: FragmentOf<typeof collectionFragment>;
- let child: FragmentOf<typeof collectionFragment>;
- let grandchild: FragmentOf<typeof collectionFragment>;
- beforeAll(async () => {
- async function createNewCollection(name: string, parentId?: string) {
- const { createCollection } = await adminClient.query(createCollectionDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name,
- description: '',
- slug: name,
- },
- ],
- filters: [],
- },
- });
- return createCollection;
- }
- top = await createNewCollection('top');
- child = await createNewCollection('child', top.id);
- grandchild = await createNewCollection('grandchild', child.id);
- });
- it('deletes all selected collections', async () => {
- const { collections: before } = await adminClient.query(getCollectionListDocument);
- expect(before.items.sort(sortById).map(pick(['name']))).toEqual([
- { name: 'top' },
- { name: 'child' },
- { name: 'grandchild' },
- { name: 'Accessories' },
- ]);
- const { deleteCollections } = await adminClient.query(deleteCollectionsBulkDocument, {
- ids: [top.id, child.id, grandchild.id],
- });
- expect(deleteCollections).toEqual([
- { result: DeletionResult.DELETED, message: null },
- { result: DeletionResult.DELETED, message: null },
- { result: DeletionResult.DELETED, message: null },
- ]);
- const { collections: after } = await adminClient.query(getCollectionListDocument);
- expect(after.items.map(pick(['id', 'name'])).sort(sortById)).toEqual([
- { id: 'T_8', name: 'Accessories' },
- ]);
- });
- });
- function getFacetValueId(code: string): string {
- const match = facetValues.find(fv => fv.code === code);
- if (!match) {
- throw new Error(`Could not find a FacetValue with the code "${code}"`);
- }
- return match.id;
- }
- });
- export const getCollectionListDocument = graphql(
- `
- query GetCollectionListAdmin($options: CollectionListOptions) {
- collections(options: $options) {
- items {
- ...Collection
- }
- totalItems
- }
- }
- `,
- [collectionFragment],
- );
- export const getCollectionListWithTranslationsDocument = graphql(`
- query GetCollectionListWithTranslations($options: CollectionListOptions) {
- collections(options: $options) {
- items {
- id
- name
- translations {
- id
- name
- }
- }
- }
- }
- `);
- export const moveCollectionDocument = graphql(
- `
- mutation MoveCollection($input: MoveCollectionInput!) {
- moveCollection(input: $input) {
- ...Collection
- }
- }
- `,
- [collectionFragment],
- );
- const getFacetValuesDocument = graphql(
- `
- query GetFacetWithFacetValues {
- facets {
- items {
- values {
- ...FacetValue
- }
- }
- }
- }
- `,
- [facetValueFragment],
- );
- const getCollectionProductVariantsDocument = graphql(`
- query GetCollectionProducts($id: ID!) {
- collection(id: $id) {
- productVariants(options: { sort: { id: ASC } }) {
- items {
- id
- name
- facetValues {
- code
- }
- productId
- }
- }
- }
- }
- `);
- const createCollectionSelectVariantsDocument = graphql(`
- mutation CreateCollectionSelectVariants($input: CreateCollectionInput!) {
- createCollection(input: $input) {
- id
- productVariants {
- items {
- name
- }
- totalItems
- }
- }
- }
- `);
- const getCollectionBreadcrumbsDocument = graphql(`
- query GetCollectionBreadcrumbs($id: ID!) {
- collection(id: $id) {
- breadcrumbs {
- id
- name
- slug
- }
- }
- }
- `);
- const getCollectionsForProductsDocument = graphql(`
- query GetCollectionsForProducts($term: String!) {
- products(options: { filter: { name: { contains: $term } } }) {
- items {
- id
- name
- collections {
- id
- name
- }
- }
- }
- }
- `);
- const deleteCollectionDocument = graphql(`
- mutation DeleteCollection($id: ID!) {
- deleteCollection(id: $id) {
- result
- message
- }
- }
- `);
- const getProductCollectionsDocument = graphql(`
- query GetProductCollections($id: ID!) {
- product(id: $id) {
- id
- collections {
- id
- name
- }
- }
- }
- `);
- const getProductCollectionsWithParentDocument = graphql(`
- query GetProductCollectionsWithParent($id: ID!) {
- product(id: $id) {
- id
- collections {
- id
- name
- parent {
- id
- name
- }
- }
- }
- }
- `);
- const getCollectionNestedParentsDocument = graphql(`
- query GetCollectionNestedParents {
- collections {
- items {
- id
- name
- parent {
- name
- parent {
- name
- parent {
- name
- }
- }
- }
- }
- }
- }
- `);
- const previewCollectionVariantsDocument = graphql(`
- query PreviewCollectionVariants(
- $input: PreviewCollectionVariantsInput!
- $options: ProductVariantListOptions
- ) {
- previewCollectionVariants(input: $input, options: $options) {
- items {
- id
- name
- }
- totalItems
- }
- }
- `);
- const removeCollectionsFromChannelDocument = graphql(
- `
- mutation RemoveCollectionsFromChannel($input: RemoveCollectionsFromChannelInput!) {
- removeCollectionsFromChannel(input: $input) {
- ...Collection
- }
- }
- `,
- [collectionFragment],
- );
- const deleteCollectionsBulkDocument = graphql(`
- mutation DeleteCollectionsBulk($ids: [ID!]!) {
- deleteCollections(ids: $ids) {
- message
- result
- }
- }
- `);
|