| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932 |
- import { DeletionResult, ErrorCode, LanguageCode, SortOrder } from '@vendure/common/lib/generated-types';
- import { omit } from '@vendure/common/lib/omit';
- import { pick } from '@vendure/common/lib/pick';
- import { notNullOrUndefined } from '@vendure/common/lib/shared-utils';
- import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } from '@vendure/testing';
- import path from 'path';
- import { afterAll, beforeAll, describe, expect, it } from 'vitest';
- import type {
- productVariantFragment,
- productWithOptionsFragment,
- productWithVariantsFragment,
- } from './graphql/fragments-admin';
- import type { ResultOf, VariablesOf } from './graphql/graphql-admin';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
- import {
- addOptionGroupToProductDocument,
- createProductDocument,
- createProductOptionGroupDocument,
- createProductVariantsDocument,
- deleteProductDocument,
- deleteProductVariantDocument,
- getAssetListDocument,
- getOptionGroupDocument,
- getProductListDocument,
- getProductSimpleDocument,
- getProductVariantDocument,
- getProductVariantListDocument,
- getProductWithVariantListDocument,
- getProductWithVariantsDocument,
- removeOptionGroupFromProductDocument,
- updateChannelDocument,
- updateGlobalSettingsDocument,
- updateProductDocument,
- updateProductVariantsDocument,
- } from './graphql/shared-definitions';
- import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
- describe('Product resolver', () => {
- const { server, adminClient, shopClient } = createTestEnvironment({
- ...testConfig(),
- });
- const removeOptionGuard: ErrorResultGuard<ResultOf<typeof productWithOptionsFragment>> =
- createErrorResultGuard(input => !!input && 'optionGroups' in input);
- const updateChannelGuard: ErrorResultGuard<ResultOf<typeof updateChannelDocument>['updateChannel']> =
- createErrorResultGuard(input => !!input && 'id' in input);
- const productGuard: ErrorResultGuard<
- NonNullable<ResultOf<typeof getProductWithVariantsDocument>['product']>
- > = createErrorResultGuard(input => !!input && 'id' in input);
- const variantGuard: ErrorResultGuard<
- NonNullable<ResultOf<typeof createProductVariantsDocument>['createProductVariants'][number]>
- > = createErrorResultGuard(input => !!input && 'id' in input);
- const productQueryGuard: ErrorResultGuard<
- NonNullable<ResultOf<typeof getProductSimpleDocument>['product']>
- > = createErrorResultGuard(input => !!input && 'id' in input);
- const updateVariantGuard: ErrorResultGuard<
- NonNullable<ResultOf<typeof updateProductVariantsDocument>['updateProductVariants'][number]>
- > = createErrorResultGuard(input => !!input && 'id' in input);
- beforeAll(async () => {
- await server.init({
- initialData,
- customerCount: 1,
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
- });
- await adminClient.asSuperAdmin();
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- describe('products list query', () => {
- it('returns all products when no options passed', async () => {
- const result = await adminClient.query(getProductListDocument, {});
- expect(result.products.items.length).toBe(20);
- expect(result.products.totalItems).toBe(20);
- });
- it('limits result set with skip & take', async () => {
- const result = await adminClient.query(getProductListDocument, {
- options: {
- skip: 0,
- take: 3,
- },
- });
- expect(result.products.items.length).toBe(3);
- expect(result.products.totalItems).toBe(20);
- });
- it('filters by name admin', async () => {
- const result = await adminClient.query(getProductListDocument, {
- options: {
- filter: {
- name: {
- contains: 'skateboard',
- },
- },
- },
- });
- expect(result.products.items.length).toBe(1);
- expect(result.products.items[0].name).toBe('Cruiser Skateboard');
- });
- it('filters multiple admin', async () => {
- const result = await adminClient.query(getProductListDocument, {
- options: {
- filter: {
- name: {
- contains: 'camera',
- },
- slug: {
- contains: 'tent',
- },
- },
- },
- });
- expect(result.products.items.length).toBe(0);
- });
- it('sorts by name admin', async () => {
- const result = await adminClient.query(getProductListDocument, {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- },
- });
- expect(result.products.items.map(p => p.name)).toEqual([
- 'Bonsai Tree',
- 'Boxing Gloves',
- 'Camera Lens',
- 'Clacky Keyboard',
- 'Cruiser Skateboard',
- 'Curvy Monitor',
- 'Football',
- 'Gaming PC',
- 'Hard Drive',
- 'Instant Camera',
- 'Laptop',
- 'Orchid',
- 'Road Bike',
- 'Running Shoe',
- 'Skipping Rope',
- 'Slr Camera',
- 'Spiky Cactus',
- 'Tent',
- 'Tripod',
- 'USB Cable',
- ]);
- });
- it('filters by name shop', async () => {
- const result = await shopClient.query(getProductListDocument, {
- options: {
- filter: {
- name: {
- contains: 'skateboard',
- },
- },
- },
- });
- expect(result.products.items.length).toBe(1);
- expect(result.products.items[0].name).toBe('Cruiser Skateboard');
- });
- it('filters by sku admin', async () => {
- const result = await adminClient.query(getProductListDocument, {
- options: {
- filter: {
- sku: {
- contains: 'IHD455T1',
- },
- },
- },
- });
- expect(result.products.items.length).toBe(1);
- expect(result.products.items[0].name).toBe('Hard Drive');
- });
- it('sorts by name shop', async () => {
- const result = await shopClient.query(getProductListDocument, {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- },
- });
- expect(result.products.items.map(p => p.name)).toEqual([
- 'Bonsai Tree',
- 'Boxing Gloves',
- 'Camera Lens',
- 'Clacky Keyboard',
- 'Cruiser Skateboard',
- 'Curvy Monitor',
- 'Football',
- 'Gaming PC',
- 'Hard Drive',
- 'Instant Camera',
- 'Laptop',
- 'Orchid',
- 'Road Bike',
- 'Running Shoe',
- 'Skipping Rope',
- 'Slr Camera',
- 'Spiky Cactus',
- 'Tent',
- 'Tripod',
- 'USB Cable',
- ]);
- });
- });
- describe('product query', () => {
- it('by id', async () => {
- const { product } = await adminClient.query(getProductSimpleDocument, { id: 'T_2' });
- productQueryGuard.assertSuccess(product);
- expect(product.id).toBe('T_2');
- });
- it('by slug', async () => {
- const { product } = await adminClient.query(getProductSimpleDocument, { slug: 'curvy-monitor' });
- productQueryGuard.assertSuccess(product);
- expect(product.slug).toBe('curvy-monitor');
- });
- // https://github.com/vendure-ecommerce/vendure/issues/820
- it('by slug with multiple assets', async () => {
- const { product: product1 } = await adminClient.query(getProductSimpleDocument, { id: 'T_1' });
- productQueryGuard.assertSuccess(product1);
- await adminClient.query(updateProductDocument, {
- input: {
- id: product1.id,
- assetIds: ['T_1', 'T_2', 'T_3'],
- },
- });
- const { product } = await adminClient.query(getProductWithVariantsDocument, {
- slug: product1.slug,
- });
- productQueryGuard.assertSuccess(product);
- expect(product.assets.map(a => a.id)).toEqual(['T_1', 'T_2', 'T_3']);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/538
- it('falls back to default language slug', async () => {
- const { product } = await adminClient.query(
- getProductSimpleDocument,
- { slug: 'curvy-monitor' },
- { languageCode: LanguageCode.de },
- );
- productQueryGuard.assertSuccess(product);
- expect(product.slug).toBe('curvy-monitor');
- });
- it(
- 'throws if neither id nor slug provided',
- assertThrowsWithMessage(async () => {
- await adminClient.query(getProductSimpleDocument, {});
- }, 'Either the Product id or slug must be provided'),
- );
- it(
- 'throws if id and slug do not refer to the same Product',
- assertThrowsWithMessage(async () => {
- await adminClient.query(getProductSimpleDocument, {
- id: 'T_2',
- slug: 'laptop',
- });
- }, 'The provided id and slug refer to different Products'),
- );
- it('returns expected properties', async () => {
- const { product } = await adminClient.query(getProductWithVariantsDocument, {
- id: 'T_2',
- });
- productGuard.assertSuccess(product);
- expect(omit(product, ['variants'])).toMatchSnapshot();
- expect(product.variants.length).toBe(2);
- });
- it('ProductVariant price properties are correct', async () => {
- const result = await adminClient.query(getProductWithVariantsDocument, {
- id: 'T_2',
- });
- productGuard.assertSuccess(result.product);
- expect(result.product.variants[0].price).toBe(14374);
- expect(result.product.variants[0].taxCategory).toEqual({
- id: 'T_1',
- name: 'Standard Tax',
- });
- });
- it('returns null when id not found', async () => {
- const result = await adminClient.query(getProductWithVariantsDocument, {
- id: 'bad_id',
- });
- expect(result.product).toBeNull();
- });
- it('returns null when slug not found', async () => {
- const result = await adminClient.query(getProductWithVariantsDocument, {
- slug: 'bad_slug',
- });
- expect(result.product).toBeNull();
- });
- describe('product query with translations', () => {
- let translatedProduct: ResultOf<typeof productWithVariantsFragment>;
- let en_translation: ResultOf<typeof productWithVariantsFragment>['translations'][number];
- let de_translation: ResultOf<typeof productWithVariantsFragment>['translations'][number];
- beforeAll(async () => {
- const result = await adminClient.query(createProductDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'en Pineapple',
- slug: 'en-pineapple',
- description: 'A delicious pineapple',
- },
- {
- languageCode: LanguageCode.de,
- name: 'de Ananas',
- slug: 'de-ananas',
- description: 'Eine köstliche Ananas',
- },
- ],
- },
- });
- translatedProduct = result.createProduct;
- const en = translatedProduct.translations.find(t => t.languageCode === LanguageCode.en);
- const de = translatedProduct.translations.find(t => t.languageCode === LanguageCode.de);
- expect(en).toBeDefined();
- expect(de).toBeDefined();
- en_translation = en as typeof en_translation;
- de_translation = de as typeof de_translation;
- });
- it('en slug without translation arg', async () => {
- const { product } = await adminClient.query(getProductSimpleDocument, {
- slug: en_translation.slug,
- });
- productQueryGuard.assertSuccess(product);
- expect(product.slug).toBe(en_translation.slug);
- });
- it('de slug without translation arg', async () => {
- const { product } = await adminClient.query(getProductSimpleDocument, {
- slug: de_translation.slug,
- });
- productQueryGuard.assertSuccess(product);
- expect(product.slug).toBe(en_translation.slug);
- });
- it('en slug with translation en', async () => {
- const { product } = await adminClient.query(
- getProductSimpleDocument,
- { slug: en_translation.slug },
- { languageCode: LanguageCode.en },
- );
- productQueryGuard.assertSuccess(product);
- expect(product.slug).toBe(en_translation.slug);
- });
- it('de slug with translation en', async () => {
- const { product } = await adminClient.query(
- getProductSimpleDocument,
- { slug: de_translation.slug },
- { languageCode: LanguageCode.en },
- );
- productQueryGuard.assertSuccess(product);
- expect(product.slug).toBe(en_translation.slug);
- });
- it('en slug with translation de', async () => {
- const { product } = await adminClient.query(
- getProductSimpleDocument,
- { slug: en_translation.slug },
- { languageCode: LanguageCode.de },
- );
- productQueryGuard.assertSuccess(product);
- expect(product.slug).toBe(de_translation.slug);
- });
- it('de slug with translation de', async () => {
- const { product } = await adminClient.query(
- getProductSimpleDocument,
- { slug: de_translation.slug },
- { languageCode: LanguageCode.de },
- );
- productQueryGuard.assertSuccess(product);
- expect(product.slug).toBe(de_translation.slug);
- });
- it('de slug with translation ru', async () => {
- const { product } = await adminClient.query(
- getProductSimpleDocument,
- { slug: de_translation.slug },
- { languageCode: LanguageCode.ru },
- );
- productQueryGuard.assertSuccess(product);
- expect(product.slug).toBe(en_translation.slug);
- });
- });
- describe('product.variants', () => {
- it('returns product variants', async () => {
- const { product } = await adminClient.query(getProductWithVariantsDocument, {
- id: 'T_1',
- });
- expect(product?.variants.length).toBe(4);
- });
- it('returns product variants in existing language', async () => {
- const { product } = await adminClient.query(
- getProductWithVariantsDocument,
- {
- id: 'T_1',
- },
- { languageCode: LanguageCode.en },
- );
- expect(product?.variants.length).toBe(4);
- });
- it('returns product variants in non-existing language', async () => {
- const { product } = await adminClient.query(
- getProductWithVariantsDocument,
- {
- id: 'T_1',
- },
- { languageCode: LanguageCode.ru },
- );
- expect(product?.variants.length).toBe(4);
- });
- });
- describe('product.variants', () => {
- it('returns product variants', async () => {
- const { product } = await adminClient.query(getProductWithVariantsDocument, {
- id: 'T_1',
- });
- expect(product?.variants.length).toBe(4);
- });
- it('returns product variants in existing language', async () => {
- const { product } = await adminClient.query(
- getProductWithVariantsDocument,
- {
- id: 'T_1',
- },
- { languageCode: LanguageCode.en },
- );
- expect(product?.variants.length).toBe(4);
- });
- it('returns product variants in non-existing language', async () => {
- const { product } = await adminClient.query(
- getProductWithVariantsDocument,
- {
- id: 'T_1',
- },
- { languageCode: LanguageCode.ru },
- );
- expect(product?.variants.length).toBe(4);
- });
- });
- describe('product.variantList', () => {
- it('returns product variants', async () => {
- const { product } = await adminClient.query(getProductWithVariantListDocument, {
- id: 'T_1',
- });
- expect(product?.variantList.items.length).toBe(4);
- expect(product?.variantList.totalItems).toBe(4);
- });
- it('returns product variants in existing language', async () => {
- const { product } = await adminClient.query(
- getProductWithVariantListDocument,
- {
- id: 'T_1',
- },
- { languageCode: LanguageCode.en },
- );
- expect(product?.variantList.items.length).toBe(4);
- });
- it('returns product variants in non-existing language', async () => {
- const { product } = await adminClient.query(
- getProductWithVariantListDocument,
- {
- id: 'T_1',
- },
- { languageCode: LanguageCode.ru },
- );
- expect(product?.variantList.items.length).toBe(4);
- });
- it('filter & sort', async () => {
- const { product } = await adminClient.query(getProductWithVariantListDocument, {
- id: 'T_1',
- variantListOptions: {
- filter: {
- name: {
- contains: '15',
- },
- },
- sort: {
- price: SortOrder.DESC,
- },
- },
- });
- expect(product?.variantList.items.map(i => i.name)).toEqual([
- 'Laptop 15 inch 16GB',
- 'Laptop 15 inch 8GB',
- ]);
- });
- });
- });
- describe('productVariants list query', () => {
- it('returns list', async () => {
- const { productVariants } = await adminClient.query(getProductVariantListDocument, {
- options: {
- take: 3,
- sort: {
- name: SortOrder.ASC,
- },
- },
- });
- expect(
- productVariants.items.map(i => pick(i, ['id', 'name', 'price', 'priceWithTax', 'sku'])),
- ).toEqual([
- {
- id: 'T_34',
- name: 'Bonsai Tree',
- price: 1999,
- priceWithTax: 2399,
- sku: 'B01MXFLUSV',
- },
- {
- id: 'T_24',
- name: 'Boxing Gloves',
- price: 3304,
- priceWithTax: 3965,
- sku: 'B000ZYLPPU',
- },
- {
- id: 'T_19',
- name: 'Camera Lens',
- price: 10400,
- priceWithTax: 12480,
- sku: 'B0012UUP02',
- },
- ]);
- });
- it('sort by price', async () => {
- const { productVariants } = await adminClient.query(getProductVariantListDocument, {
- options: {
- take: 3,
- sort: {
- price: SortOrder.ASC,
- },
- },
- });
- expect(
- productVariants.items.map(i => pick(i, ['id', 'name', 'price', 'priceWithTax', 'sku'])),
- ).toEqual([
- {
- id: 'T_23',
- name: 'Skipping Rope',
- price: 799,
- priceWithTax: 959,
- sku: 'B07CNGXVXT',
- },
- {
- id: 'T_20',
- name: 'Tripod',
- price: 1498,
- priceWithTax: 1798,
- sku: 'B00XI87KV8',
- },
- {
- id: 'T_32',
- name: 'Spiky Cactus',
- price: 1550,
- priceWithTax: 1860,
- sku: 'SC011001',
- },
- ]);
- });
- it('sort by priceWithTax', async () => {
- const { productVariants } = await adminClient.query(getProductVariantListDocument, {
- options: {
- take: 3,
- sort: {
- priceWithTax: SortOrder.ASC,
- },
- },
- });
- expect(
- productVariants.items.map(i => pick(i, ['id', 'name', 'price', 'priceWithTax', 'sku'])),
- ).toEqual([
- {
- id: 'T_23',
- name: 'Skipping Rope',
- price: 799,
- priceWithTax: 959,
- sku: 'B07CNGXVXT',
- },
- {
- id: 'T_20',
- name: 'Tripod',
- price: 1498,
- priceWithTax: 1798,
- sku: 'B00XI87KV8',
- },
- {
- id: 'T_32',
- name: 'Spiky Cactus',
- price: 1550,
- priceWithTax: 1860,
- sku: 'SC011001',
- },
- ]);
- });
- it('filter by price', async () => {
- const { productVariants } = await adminClient.query(getProductVariantListDocument, {
- options: {
- take: 3,
- filter: {
- price: {
- between: {
- start: 1400,
- end: 1500,
- },
- },
- },
- },
- });
- expect(
- productVariants.items.map(i => pick(i, ['id', 'name', 'price', 'priceWithTax', 'sku'])),
- ).toEqual([
- {
- id: 'T_20',
- name: 'Tripod',
- price: 1498,
- priceWithTax: 1798,
- sku: 'B00XI87KV8',
- },
- ]);
- });
- it('filter by priceWithTax', async () => {
- const { productVariants } = await adminClient.query(getProductVariantListDocument, {
- options: {
- take: 3,
- filter: {
- priceWithTax: {
- between: {
- start: 1400,
- end: 1500,
- },
- },
- },
- },
- });
- // Note the results are incorrect. This is a design trade-off. See the
- // commend on the ProductVariant.priceWithTax annotation for explanation.
- expect(
- productVariants.items.map(i => pick(i, ['id', 'name', 'price', 'priceWithTax', 'sku'])),
- ).toEqual([
- {
- id: 'T_20',
- name: 'Tripod',
- price: 1498,
- priceWithTax: 1798,
- sku: 'B00XI87KV8',
- },
- ]);
- });
- it('returns variants for particular product by id', async () => {
- const { productVariants } = await adminClient.query(getProductVariantListDocument, {
- options: {
- take: 3,
- sort: {
- price: SortOrder.ASC,
- },
- },
- productId: 'T_1',
- });
- expect(
- productVariants.items.map(i => pick(i, ['id', 'name', 'price', 'priceWithTax', 'sku'])),
- ).toEqual([
- {
- id: 'T_1',
- name: 'Laptop 13 inch 8GB',
- price: 129900,
- priceWithTax: 155880,
- sku: 'L2201308',
- },
- {
- id: 'T_2',
- name: 'Laptop 15 inch 8GB',
- price: 139900,
- priceWithTax: 167880,
- sku: 'L2201508',
- },
- {
- id: 'T_3',
- name: 'Laptop 13 inch 16GB',
- priceWithTax: 263880,
- price: 219900,
- sku: 'L2201316',
- },
- ]);
- });
- });
- describe('productVariant query', () => {
- it('by id', async () => {
- const { productVariant } = await adminClient.query(getProductVariantDocument, {
- id: 'T_1',
- });
- expect(productVariant?.id).toBe('T_1');
- expect(productVariant?.name).toBe('Laptop 13 inch 8GB');
- });
- it('returns null when id not found', async () => {
- const { productVariant } = await adminClient.query(getProductVariantDocument, {
- id: 'T_999',
- });
- expect(productVariant).toBeNull();
- });
- });
- describe('product mutation', () => {
- let newTranslatedProduct: ResultOf<typeof productWithVariantsFragment>;
- let newProduct: ResultOf<typeof productWithVariantsFragment>;
- let newProductWithAssets: ResultOf<typeof productWithVariantsFragment>;
- it('createProduct creates a new Product', async () => {
- const result = await adminClient.query(createProductDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'en Baked Potato',
- slug: 'en Baked Potato',
- description: 'A baked potato',
- },
- {
- languageCode: LanguageCode.de,
- name: 'de Baked Potato',
- slug: 'de-baked-potato',
- description: 'Eine baked Erdapfel',
- },
- ],
- },
- });
- expect(omit(result.createProduct, ['translations'])).toMatchSnapshot();
- expect(result.createProduct.translations.map(t => t.description).sort()).toEqual([
- 'A baked potato',
- 'Eine baked Erdapfel',
- ]);
- newTranslatedProduct = result.createProduct;
- });
- it('createProduct creates a new Product with assets', async () => {
- const assetsResult = await adminClient.query(getAssetListDocument);
- const assetIds = assetsResult.assets.items.slice(0, 2).map(a => a.id);
- const featuredAssetId = assetsResult.assets.items[0].id;
- const result = await adminClient.query(createProductDocument, {
- input: {
- assetIds,
- featuredAssetId,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'en Has Assets',
- slug: 'en-has-assets',
- description: 'A product with assets',
- },
- ],
- },
- });
- expect(result.createProduct.assets.map(a => a.id)).toEqual(assetIds);
- expect(result.createProduct.featuredAsset).toBeDefined();
- expect(result.createProduct.featuredAsset?.id).toBe(featuredAssetId);
- newProductWithAssets = result.createProduct;
- });
- it('createProduct creates a disabled Product', async () => {
- const result = await adminClient.query(createProductDocument, {
- input: {
- enabled: false,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'en Small apple',
- slug: 'en-small-apple',
- description: 'A small apple',
- },
- ],
- },
- });
- expect(result.createProduct.enabled).toBe(false);
- newProduct = result.createProduct;
- });
- it('updateProduct updates a Product', async () => {
- const result = await adminClient.query(updateProductDocument, {
- input: {
- id: newProduct.id,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'en Mashed Potato',
- slug: 'en-mashed-potato',
- description: 'A blob of mashed potato',
- },
- {
- languageCode: LanguageCode.de,
- name: 'de Mashed Potato',
- slug: 'de-mashed-potato',
- description: 'Eine blob von gemashed Erdapfel',
- },
- ],
- },
- });
- expect(result.updateProduct.translations.map(t => t.description).sort()).toEqual([
- 'A blob of mashed potato',
- 'Eine blob von gemashed Erdapfel',
- ]);
- });
- it('slug is normalized to be url-safe', async () => {
- const result = await adminClient.query(updateProductDocument, {
- input: {
- id: newProduct.id,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'en Mashed Potato',
- slug: 'A (very) nice potato!!',
- description: 'A blob of mashed potato',
- },
- ],
- },
- });
- expect(result.updateProduct.slug).toBe('a-very-nice-potato');
- });
- it('create with duplicate slug is renamed to be unique', async () => {
- const result = await adminClient.query(createProductDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Another baked potato',
- slug: 'a-very-nice-potato',
- description: 'Another baked potato but a bit different',
- },
- ],
- },
- });
- expect(result.createProduct.slug).toBe('a-very-nice-potato-2');
- });
- it('update with duplicate slug is renamed to be unique', async () => {
- const result = await adminClient.query(updateProductDocument, {
- input: {
- id: newProduct.id,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Yet another baked potato',
- slug: 'a-very-nice-potato-2',
- description: 'Possibly the final baked potato',
- },
- ],
- },
- });
- expect(result.updateProduct.slug).toBe('a-very-nice-potato-3');
- });
- it('slug duplicate check does not include self', async () => {
- const result = await adminClient.query(updateProductDocument, {
- input: {
- id: newProduct.id,
- translations: [
- {
- languageCode: LanguageCode.en,
- slug: 'a-very-nice-potato-3',
- },
- ],
- },
- });
- expect(result.updateProduct.slug).toBe('a-very-nice-potato-3');
- });
- it('updateProduct accepts partial input', async () => {
- const result = await adminClient.query(updateProductDocument, {
- input: {
- id: newProduct.id,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'en Very Mashed Potato',
- },
- ],
- },
- });
- expect(result.updateProduct.translations.length).toBe(2);
- const deTranslation = result.updateProduct.translations.find(
- t => t.languageCode === LanguageCode.de,
- );
- const enTranslation = result.updateProduct.translations.find(
- t => t.languageCode === LanguageCode.en,
- );
- expect(deTranslation).toBeDefined();
- expect(enTranslation).toBeDefined();
- expect(deTranslation?.name).toBe('de Mashed Potato');
- expect(enTranslation?.name).toBe('en Very Mashed Potato');
- expect(enTranslation?.description).toBe('Possibly the final baked potato');
- });
- it('updateProduct adds Assets to a product and sets featured asset', async () => {
- const assetsResult = await adminClient.query(getAssetListDocument);
- const assetIds = assetsResult.assets.items.map(a => a.id);
- const featuredAssetId = assetsResult.assets.items[2].id;
- const result = await adminClient.query(updateProductDocument, {
- input: {
- id: newProduct.id,
- assetIds,
- featuredAssetId,
- },
- });
- expect(result.updateProduct.assets.map(a => a.id)).toEqual(assetIds);
- expect(result.updateProduct.featuredAsset).toBeDefined();
- expect(result.updateProduct.featuredAsset?.id).toBe(featuredAssetId);
- });
- it('updateProduct sets a featured asset', async () => {
- const productResult = await adminClient.query(getProductWithVariantsDocument, {
- id: newProduct.id,
- });
- productGuard.assertSuccess(productResult.product);
- const assets = productResult.product.assets;
- const result = await adminClient.query(updateProductDocument, {
- input: {
- id: newProduct.id,
- featuredAssetId: assets[0].id,
- },
- });
- expect(result.updateProduct.featuredAsset).toBeDefined();
- expect(result.updateProduct.featuredAsset?.id).toBe(assets[0].id);
- });
- it('updateProduct updates assets', async () => {
- const result = await adminClient.query(updateProductDocument, {
- input: {
- id: newProduct.id,
- featuredAssetId: 'T_1',
- assetIds: ['T_1', 'T_2'],
- },
- });
- expect(result.updateProduct.assets.map(a => a.id)).toEqual(['T_1', 'T_2']);
- });
- it('updateProduct updates FacetValues', async () => {
- const result = await adminClient.query(updateProductDocument, {
- input: {
- id: newProduct.id,
- facetValueIds: ['T_1'],
- },
- });
- expect(result.updateProduct.facetValues.length).toEqual(1);
- });
- it(
- 'updateProduct errors with an invalid productId',
- assertThrowsWithMessage(
- () =>
- adminClient.query(updateProductDocument, {
- input: {
- id: '999',
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'en Mashed Potato',
- slug: 'en-mashed-potato',
- description: 'A blob of mashed potato',
- },
- {
- languageCode: LanguageCode.de,
- name: 'de Mashed Potato',
- slug: 'de-mashed-potato',
- description: 'Eine blob von gemashed Erdapfel',
- },
- ],
- },
- }),
- 'No Product with the id "999" could be found',
- ),
- );
- it('addOptionGroupToProduct adds an option group', async () => {
- const optionGroup = await createOptionGroup('Quark-type', ['Charm', 'Strange']);
- const result = await adminClient.query(addOptionGroupToProductDocument, {
- optionGroupId: optionGroup.id,
- productId: newProduct.id,
- });
- expect(result.addOptionGroupToProduct.optionGroups.length).toBe(1);
- expect(result.addOptionGroupToProduct.optionGroups[0].id).toBe(optionGroup.id);
- // not really testing this, but just cleaning up for later tests
- const { removeOptionGroupFromProduct } = await adminClient.query(
- removeOptionGroupFromProductDocument,
- {
- optionGroupId: optionGroup.id,
- productId: newProduct.id,
- },
- );
- removeOptionGuard.assertSuccess(removeOptionGroupFromProduct);
- });
- it(
- 'addOptionGroupToProduct errors with an invalid productId',
- assertThrowsWithMessage(
- () =>
- adminClient.query(addOptionGroupToProductDocument, {
- optionGroupId: 'T_1',
- productId: 'T_999',
- }),
- 'No Product with the id "999" could be found',
- ),
- );
- it(
- 'addOptionGroupToProduct errors if the OptionGroup is already assigned to another Product',
- assertThrowsWithMessage(
- () =>
- adminClient.query(addOptionGroupToProductDocument, {
- optionGroupId: 'T_1',
- productId: 'T_2',
- }),
- 'The ProductOptionGroup "laptop-screen-size" is already assigned to the Product "Laptop"',
- ),
- );
- it(
- 'addOptionGroupToProduct errors with an invalid optionGroupId',
- assertThrowsWithMessage(
- () =>
- adminClient.query(addOptionGroupToProductDocument, {
- optionGroupId: '999',
- productId: newProduct.id,
- }),
- 'No ProductOptionGroup with the id "999" could be found',
- ),
- );
- it('removeOptionGroupFromProduct removes an option group', async () => {
- const optionGroup = await createOptionGroup('Length', ['Short', 'Long']);
- const { addOptionGroupToProduct } = await adminClient.query(addOptionGroupToProductDocument, {
- optionGroupId: optionGroup.id,
- productId: newProductWithAssets.id,
- });
- expect(addOptionGroupToProduct.optionGroups.length).toBe(1);
- const { removeOptionGroupFromProduct } = await adminClient.query(
- removeOptionGroupFromProductDocument,
- {
- optionGroupId: optionGroup.id,
- productId: newProductWithAssets.id,
- },
- );
- removeOptionGuard.assertSuccess(removeOptionGroupFromProduct);
- expect(removeOptionGroupFromProduct?.id).toBe(newProductWithAssets.id);
- expect(removeOptionGroupFromProduct?.optionGroups.length).toBe(0);
- });
- it('removeOptionGroupFromProduct return error result if the optionGroup is being used by variants', async () => {
- const { removeOptionGroupFromProduct } = await adminClient.query(
- removeOptionGroupFromProductDocument,
- {
- optionGroupId: 'T_3',
- productId: 'T_2',
- },
- );
- removeOptionGuard.assertErrorResult(removeOptionGroupFromProduct);
- expect(removeOptionGroupFromProduct.message).toBe(
- 'Cannot remove ProductOptionGroup "curvy-monitor-monitor-size" as it is used by 2 ProductVariants. Use the `force` argument to remove it anyway',
- );
- expect(removeOptionGroupFromProduct.errorCode).toBe(ErrorCode.PRODUCT_OPTION_IN_USE_ERROR);
- expect(removeOptionGroupFromProduct.optionGroupCode).toBe('curvy-monitor-monitor-size');
- expect(removeOptionGroupFromProduct.productVariantCount).toBe(2);
- });
- it('removeOptionGroupFromProduct succeeds if all related ProductVariants are also deleted', async () => {
- const { product } = await adminClient.query(getProductWithVariantsDocument, { id: 'T_2' });
- productGuard.assertSuccess(product);
- // Delete all variants for that product
- for (const variant of product.variants) {
- await adminClient.query(deleteProductVariantDocument, {
- id: variant.id,
- });
- }
- const { removeOptionGroupFromProduct } = await adminClient.query(
- removeOptionGroupFromProductDocument,
- {
- optionGroupId: product.optionGroups[0].id,
- productId: product.id,
- },
- );
- removeOptionGuard.assertSuccess(removeOptionGroupFromProduct);
- });
- it(
- 'removeOptionGroupFromProduct errors with an invalid productId',
- assertThrowsWithMessage(
- () =>
- adminClient.query(removeOptionGroupFromProductDocument, {
- optionGroupId: '1',
- productId: '999',
- }),
- 'No Product with the id "999" could be found',
- ),
- );
- it(
- 'removeOptionGroupFromProduct errors with an invalid optionGroupId',
- assertThrowsWithMessage(
- () =>
- adminClient.query(removeOptionGroupFromProductDocument, {
- optionGroupId: '999',
- productId: newProduct.id,
- }),
- 'No ProductOptionGroup with the id "999" could be found',
- ),
- );
- describe('variants', () => {
- let variants: ResultOf<typeof createProductVariantsDocument>['createProductVariants'];
- let optionGroup2: NonNullable<ResultOf<typeof getOptionGroupDocument>['productOptionGroup']>;
- let optionGroup3: NonNullable<ResultOf<typeof getOptionGroupDocument>['productOptionGroup']>;
- beforeAll(async () => {
- optionGroup2 = await createOptionGroup('group-2', ['group2-option-1', 'group2-option-2']);
- optionGroup3 = await createOptionGroup('group-3', ['group3-option-1', 'group3-option-2']);
- await adminClient.query(addOptionGroupToProductDocument, {
- optionGroupId: optionGroup2.id,
- productId: newProduct.id,
- });
- await adminClient.query(addOptionGroupToProductDocument, {
- optionGroupId: optionGroup3.id,
- productId: newProduct.id,
- });
- });
- it(
- 'createProductVariants throws if optionIds not compatible with product',
- assertThrowsWithMessage(async () => {
- await adminClient.query(createProductVariantsDocument, {
- input: [
- {
- productId: newProduct.id,
- sku: 'PV1',
- optionIds: [],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
- },
- ],
- });
- }, 'ProductVariant optionIds must include one optionId from each of the groups: group-2, group-3'),
- );
- it(
- 'createProductVariants throws if optionIds are duplicated',
- assertThrowsWithMessage(async () => {
- await adminClient.query(createProductVariantsDocument, {
- input: [
- {
- productId: newProduct.id,
- sku: 'PV1',
- optionIds: [optionGroup2.options[0].id, optionGroup2.options[1].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
- },
- ],
- });
- }, 'ProductVariant optionIds must include one optionId from each of the groups: group-2, group-3'),
- );
- it('createProductVariants works', async () => {
- const { createProductVariants } = await adminClient.query(createProductVariantsDocument, {
- input: [
- {
- productId: newProduct.id,
- sku: 'PV1',
- optionIds: [optionGroup2.options[0].id, optionGroup3.options[0].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
- },
- ],
- });
- const createdVariant = createProductVariants[0];
- variantGuard.assertSuccess(createdVariant);
- expect(createdVariant.name).toBe('Variant 1');
- expect(createdVariant.options.map(pick(['id']))).toContainEqual({
- id: optionGroup2.options[0].id,
- });
- expect(createdVariant.options.map(pick(['id']))).toContainEqual({
- id: optionGroup3.options[0].id,
- });
- });
- it('createProductVariants adds multiple variants at once', async () => {
- const { createProductVariants } = await adminClient.query(createProductVariantsDocument, {
- input: [
- {
- productId: newProduct.id,
- sku: 'PV2',
- optionIds: [optionGroup2.options[1].id, optionGroup3.options[0].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 2' }],
- },
- {
- productId: newProduct.id,
- sku: 'PV3',
- optionIds: [optionGroup2.options[1].id, optionGroup3.options[1].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 3' }],
- },
- ],
- });
- const variant2 = createProductVariants.find(v => v?.name === 'Variant 2');
- const variant3 = createProductVariants.find(v => v?.name === 'Variant 3');
- variantGuard.assertSuccess(variant2);
- variantGuard.assertSuccess(variant3);
- expect(variant2.options.map(pick(['id']))).toContainEqual({ id: optionGroup2.options[1].id });
- expect(variant2.options.map(pick(['id']))).toContainEqual({ id: optionGroup3.options[0].id });
- expect(variant3.options.map(pick(['id']))).toContainEqual({ id: optionGroup2.options[1].id });
- expect(variant3.options.map(pick(['id']))).toContainEqual({ id: optionGroup3.options[1].id });
- variants = createProductVariants.filter(notNullOrUndefined);
- });
- it(
- 'createProductVariants throws if options combination already exists',
- assertThrowsWithMessage(async () => {
- await adminClient.query(createProductVariantsDocument, {
- input: [
- {
- productId: newProduct.id,
- sku: 'PV2',
- optionIds: [optionGroup2.options[0].id, optionGroup3.options[0].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 2' }],
- },
- ],
- });
- }, 'A ProductVariant with the selected options already exists: Variant 1'),
- );
- it('updateProductVariants updates variants', async () => {
- const firstVariant = variants[0];
- variantGuard.assertSuccess(firstVariant);
- const { updateProductVariants } = await adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: firstVariant.id,
- translations: firstVariant.translations,
- sku: 'ABC',
- price: 432,
- },
- ],
- });
- const updatedVariant = updateProductVariants[0];
- updateVariantGuard.assertSuccess(updatedVariant);
- expect(updatedVariant.sku).toBe('ABC');
- expect(updatedVariant.price).toBe(432);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1101
- it('after update, the updatedAt should be modified', async () => {
- // Pause for a second to ensure the updatedAt date is more than 1s
- // later than the createdAt date, since sqlite does not seem to store
- // down to millisecond resolution.
- await new Promise(resolve => setTimeout(resolve, 1000));
- const firstVariant = variants[0];
- variantGuard.assertSuccess(firstVariant);
- const { updateProductVariants } = await adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: firstVariant.id,
- translations: firstVariant.translations,
- sku: 'ABCD',
- price: 432,
- },
- ],
- });
- const updatedVariant = updateProductVariants.find(v => v?.id === firstVariant.id);
- expect(updatedVariant?.updatedAt).not.toBe(updatedVariant?.createdAt);
- });
- it('updateProductVariants updates assets', async () => {
- const firstVariant = variants[0];
- variantGuard.assertSuccess(firstVariant);
- const result = await adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: firstVariant.id,
- assetIds: ['T_1', 'T_2'],
- featuredAssetId: 'T_2',
- },
- ],
- });
- const updatedVariant = result.updateProductVariants[0];
- updateVariantGuard.assertSuccess(updatedVariant);
- expect(updatedVariant.assets.map(a => a.id)).toEqual(['T_1', 'T_2']);
- expect(updatedVariant.featuredAsset).toBeDefined();
- expect(updatedVariant.featuredAsset?.id).toBe('T_2');
- });
- it('updateProductVariants updates assets again', async () => {
- const firstVariant = variants[0];
- variantGuard.assertSuccess(firstVariant);
- const result = await adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: firstVariant.id,
- assetIds: ['T_4', 'T_3'],
- featuredAssetId: 'T_4',
- },
- ],
- });
- const updatedVariant = result.updateProductVariants[0];
- updateVariantGuard.assertSuccess(updatedVariant);
- expect(updatedVariant.assets.map(a => a.id)).toEqual(['T_4', 'T_3']);
- expect(updatedVariant.featuredAsset).toBeDefined();
- expect(updatedVariant.featuredAsset?.id).toBe('T_4');
- });
- it('updateProductVariants updates taxCategory and price', async () => {
- const firstVariant = variants[0];
- variantGuard.assertSuccess(firstVariant);
- const result = await adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: firstVariant.id,
- price: 105,
- taxCategoryId: 'T_2',
- },
- ],
- });
- const updatedVariant = result.updateProductVariants[0];
- updateVariantGuard.assertSuccess(updatedVariant);
- expect(updatedVariant.price).toBe(105);
- expect(updatedVariant.taxCategory.id).toBe('T_2');
- });
- it('updateProductVariants updates facetValues', async () => {
- const firstVariant = variants[0];
- variantGuard.assertSuccess(firstVariant);
- const result = await adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: firstVariant.id,
- facetValueIds: ['T_1'],
- },
- ],
- });
- const updatedVariant = result.updateProductVariants[0];
- updateVariantGuard.assertSuccess(updatedVariant);
- expect(updatedVariant.facetValues.length).toBe(1);
- expect(updatedVariant.facetValues[0].id).toBe('T_1');
- });
- it('updateProductVariants throws with an invalid variant id', async () => {
- const firstVariant = variants[0];
- variantGuard.assertSuccess(firstVariant);
- await expect(
- adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: 'T_999',
- translations: firstVariant.translations,
- sku: 'ABC',
- price: 432,
- },
- ],
- }),
- ).rejects.toThrow('No ProductVariant with the id "999" could be found');
- });
- describe('adding options to existing variants', () => {
- let variantToModify: NonNullable<
- ResultOf<typeof createProductVariantsDocument>['createProductVariants'][number]
- >;
- let initialOptionIds: string[];
- let newOptionGroup: ResultOf<
- typeof createProductOptionGroupDocument
- >['createProductOptionGroup'];
- beforeAll(() => {
- const firstVariant = variants[0];
- expect(firstVariant).toBeDefined();
- variantToModify = firstVariant as typeof variantToModify;
- initialOptionIds = variantToModify.options.map(o => o.id);
- });
- it('assert initial state', async () => {
- expect(variantToModify.options.map(o => o.code)).toEqual([
- 'group2-option-2',
- 'group3-option-1',
- ]);
- });
- it(
- 'passing optionIds from an invalid OptionGroup throws',
- assertThrowsWithMessage(async () => {
- await adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: variantToModify.id,
- optionIds: [...variantToModify.options.map(o => o.id), 'T_1'],
- },
- ],
- });
- }, 'ProductVariant optionIds must include one optionId from each of the groups: group-2, group-3'),
- );
- it('passing optionIds that match an existing variant should not throw', async () => {
- const { updateProductVariants } = await adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: variantToModify.id,
- optionIds: variantToModify.options.map(o => o.id),
- sku: 'ABC',
- price: 432,
- },
- ],
- });
- const updatedVariant = updateProductVariants[0];
- updateVariantGuard.assertSuccess(updatedVariant);
- expect(updatedVariant.sku).toBe('ABC');
- expect(updatedVariant.price).toBe(432);
- });
- it('addOptionGroupToProduct and then update existing ProductVariant with a new option', async () => {
- const optionGroup4 = await createOptionGroup('group-4', [
- 'group4-option-1',
- 'group4-option-2',
- ]);
- newOptionGroup = optionGroup4;
- const result = await adminClient.query(addOptionGroupToProductDocument, {
- optionGroupId: optionGroup4.id,
- productId: newProduct.id,
- });
- expect(result.addOptionGroupToProduct.optionGroups.length).toBe(3);
- expect(result.addOptionGroupToProduct.optionGroups[2].id).toBe(optionGroup4.id);
- const { updateProductVariants } = await adminClient.query(updateProductVariantsDocument, {
- input: [
- {
- id: variantToModify.id,
- optionIds: [
- ...variantToModify.options.map(o => o.id),
- optionGroup4.options[0].id,
- ],
- },
- ],
- });
- const updatedVariant = updateProductVariants[0];
- updateVariantGuard.assertSuccess(updatedVariant);
- expect(updatedVariant.options.map(o => o.code)).toEqual([
- 'group2-option-2',
- 'group3-option-1',
- 'group4-option-1',
- ]);
- });
- it('removeOptionGroup fails because option is in use', async () => {
- const { removeOptionGroupFromProduct } = await adminClient.query(
- removeOptionGroupFromProductDocument,
- {
- optionGroupId: newOptionGroup.id,
- productId: newProduct.id,
- },
- );
- removeOptionGuard.assertErrorResult(removeOptionGroupFromProduct);
- expect(removeOptionGroupFromProduct.message).toBe(
- 'Cannot remove ProductOptionGroup "group-4" as it is used by 3 ProductVariants. Use the `force` argument to remove it anyway',
- );
- });
- it('removeOptionGroup with force argument', async () => {
- const { removeOptionGroupFromProduct } = await adminClient.query(
- removeOptionGroupFromProductDocument,
- {
- optionGroupId: newOptionGroup.id,
- productId: newProduct.id,
- force: true,
- },
- );
- removeOptionGuard.assertSuccess(removeOptionGroupFromProduct);
- expect(removeOptionGroupFromProduct?.optionGroups.length).toBe(2);
- const { product } = await adminClient.query(getProductWithVariantsDocument, {
- id: newProduct.id,
- });
- productGuard.assertSuccess(product);
- function assertNoOptionGroup(
- variant: ResultOf<typeof productVariantFragment>,
- optionGroupId: string,
- ) {
- expect(variant.options.map(o => o.groupId).every(id => id !== optionGroupId)).toBe(
- true,
- );
- }
- assertNoOptionGroup(product.variants[0], newOptionGroup.id);
- assertNoOptionGroup(product.variants[1], newOptionGroup.id);
- assertNoOptionGroup(product.variants[2], newOptionGroup.id);
- });
- });
- let deletedVariant: ResultOf<typeof productVariantFragment>;
- it('deleteProductVariant', async () => {
- const result1 = await adminClient.query(getProductWithVariantsDocument, {
- id: newProduct.id,
- });
- productGuard.assertSuccess(result1.product);
- const sortedVariantIds = result1.product.variants.map(v => v.id).sort();
- expect(sortedVariantIds).toEqual(['T_35', 'T_36', 'T_37']);
- const { deleteProductVariant } = await adminClient.query(deleteProductVariantDocument, {
- id: sortedVariantIds[0],
- });
- expect(deleteProductVariant.result).toBe(DeletionResult.DELETED);
- const result2 = await adminClient.query(getProductWithVariantsDocument, {
- id: newProduct.id,
- });
- productGuard.assertSuccess(result2.product);
- expect(result2.product.variants.map(v => v.id).sort()).toEqual(['T_36', 'T_37']);
- const foundVariant = result1.product.variants.find(v => v.id === 'T_35');
- variantGuard.assertSuccess(foundVariant);
- deletedVariant = foundVariant;
- });
- /** Testing https://github.com/vendure-ecommerce/vendure/issues/412 **/
- it('createProductVariants ignores deleted variants when checking for existing combinations', async () => {
- const { createProductVariants } = await adminClient.query(createProductVariantsDocument, {
- input: [
- {
- productId: newProduct.id,
- sku: 'RE1',
- optionIds: [deletedVariant.options[0].id, deletedVariant.options[1].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Re-created Variant' }],
- },
- ],
- });
- expect(createProductVariants.length).toBe(1);
- const createdVariant = createProductVariants[0];
- variantGuard.assertSuccess(createdVariant);
- expect(createdVariant.options.map(o => o.code).sort()).toEqual(
- deletedVariant.options.map(o => o.code).sort(),
- );
- });
- // https://github.com/vendure-ecommerce/vendure/issues/980
- it('creating variants in a non-default language', async () => {
- const { createProduct } = await adminClient.query(createProductDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.de,
- name: 'Ananas',
- slug: 'ananas',
- description: 'Yummy Ananas',
- },
- ],
- },
- });
- const { createProductVariants } = await adminClient.query(createProductVariantsDocument, {
- input: [
- {
- productId: createProduct.id,
- sku: 'AN1110111',
- optionIds: [],
- translations: [{ languageCode: LanguageCode.de, name: 'Ananas Klein' }],
- },
- ],
- });
- expect(createProductVariants.length).toBe(1);
- expect(createProductVariants[0]?.name).toBe('Ananas Klein');
- const { product } = await adminClient.query(
- getProductWithVariantsDocument,
- {
- id: createProduct.id,
- },
- { languageCode: LanguageCode.en },
- );
- expect(product?.variants.length).toBe(1);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1631
- describe('changing the Channel default language', () => {
- let productId: string;
- function getProductWithVariantsInLanguage(
- id: string,
- languageCode: LanguageCode,
- variantListOptions?: VariablesOf<
- typeof getProductWithVariantListDocument
- >['variantListOptions'],
- ) {
- return adminClient.query(
- getProductWithVariantListDocument,
- { id, variantListOptions },
- { languageCode },
- );
- }
- beforeAll(async () => {
- await adminClient.query(updateGlobalSettingsDocument, {
- input: {
- availableLanguages: [LanguageCode.en, LanguageCode.de],
- },
- });
- const { createProduct } = await adminClient.query(createProductDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Bottle',
- slug: 'bottle',
- description: 'A container for liquids',
- },
- ],
- },
- });
- productId = createProduct.id;
- await adminClient.query(createProductVariantsDocument, {
- input: [
- {
- productId,
- sku: 'BOTTLE111',
- optionIds: [],
- translations: [{ languageCode: LanguageCode.en, name: 'Bottle' }],
- },
- ],
- });
- });
- afterAll(async () => {
- // Restore the default language to English for the subsequent tests
- await adminClient.query(updateChannelDocument, {
- input: {
- id: 'T_1',
- defaultLanguageCode: LanguageCode.en,
- },
- });
- });
- it('returns all variants', async () => {
- const { product: product1 } = await adminClient.query(
- getProductWithVariantsDocument,
- {
- id: productId,
- },
- { languageCode: LanguageCode.en },
- );
- expect(product1?.variants.length).toBe(1);
- // Change the default language of the channel to "de"
- const { updateChannel } = await adminClient.query(updateChannelDocument, {
- input: {
- id: 'T_1',
- defaultLanguageCode: LanguageCode.de,
- },
- });
- updateChannelGuard.assertSuccess(updateChannel);
- expect((updateChannel as any).defaultLanguageCode).toBe(LanguageCode.de);
- // Fetch the product in en, it should still return 1 variant
- const { product: product2 } = await getProductWithVariantsInLanguage(
- productId,
- LanguageCode.en,
- );
- expect(product2?.variantList.items.length).toBe(1);
- // Fetch the product in de, it should still return 1 variant
- const { product: product3 } = await getProductWithVariantsInLanguage(
- productId,
- LanguageCode.de,
- );
- expect(product3?.variantList.items.length).toBe(1);
- });
- it('returns all variants when sorting on variant name', async () => {
- // Fetch the product in en, it should still return 1 variant
- const { product: product1 } = await getProductWithVariantsInLanguage(
- productId,
- LanguageCode.en,
- { sort: { name: SortOrder.ASC } },
- );
- expect(product1?.variantList.items.length).toBe(1);
- // Fetch the product in de, it should still return 1 variant
- const { product: product2 } = await getProductWithVariantsInLanguage(
- productId,
- LanguageCode.de,
- { sort: { name: SortOrder.ASC } },
- );
- expect(product2?.variantList.items.length).toBe(1);
- });
- });
- });
- });
- describe('deletion', () => {
- let allProducts: ResultOf<typeof getProductListDocument>['products']['items'];
- let productToDelete: NonNullable<ResultOf<typeof getProductWithVariantsDocument>['product']>;
- beforeAll(async () => {
- const result = await adminClient.query(getProductListDocument, {
- options: {
- sort: {
- id: SortOrder.ASC,
- },
- },
- });
- allProducts = result.products.items;
- });
- it('deletes a product', async () => {
- const { product } = await adminClient.query(getProductWithVariantsDocument, {
- id: allProducts[0].id,
- });
- productGuard.assertSuccess(product);
- const result = await adminClient.query(deleteProductDocument, { id: product.id });
- expect(result.deleteProduct).toEqual({ result: DeletionResult.DELETED });
- productToDelete = product;
- });
- it('cannot get a deleted product', async () => {
- const { product } = await adminClient.query(getProductWithVariantsDocument, {
- id: productToDelete.id,
- });
- expect(product).toBe(null);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1096
- it('variants of deleted product are also deleted', async () => {
- for (const variant of productToDelete.variants) {
- const { productVariant } = await adminClient.query(getProductVariantDocument, {
- id: variant.id,
- });
- expect(productVariant).toBe(null);
- }
- });
- it('deleted product omitted from list', async () => {
- const result = await adminClient.query(getProductListDocument);
- expect(result.products.items.length).toBe(allProducts.length - 1);
- expect(result.products.items.map(c => c.id).includes(productToDelete.id)).toBe(false);
- });
- it(
- 'updateProduct throws for deleted product',
- assertThrowsWithMessage(
- () =>
- adminClient.query(updateProductDocument, {
- input: {
- id: productToDelete.id,
- facetValueIds: ['T_1'],
- },
- }),
- 'No Product with the id "1" could be found',
- ),
- );
- it(
- 'addOptionGroupToProduct throws for deleted product',
- assertThrowsWithMessage(
- () =>
- adminClient.query(addOptionGroupToProductDocument, {
- optionGroupId: 'T_1',
- productId: productToDelete.id,
- }),
- 'No Product with the id "1" could be found',
- ),
- );
- it(
- 'removeOptionGroupToProduct throws for deleted product',
- assertThrowsWithMessage(
- () =>
- adminClient.query(removeOptionGroupFromProductDocument, {
- optionGroupId: 'T_1',
- productId: productToDelete.id,
- }),
- 'No Product with the id "1" could be found',
- ),
- );
- // https://github.com/vendure-ecommerce/vendure/issues/558
- it('slug of a deleted product can be re-used', async () => {
- const result = await adminClient.query(createProductDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Product reusing deleted slug',
- slug: productToDelete.slug,
- description: 'stuff',
- },
- ],
- },
- });
- expect(result.createProduct.slug).toBe(productToDelete.slug);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1505
- it('attempting to re-use deleted slug twice is not allowed', async () => {
- const result = await adminClient.query(createProductDocument, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Product reusing deleted slug',
- slug: productToDelete.slug,
- description: 'stuff',
- },
- ],
- },
- });
- expect(result.createProduct.slug).not.toBe(productToDelete.slug);
- expect(result.createProduct.slug).toBe('laptop-2');
- });
- // https://github.com/vendure-ecommerce/vendure/issues/800
- it('product can be fetched by slug of a deleted product', async () => {
- const { product } = await adminClient.query(getProductSimpleDocument, {
- slug: productToDelete.slug,
- });
- productQueryGuard.assertSuccess(product);
- expect(product.slug).toBe(productToDelete.slug);
- });
- });
- async function createOptionGroup(name: string, options: string[]) {
- const { createProductOptionGroup } = await adminClient.query(createProductOptionGroupDocument, {
- input: {
- code: name.toLowerCase(),
- translations: [{ languageCode: LanguageCode.en, name }],
- options: options.map(option => ({
- code: option.toLowerCase(),
- translations: [{ languageCode: LanguageCode.en, name: option }],
- })),
- },
- });
- return createProductOptionGroup;
- }
- });
|