| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043 |
- /* tslint:disable:no-non-null-assertion */
- import { ROOT_COLLECTION_NAME } from '@vendure/common/lib/shared-constants';
- import {
- DefaultJobQueuePlugin,
- facetValueCollectionFilter,
- variantNameCollectionFilter,
- } from '@vendure/core';
- import { createTestEnvironment } from '@vendure/testing';
- import gql from 'graphql-tag';
- import path from 'path';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
- import { pick } from '../../common/lib/pick';
- import { COLLECTION_FRAGMENT, FACET_VALUE_FRAGMENT } from './graphql/fragments';
- import {
- CollectionFragment,
- CreateCollectionInput,
- CreateCollectionMutation,
- DeletionResult,
- FacetValueFragment,
- GetAssetListQuery,
- GetProductsWithVariantIdsQuery,
- LanguageCode,
- SortOrder,
- } from './graphql/generated-e2e-admin-types';
- import * as Codegen from './graphql/generated-e2e-admin-types';
- import {
- CREATE_COLLECTION,
- DELETE_PRODUCT,
- DELETE_PRODUCT_VARIANT,
- GET_ASSET_LIST,
- GET_COLLECTIONS,
- UPDATE_COLLECTION,
- UPDATE_PRODUCT,
- UPDATE_PRODUCT_VARIANTS,
- } 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: GetAssetListQuery['assets']['items'];
- let facetValues: FacetValueFragment[];
- let electronicsCollection: CollectionFragment;
- let computersCollection: CollectionFragment;
- let pearCollection: CollectionFragment;
- let electronicsBreadcrumbsCollection: CollectionFragment;
- let computersBreadcrumbsCollection: CollectionFragment;
- let pearBreadcrumbsCollection: CollectionFragment;
- 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<
- Codegen.GetAssetListQuery,
- Codegen.GetAssetListQueryVariables
- >(GET_ASSET_LIST, {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- },
- });
- assets = assetsResult.assets.items;
- const facetValuesResult = await adminClient.query<Codegen.GetFacetValuesQuery>(GET_FACET_VALUES);
- facetValues = facetValuesResult.facets.items.reduce(
- (values, facet) => [...values, ...facet.values],
- [] as FacetValueFragment[],
- );
- }, 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<Codegen.GetCollectionBreadcrumbsQuery>(
- GET_COLLECTION_BREADCRUMBS,
- {
- 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<
- Codegen.CreateCollectionMutation,
- Codegen.CreateCollectionMutationVariables
- >(CREATE_COLLECTION, {
- 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<
- Codegen.CreateCollectionMutation,
- Codegen.CreateCollectionMutationVariables
- >(CREATE_COLLECTION, {
- 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<
- Codegen.CreateCollectionMutation,
- Codegen.CreateCollectionMutationVariables
- >(CREATE_COLLECTION, {
- 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<
- Codegen.CreateCollectionMutation,
- Codegen.CreateCollectionMutationVariables
- >(CREATE_COLLECTION, {
- 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(
- 'zubehor',
- );
- });
- it('create with duplicate slug is renamed to be unique', async () => {
- const { createCollection } = await adminClient.query<
- Codegen.CreateCollectionMutation,
- Codegen.CreateCollectionMutationVariables
- >(CREATE_COLLECTION, {
- 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(
- 'zubehor-2',
- );
- });
- it('creates a root collection to became a 1st level collection later #779', async () => {
- const result = await adminClient.query<
- Codegen.CreateCollectionMutation,
- Codegen.CreateCollectionMutationVariables
- >(CREATE_COLLECTION, {
- 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<
- Codegen.CreateCollectionMutation,
- Codegen.CreateCollectionMutationVariables
- >(CREATE_COLLECTION, {
- 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<
- Codegen.CreateCollectionMutation,
- Codegen.CreateCollectionMutationVariables
- >(CREATE_COLLECTION, {
- 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<
- Codegen.UpdateCollectionMutation,
- Codegen.UpdateCollectionMutationVariables
- >(UPDATE_COLLECTION, {
- 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<
- Codegen.UpdateCollectionMutation,
- Codegen.UpdateCollectionMutationVariables
- >(UPDATE_COLLECTION, {
- 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<
- Codegen.UpdateCollectionMutation,
- Codegen.UpdateCollectionMutationVariables
- >(UPDATE_COLLECTION, {
- 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<
- Codegen.GetCollectionQuery,
- Codegen.GetCollectionQueryVariables
- >(GET_COLLECTION, {
- 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<
- Codegen.GetCollectionQuery,
- Codegen.GetCollectionQueryVariables
- >(GET_COLLECTION, {
- 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<
- Codegen.GetCollectionQuery,
- Codegen.GetCollectionQueryVariables
- >(
- GET_COLLECTION,
- {
- 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<Codegen.GetCollectionQuery, Codegen.GetCollectionQueryVariables>(
- GET_COLLECTION,
- {},
- );
- }, '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<Codegen.GetCollectionQuery, Codegen.GetCollectionQueryVariables>(
- GET_COLLECTION,
- {
- id: computersCollection.id,
- slug: pearCollection.slug,
- },
- );
- }, 'The provided id and slug refer to different Collections'),
- );
- it('parent field', async () => {
- const result = await adminClient.query<
- Codegen.GetCollectionQuery,
- Codegen.GetCollectionQueryVariables
- >(GET_COLLECTION, {
- 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<
- Codegen.GetProductCollectionsWithParentQuery,
- Codegen.GetProductCollectionsWithParentQueryVariables
- >(GET_PRODUCT_COLLECTIONS_WITH_PARENT, {
- id: 'T_1',
- });
- expect(product?.collections.length).toBe(6);
- expect(product?.collections.sort(sortById)).toEqual([
- {
- id: 'T_10',
- name: 'Pear Breadcrumbs',
- parent: {
- id: 'T_8',
- 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_8',
- name: 'Computers Breadcrumbs',
- parent: {
- id: 'T_1',
- name: '__root_collection__',
- },
- },
- {
- id: 'T_9',
- name: 'Electronics 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<Codegen.GetCollectionNestedParentsQuery>(
- GET_COLLECTION_NESTED_PARENTS,
- );
- expect(collections.items[0].parent?.name).toBe(ROOT_COLLECTION_NAME);
- });
- it('children field', async () => {
- const result = await adminClient.query<
- Codegen.GetCollectionQuery,
- Codegen.GetCollectionQueryVariables
- >(GET_COLLECTION, {
- 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<
- Codegen.GetCollectionBreadcrumbsQuery,
- Codegen.GetCollectionBreadcrumbsQueryVariables
- >(GET_COLLECTION_BREADCRUMBS, {
- 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<
- Codegen.GetCollectionBreadcrumbsQuery,
- Codegen.GetCollectionBreadcrumbsQueryVariables
- >(GET_COLLECTION_BREADCRUMBS, {
- 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<Codegen.GetCollectionsWithAssetsQuery>(gql`
- 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<
- Codegen.GetCollectionQuery,
- Codegen.GetCollectionQueryVariables
- >(GET_COLLECTION, {
- id: computersCollection.id,
- variantListOptions: {
- sort: {
- price: SortOrder.ASC,
- },
- },
- });
- 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,
- ]);
- });
- });
- describe('moveCollection', () => {
- it('moves a collection to a new parent', async () => {
- const result = await adminClient.query<
- Codegen.MoveCollectionMutation,
- Codegen.MoveCollectionMutationVariables
- >(MOVE_COLLECTION, {
- 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<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
- 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<
- Codegen.MoveCollectionMutation,
- Codegen.MoveCollectionMutationVariables
- >(MOVE_COLLECTION, {
- 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<
- Codegen.GetCollectionBreadcrumbsQuery,
- Codegen.GetCollectionBreadcrumbsQueryVariables
- >(GET_COLLECTION_BREADCRUMBS, {
- 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<Codegen.MoveCollectionMutation, Codegen.MoveCollectionMutationVariables>(
- MOVE_COLLECTION,
- {
- 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<Codegen.MoveCollectionMutation, Codegen.MoveCollectionMutationVariables>(
- MOVE_COLLECTION,
- {
- 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<Codegen.MoveCollectionMutation, Codegen.MoveCollectionMutationVariables>(
- MOVE_COLLECTION,
- {
- 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<Codegen.MoveCollectionMutation, Codegen.MoveCollectionMutationVariables>(
- MOVE_COLLECTION,
- {
- 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<
- Codegen.MoveCollectionMutation,
- Codegen.MoveCollectionMutationVariables
- >(MOVE_COLLECTION, {
- 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<
- Codegen.MoveCollectionMutation,
- Codegen.MoveCollectionMutationVariables
- >(MOVE_COLLECTION, {
- input: {
- collectionId: pearCollection.id,
- parentId: pearCollection.id,
- index: 0,
- },
- }),
- `Cannot move a Collection into itself`,
- ),
- );
- async function getChildrenOf(parentId: string): Promise<Array<{ name: string; id: string }>> {
- const result = await adminClient.query<Codegen.GetCollectionsQuery>(GET_COLLECTIONS);
- return result.collections.items.filter(i => i.parent!.id === parentId);
- }
- });
- describe('deleteCollection', () => {
- let collectionToDeleteParent: CreateCollectionMutation['createCollection'];
- let collectionToDeleteChild: CreateCollectionMutation['createCollection'];
- let laptopProductId: string;
- beforeAll(async () => {
- const result1 = await adminClient.query<
- Codegen.CreateCollectionMutation,
- Codegen.CreateCollectionMutationVariables
- >(CREATE_COLLECTION, {
- 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<
- Codegen.CreateCollectionMutation,
- Codegen.CreateCollectionMutationVariables
- >(CREATE_COLLECTION, {
- 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<
- Codegen.DeleteCollectionMutation,
- Codegen.DeleteCollectionMutationVariables
- >(DELETE_COLLECTION, {
- 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<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- id: collectionToDeleteParent.id,
- });
- 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<
- Codegen.GetProductCollectionsQuery,
- Codegen.GetProductCollectionsQueryVariables
- >(GET_PRODUCT_COLLECTIONS, {
- id: laptopProductId,
- });
- expect(product!.collections).toEqual([
- {
- id: 'T_3',
- name: 'Electronics',
- },
- {
- id: 'T_4',
- name: 'Computers',
- },
- {
- id: 'T_5',
- name: 'Pear',
- },
- {
- id: 'T_8',
- name: 'Computers Breadcrumbs',
- },
- {
- id: 'T_9',
- name: 'Electronics Breadcrumbs',
- },
- {
- id: 'T_10',
- name: 'Pear Breadcrumbs',
- },
- {
- id: 'T_11',
- name: 'Delete Me Parent',
- },
- {
- id: 'T_12',
- name: 'Delete Me Child',
- },
- ]);
- });
- it('deleteCollection works', async () => {
- const { deleteCollection } = await adminClient.query<
- Codegen.DeleteCollectionMutation,
- Codegen.DeleteCollectionMutationVariables
- >(DELETE_COLLECTION, {
- id: collectionToDeleteParent.id,
- });
- expect(deleteCollection.result).toBe(DeletionResult.DELETED);
- });
- it('deleted parent collection is null', async () => {
- const { collection } = await adminClient.query<
- Codegen.GetCollectionQuery,
- Codegen.GetCollectionQueryVariables
- >(GET_COLLECTION, {
- id: collectionToDeleteParent.id,
- });
- expect(collection).toBeNull();
- });
- it('deleted child collection is null', async () => {
- const { collection } = await adminClient.query<
- Codegen.GetCollectionQuery,
- Codegen.GetCollectionQueryVariables
- >(GET_COLLECTION, {
- id: collectionToDeleteChild.id,
- });
- expect(collection).toBeNull();
- });
- it('product no longer lists collection', async () => {
- const { product } = await adminClient.query<
- Codegen.GetProductCollectionsQuery,
- Codegen.GetProductCollectionsQueryVariables
- >(GET_PRODUCT_COLLECTIONS, {
- id: laptopProductId,
- });
- expect(product!.collections).toEqual([
- { id: 'T_3', name: 'Electronics' },
- { id: 'T_4', name: 'Computers' },
- { id: 'T_5', name: 'Pear' },
- {
- id: 'T_8',
- name: 'Computers Breadcrumbs',
- },
- {
- id: 'T_9',
- name: 'Electronics Breadcrumbs',
- },
- {
- id: 'T_10',
- name: 'Pear Breadcrumbs',
- },
- ]);
- });
- });
- describe('filters', () => {
- it('Collection with no filters has no productVariants', async () => {
- const result = await adminClient.query<
- Codegen.CreateCollectionSelectVariantsMutation,
- Codegen.CreateCollectionSelectVariantsMutationVariables
- >(CREATE_COLLECTION_SELECT_VARIANTS, {
- input: {
- translations: [
- { languageCode: LanguageCode.en, name: 'Empty', description: '', slug: 'empty' },
- ],
- filters: [],
- } as CreateCollectionInput,
- });
- expect(result.createCollection.productVariants.totalItems).toBe(0);
- });
- describe('facetValue filter', () => {
- it('electronics', async () => {
- const result = await adminClient.query<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- id: electronicsCollection.id,
- });
- 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<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- id: computersCollection.id,
- });
- 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<
- Codegen.CreateCollectionSelectVariantsMutation,
- Codegen.CreateCollectionSelectVariantsMutationVariables
- >(CREATE_COLLECTION_SELECT_VARIANTS, {
- 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`,
- },
- ],
- },
- ],
- } as CreateCollectionInput,
- });
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await adminClient.query<
- Codegen.GetCollectionQuery,
- Codegen.GetCollectionQueryVariables
- >(GET_COLLECTION, {
- id: result.createCollection.id,
- });
- expect(collection!.productVariants.items.map(i => i.name)).toEqual(['Instant Camera']);
- });
- it('photo OR pear', async () => {
- const result = await adminClient.query<
- Codegen.CreateCollectionSelectVariantsMutation,
- Codegen.CreateCollectionSelectVariantsMutationVariables
- >(CREATE_COLLECTION_SELECT_VARIANTS, {
- 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`,
- },
- ],
- },
- ],
- } as CreateCollectionInput,
- });
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await adminClient.query<
- Codegen.GetCollectionQuery,
- Codegen.GetCollectionQueryVariables
- >(GET_COLLECTION, {
- id: result.createCollection.id,
- });
- 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<
- Codegen.CreateCollectionSelectVariantsMutation,
- Codegen.CreateCollectionSelectVariantsMutationVariables
- >(CREATE_COLLECTION_SELECT_VARIANTS, {
- 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`,
- },
- ],
- },
- ],
- } as CreateCollectionInput,
- });
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await adminClient.query<
- Codegen.GetCollectionQuery,
- Codegen.GetCollectionQueryVariables
- >(GET_COLLECTION, {
- id: result.createCollection.id,
- });
- 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<CollectionFragment> {
- const { createCollection } = await adminClient.query<
- Codegen.CreateCollectionMutation,
- Codegen.CreateCollectionMutationVariables
- >(CREATE_COLLECTION, {
- 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<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- id: collection.id,
- });
- 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<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- id: collection.id,
- });
- 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<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- id: collection.id,
- });
- 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<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- id: collection.id,
- });
- 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<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- 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<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- 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('re-evaluation of contents on changes', () => {
- let products: GetProductsWithVariantIdsQuery['products']['items'];
- beforeAll(async () => {
- const result = await adminClient.query<Codegen.GetProductsWithVariantIdsQuery>(gql`
- query GetProductsWithVariantIds {
- products(options: { sort: { id: ASC } }) {
- items {
- id
- name
- variants {
- id
- name
- }
- }
- }
- }
- `);
- products = result.products.items;
- });
- it('updates contents when Product is updated', async () => {
- await adminClient.query<
- Codegen.UpdateProductMutation,
- Codegen.UpdateProductMutationVariables
- >(UPDATE_PRODUCT, {
- input: {
- id: products[1].id,
- facetValueIds: [
- getFacetValueId('electronics'),
- getFacetValueId('computers'),
- getFacetValueId('pear'),
- ],
- },
- });
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
- 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<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: gamingPc240GB.id,
- facetValueIds: [getFacetValueId('pear')],
- },
- ],
- });
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
- 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<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: gamingPc240GB.id,
- facetValueIds: [getFacetValueId('electronics'), getFacetValueId('pear')],
- },
- ],
- });
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
- 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<
- Codegen.CreateCollectionSelectVariantsMutation,
- Codegen.CreateCollectionSelectVariantsMutationVariables
- >(CREATE_COLLECTION_SELECT_VARIANTS, {
- 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`,
- },
- ],
- },
- ],
- } as CreateCollectionInput,
- });
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearElectronics.id });
- 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<
- Codegen.CreateCollectionSelectVariantsMutation,
- Codegen.CreateCollectionSelectVariantsMutationVariables
- >(CREATE_COLLECTION_SELECT_VARIANTS, {
- 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`,
- },
- ],
- },
- ],
- } as CreateCollectionInput,
- });
- await awaitRunningJobs(adminClient, 5000);
- clothesCollectionId = clothesCollection.id;
- const result = await adminClient.query<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, { id: clothesCollection.id });
- 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<
- Codegen.CreateCollectionSelectVariantsMutation,
- Codegen.CreateCollectionSelectVariantsMutationVariables
- >(CREATE_COLLECTION_SELECT_VARIANTS, {
- 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`,
- },
- ],
- },
- ],
- } as CreateCollectionInput,
- });
- await awaitRunningJobs(adminClient, 5000);
- const result = await adminClient.query<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, { id: footwearCollection.id });
- expect(result.collection!.productVariants.items.map(i => i.name)).toEqual(['Boots']);
- });
- });
- });
- describe('Product collections property', () => {
- it('returns all collections to which the Product belongs', async () => {
- const result = await adminClient.query<
- Codegen.GetCollectionsForProductsQuery,
- Codegen.GetCollectionsForProductsQueryVariables
- >(GET_COLLECTIONS_FOR_PRODUCTS, { term: 'camera' });
- expect(result.products.items[0].collections).toEqual([
- {
- id: 'T_3',
- name: 'Electronics',
- },
- {
- id: 'T_5',
- name: 'Pear',
- },
- {
- id: 'T_9',
- name: 'Electronics Breadcrumbs',
- },
- {
- id: 'T_14',
- name: 'Photo AND Pear',
- },
- {
- id: 'T_15',
- name: 'Photo OR Pear',
- },
- {
- id: 'T_17',
- name: 'contains camera',
- },
- {
- id: 'T_19',
- name: 'endsWith camera',
- },
- {
- id: 'T_23',
- name: 'pear electronics',
- },
- ]);
- });
- });
- describe('productVariants list', () => {
- it('does not list variants from deleted products', async () => {
- await adminClient.query<Codegen.DeleteProductMutation, Codegen.DeleteProductMutationVariables>(
- DELETE_PRODUCT,
- {
- id: 'T_2', // curvy monitor
- },
- );
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await adminClient.query<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- id: pearCollection.id,
- });
- 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<
- Codegen.DeleteProductVariantMutation,
- Codegen.DeleteProductVariantMutationVariables
- >(DELETE_PRODUCT_VARIANT, {
- id: 'T_18', // Instant Camera
- });
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await adminClient.query<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- id: pearCollection.id,
- });
- 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<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [{ id: 'T_1', enabled: false }],
- });
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await shopClient.query<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- id: pearCollection.id,
- });
- 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<Codegen.UpdateProductMutation, Codegen.UpdateProductMutationVariables>(
- UPDATE_PRODUCT,
- {
- input: { id: 'T_1', enabled: false },
- },
- );
- await awaitRunningJobs(adminClient, 5000);
- const { collection } = await shopClient.query<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(GET_COLLECTION_PRODUCT_VARIANTS, {
- id: pearCollection.id,
- });
- 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<Codegen.UpdateProductMutation, Codegen.UpdateProductMutationVariables>(
- UPDATE_PRODUCT,
- {
- input: { id: 'T_1', enabled: true },
- },
- );
- await adminClient.query<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: 'T_2',
- translations: [{ languageCode: LanguageCode.de, name: 'Taschenrechner 15 Zoll' }],
- },
- ],
- });
- const { collection } = await shopClient.query<
- Codegen.GetCollectionProductsQuery,
- Codegen.GetCollectionProductsQueryVariables
- >(
- GET_COLLECTION_PRODUCT_VARIANTS,
- {
- id: pearCollection.id,
- },
- { languageCode: LanguageCode.de },
- );
- 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',
- ]);
- });
- });
- 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 GET_COLLECTION = gql`
- query GetCollection($id: ID, $slug: String, $variantListOptions: ProductVariantListOptions) {
- collection(id: $id, slug: $slug) {
- ...Collection
- productVariants(options: $variantListOptions) {
- items {
- id
- name
- price
- }
- }
- }
- }
- ${COLLECTION_FRAGMENT}
- `;
- export const MOVE_COLLECTION = gql`
- mutation MoveCollection($input: MoveCollectionInput!) {
- moveCollection(input: $input) {
- ...Collection
- }
- }
- ${COLLECTION_FRAGMENT}
- `;
- const GET_FACET_VALUES = gql`
- query GetFacetValues {
- facets {
- items {
- values {
- ...FacetValue
- }
- }
- }
- }
- ${FACET_VALUE_FRAGMENT}
- `;
- const GET_COLLECTION_PRODUCT_VARIANTS = gql`
- query GetCollectionProducts($id: ID!) {
- collection(id: $id) {
- productVariants(options: { sort: { id: ASC } }) {
- items {
- id
- name
- facetValues {
- code
- }
- productId
- }
- }
- }
- }
- `;
- const CREATE_COLLECTION_SELECT_VARIANTS = gql`
- mutation CreateCollectionSelectVariants($input: CreateCollectionInput!) {
- createCollection(input: $input) {
- id
- productVariants {
- items {
- name
- }
- totalItems
- }
- }
- }
- `;
- const GET_COLLECTION_BREADCRUMBS = gql`
- query GetCollectionBreadcrumbs($id: ID!) {
- collection(id: $id) {
- breadcrumbs {
- id
- name
- slug
- }
- }
- }
- `;
- const GET_COLLECTIONS_FOR_PRODUCTS = gql`
- query GetCollectionsForProducts($term: String!) {
- products(options: { filter: { name: { contains: $term } } }) {
- items {
- id
- name
- collections {
- id
- name
- }
- }
- }
- }
- `;
- const DELETE_COLLECTION = gql`
- mutation DeleteCollection($id: ID!) {
- deleteCollection(id: $id) {
- result
- message
- }
- }
- `;
- const GET_PRODUCT_COLLECTIONS = gql`
- query GetProductCollections($id: ID!) {
- product(id: $id) {
- id
- collections {
- id
- name
- }
- }
- }
- `;
- const GET_PRODUCT_COLLECTIONS_WITH_PARENT = gql`
- query GetProductCollectionsWithParent($id: ID!) {
- product(id: $id) {
- id
- collections {
- id
- name
- parent {
- id
- name
- }
- }
- }
- }
- `;
- const GET_COLLECTION_NESTED_PARENTS = gql`
- query GetCollectionNestedParents {
- collections {
- items {
- id
- name
- parent {
- name
- parent {
- name
- parent {
- name
- }
- }
- }
- }
- }
- }
- `;
|