| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350 |
- 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 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 { PRODUCT_WITH_OPTIONS_FRAGMENT } from './graphql/fragments';
- import {
- AddOptionGroupToProduct,
- CreateProduct,
- CreateProductVariants,
- DeleteProduct,
- DeleteProductVariant,
- DeletionResult,
- ErrorCode,
- GetAssetList,
- GetOptionGroup,
- GetProductList,
- GetProductSimple,
- GetProductVariant,
- GetProductVariantList,
- GetProductWithVariants,
- LanguageCode,
- ProductVariantFragment,
- ProductWithOptionsFragment,
- ProductWithVariants,
- RemoveOptionGroupFromProduct,
- SortOrder,
- UpdateProduct,
- UpdateProductVariants,
- } from './graphql/generated-e2e-admin-types';
- import {
- ADD_OPTION_GROUP_TO_PRODUCT,
- CREATE_PRODUCT,
- CREATE_PRODUCT_VARIANTS,
- DELETE_PRODUCT,
- DELETE_PRODUCT_VARIANT,
- GET_ASSET_LIST,
- GET_PRODUCT_LIST,
- GET_PRODUCT_SIMPLE,
- GET_PRODUCT_WITH_VARIANTS,
- UPDATE_PRODUCT,
- UPDATE_PRODUCT_VARIANTS,
- } from './graphql/shared-definitions';
- import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
- // tslint:disable:no-non-null-assertion
- describe('Product resolver', () => {
- const { server, adminClient, shopClient } = createTestEnvironment(testConfig);
- const removeOptionGuard: ErrorResultGuard<ProductWithOptionsFragment> = createErrorResultGuard(
- input => !!input.optionGroups,
- );
- 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<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {},
- );
- 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<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- 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<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- 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<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- 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<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- 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<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- options: {
- filter: {
- name: {
- contains: 'skateboard',
- },
- },
- },
- },
- );
- expect(result.products.items.length).toBe(1);
- expect(result.products.items[0].name).toBe('Cruiser Skateboard');
- });
- it('sorts by name shop', async () => {
- const result = await shopClient.query<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- 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<GetProductSimple.Query, GetProductSimple.Variables>(
- GET_PRODUCT_SIMPLE,
- { id: 'T_2' },
- );
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.id).toBe('T_2');
- });
- it('by slug', async () => {
- const { product } = await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
- GET_PRODUCT_SIMPLE,
- { slug: 'curvy-monitor' },
- );
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.slug).toBe('curvy-monitor');
- });
- // https://github.com/vendure-ecommerce/vendure/issues/538
- it('falls back to default language slug', async () => {
- const { product } = await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
- GET_PRODUCT_SIMPLE,
- { slug: 'curvy-monitor' },
- { languageCode: LanguageCode.de },
- );
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.slug).toBe('curvy-monitor');
- });
- it(
- 'throws if neither id nor slug provided',
- assertThrowsWithMessage(async () => {
- await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
- GET_PRODUCT_SIMPLE,
- {},
- );
- }, '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<GetProductSimple.Query, GetProductSimple.Variables>(
- GET_PRODUCT_SIMPLE,
- {
- id: 'T_2',
- slug: 'laptop',
- },
- );
- }, 'The provided id and slug refer to different Products'),
- );
- it('returns expected properties', async () => {
- const { product } = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: 'T_2',
- });
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(omit(product, ['variants'])).toMatchSnapshot();
- expect(product.variants.length).toBe(2);
- });
- it('ProductVariant price properties are correct', async () => {
- const result = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: 'T_2',
- });
- if (!result.product) {
- fail('Product not found');
- return;
- }
- 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<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: 'bad_id',
- });
- expect(result.product).toBeNull();
- });
- });
- describe('productVariants list query', () => {
- it('returns list', async () => {
- const { productVariants } = await adminClient.query<
- GetProductVariantList.Query,
- GetProductVariantList.Variables
- >(GET_PRODUCT_VARIANT_LIST, {
- options: {
- take: 3,
- sort: {
- name: SortOrder.ASC,
- },
- },
- });
- expect(productVariants.items).toEqual([
- {
- id: 'T_34',
- name: 'Bonsai Tree',
- price: 1999,
- sku: 'B01MXFLUSV',
- },
- {
- id: 'T_24',
- name: 'Boxing Gloves',
- price: 3304,
- sku: 'B000ZYLPPU',
- },
- {
- id: 'T_19',
- name: 'Camera Lens',
- price: 10400,
- sku: 'B0012UUP02',
- },
- ]);
- });
- it('sort by price', async () => {
- const { productVariants } = await adminClient.query<
- GetProductVariantList.Query,
- GetProductVariantList.Variables
- >(GET_PRODUCT_VARIANT_LIST, {
- options: {
- take: 3,
- sort: {
- price: SortOrder.ASC,
- },
- },
- });
- expect(productVariants.items).toEqual([
- {
- id: 'T_23',
- name: 'Skipping Rope',
- price: 799,
- sku: 'B07CNGXVXT',
- },
- {
- id: 'T_20',
- name: 'Tripod',
- price: 1498,
- sku: 'B00XI87KV8',
- },
- {
- id: 'T_32',
- name: 'Spiky Cactus',
- price: 1550,
- sku: 'SC011001',
- },
- ]);
- });
- });
- describe('productVariant query', () => {
- it('by id', async () => {
- const { productVariant } = await adminClient.query<
- GetProductVariant.Query,
- GetProductVariant.Variables
- >(GET_PRODUCT_VARIANT, {
- 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<
- GetProductVariant.Query,
- GetProductVariant.Variables
- >(GET_PRODUCT_VARIANT, {
- id: 'T_999',
- });
- expect(productVariant).toBeNull();
- });
- });
- describe('product mutation', () => {
- let newProduct: ProductWithVariants.Fragment;
- let newProductWithAssets: ProductWithVariants.Fragment;
- it('createProduct creates a new Product', async () => {
- const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
- CREATE_PRODUCT,
- {
- 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',
- ]);
- newProduct = result.createProduct;
- });
- it('createProduct creates a new Product with assets', async () => {
- const assetsResult = await adminClient.query<GetAssetList.Query, GetAssetList.Variables>(
- GET_ASSET_LIST,
- );
- const assetIds = assetsResult.assets.items.slice(0, 2).map(a => a.id);
- const featuredAssetId = assetsResult.assets.items[0].id;
- const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
- CREATE_PRODUCT,
- {
- 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!.id).toBe(featuredAssetId);
- newProductWithAssets = result.createProduct;
- });
- it('createProduct creates a disabled Product', async () => {
- const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
- CREATE_PRODUCT,
- {
- 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<UpdateProduct.Mutation, UpdateProduct.Variables>(
- UPDATE_PRODUCT,
- {
- 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<UpdateProduct.Mutation, UpdateProduct.Variables>(
- UPDATE_PRODUCT,
- {
- 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<CreateProduct.Mutation, CreateProduct.Variables>(
- CREATE_PRODUCT,
- {
- 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<UpdateProduct.Mutation, UpdateProduct.Variables>(
- UPDATE_PRODUCT,
- {
- 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<UpdateProduct.Mutation, UpdateProduct.Variables>(
- UPDATE_PRODUCT,
- {
- 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<UpdateProduct.Mutation, UpdateProduct.Variables>(
- UPDATE_PRODUCT,
- {
- input: {
- id: newProduct.id,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'en Very Mashed Potato',
- },
- ],
- },
- },
- );
- expect(result.updateProduct.translations.length).toBe(2);
- expect(
- result.updateProduct.translations.find(t => t.languageCode === LanguageCode.de)!.name,
- ).toBe('de Mashed Potato');
- expect(
- result.updateProduct.translations.find(t => t.languageCode === LanguageCode.en)!.name,
- ).toBe('en Very Mashed Potato');
- expect(
- result.updateProduct.translations.find(t => t.languageCode === LanguageCode.en)!.description,
- ).toBe('Possibly the final baked potato');
- });
- it('updateProduct adds Assets to a product and sets featured asset', async () => {
- const assetsResult = await adminClient.query<GetAssetList.Query, GetAssetList.Variables>(
- GET_ASSET_LIST,
- );
- const assetIds = assetsResult.assets.items.map(a => a.id);
- const featuredAssetId = assetsResult.assets.items[2].id;
- const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
- UPDATE_PRODUCT,
- {
- input: {
- id: newProduct.id,
- assetIds,
- featuredAssetId,
- },
- },
- );
- expect(result.updateProduct.assets.map(a => a.id)).toEqual(assetIds);
- expect(result.updateProduct.featuredAsset!.id).toBe(featuredAssetId);
- });
- it('updateProduct sets a featured asset', async () => {
- const productResult = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: newProduct.id,
- });
- const assets = productResult.product!.assets;
- const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
- UPDATE_PRODUCT,
- {
- input: {
- id: newProduct.id,
- featuredAssetId: assets[0].id,
- },
- },
- );
- expect(result.updateProduct.featuredAsset!.id).toBe(assets[0].id);
- });
- it('updateProduct updates assets', async () => {
- const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
- UPDATE_PRODUCT,
- {
- 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<UpdateProduct.Mutation, UpdateProduct.Variables>(
- UPDATE_PRODUCT,
- {
- input: {
- id: newProduct.id,
- facetValueIds: ['T_1'],
- },
- },
- );
- expect(result.updateProduct.facetValues.length).toEqual(1);
- });
- it(
- 'updateProduct errors with an invalid productId',
- assertThrowsWithMessage(
- () =>
- adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
- 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 result = await adminClient.query<
- AddOptionGroupToProduct.Mutation,
- AddOptionGroupToProduct.Variables
- >(ADD_OPTION_GROUP_TO_PRODUCT, {
- optionGroupId: 'T_2',
- productId: newProduct.id,
- });
- expect(result.addOptionGroupToProduct.optionGroups.length).toBe(1);
- expect(result.addOptionGroupToProduct.optionGroups[0].id).toBe('T_2');
- });
- it(
- 'addOptionGroupToProduct errors with an invalid productId',
- assertThrowsWithMessage(
- () =>
- adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
- ADD_OPTION_GROUP_TO_PRODUCT,
- {
- optionGroupId: 'T_1',
- productId: '999',
- },
- ),
- `No Product with the id '999' could be found`,
- ),
- );
- it(
- 'addOptionGroupToProduct errors with an invalid optionGroupId',
- assertThrowsWithMessage(
- () =>
- adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
- ADD_OPTION_GROUP_TO_PRODUCT,
- {
- optionGroupId: '999',
- productId: newProduct.id,
- },
- ),
- `No ProductOptionGroup with the id '999' could be found`,
- ),
- );
- it('removeOptionGroupFromProduct removes an option group', async () => {
- const { addOptionGroupToProduct } = await adminClient.query<
- AddOptionGroupToProduct.Mutation,
- AddOptionGroupToProduct.Variables
- >(ADD_OPTION_GROUP_TO_PRODUCT, {
- optionGroupId: 'T_1',
- productId: newProductWithAssets.id,
- });
- expect(addOptionGroupToProduct.optionGroups.length).toBe(1);
- const { removeOptionGroupFromProduct } = await adminClient.query<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- optionGroupId: 'T_1',
- 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<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- 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`,
- );
- 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 errors with an invalid productId',
- assertThrowsWithMessage(
- () =>
- adminClient.query<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- optionGroupId: '1',
- productId: '999',
- }),
- `No Product with the id '999' could be found`,
- ),
- );
- it(
- 'removeOptionGroupFromProduct errors with an invalid optionGroupId',
- assertThrowsWithMessage(
- () =>
- adminClient.query<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- optionGroupId: '999',
- productId: newProduct.id,
- }),
- `No ProductOptionGroup with the id '999' could be found`,
- ),
- );
- describe('variants', () => {
- let variants: CreateProductVariants.CreateProductVariants[];
- let optionGroup2: GetOptionGroup.ProductOptionGroup;
- let optionGroup3: GetOptionGroup.ProductOptionGroup;
- beforeAll(async () => {
- await adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
- ADD_OPTION_GROUP_TO_PRODUCT,
- {
- optionGroupId: 'T_3',
- productId: newProduct.id,
- },
- );
- const result1 = await adminClient.query<GetOptionGroup.Query, GetOptionGroup.Variables>(
- GET_OPTION_GROUP,
- { id: 'T_2' },
- );
- const result2 = await adminClient.query<GetOptionGroup.Query, GetOptionGroup.Variables>(
- GET_OPTION_GROUP,
- { id: 'T_3' },
- );
- optionGroup2 = result1.productOptionGroup!;
- optionGroup3 = result2.productOptionGroup!;
- });
- it(
- 'createProductVariants throws if optionIds not compatible with product',
- assertThrowsWithMessage(async () => {
- await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
- CREATE_PRODUCT_VARIANTS,
- {
- 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: curvy-monitor-monitor-size, laptop-ram'),
- );
- it(
- 'createProductVariants throws if optionIds are duplicated',
- assertThrowsWithMessage(async () => {
- await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
- CREATE_PRODUCT_VARIANTS,
- {
- 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: curvy-monitor-monitor-size, laptop-ram'),
- );
- it('createProductVariants works', async () => {
- const { createProductVariants } = await adminClient.query<
- CreateProductVariants.Mutation,
- CreateProductVariants.Variables
- >(CREATE_PRODUCT_VARIANTS, {
- input: [
- {
- productId: newProduct.id,
- sku: 'PV1',
- optionIds: [optionGroup2.options[0].id, optionGroup3.options[0].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
- },
- ],
- });
- expect(createProductVariants[0]!.name).toBe('Variant 1');
- expect(createProductVariants[0]!.options.map(pick(['id']))).toContainEqual({
- id: optionGroup2.options[0].id,
- });
- expect(createProductVariants[0]!.options.map(pick(['id']))).toContainEqual({
- id: optionGroup3.options[0].id,
- });
- });
- it('createProductVariants adds multiple variants at once', async () => {
- const { createProductVariants } = await adminClient.query<
- CreateProductVariants.Mutation,
- CreateProductVariants.Variables
- >(CREATE_PRODUCT_VARIANTS, {
- 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')!;
- 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<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
- CREATE_PRODUCT_VARIANTS,
- {
- input: [
- {
- productId: newProduct.id,
- sku: 'PV2',
- optionIds: [optionGroup2.options[0].id, optionGroup3.options[0].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 2' }],
- },
- ],
- },
- );
- }, 'A ProductVariant already exists with the options:'),
- );
- it('updateProductVariants updates variants', async () => {
- const firstVariant = variants[0];
- const { updateProductVariants } = await adminClient.query<
- UpdateProductVariants.Mutation,
- UpdateProductVariants.Variables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: firstVariant.id,
- translations: firstVariant.translations,
- sku: 'ABC',
- price: 432,
- },
- ],
- });
- const updatedVariant = updateProductVariants[0];
- if (!updatedVariant) {
- fail('no updated variant returned.');
- return;
- }
- expect(updatedVariant.sku).toBe('ABC');
- expect(updatedVariant.price).toBe(432);
- });
- it('updateProductVariants updates assets', async () => {
- const firstVariant = variants[0];
- const result = await adminClient.query<
- UpdateProductVariants.Mutation,
- UpdateProductVariants.Variables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: firstVariant.id,
- assetIds: ['T_1', 'T_2'],
- featuredAssetId: 'T_2',
- },
- ],
- });
- const updatedVariant = result.updateProductVariants[0];
- if (!updatedVariant) {
- fail('no updated variant returned.');
- return;
- }
- expect(updatedVariant.assets.map(a => a.id)).toEqual(['T_1', 'T_2']);
- expect(updatedVariant.featuredAsset!.id).toBe('T_2');
- });
- it('updateProductVariants updates assets again', async () => {
- const firstVariant = variants[0];
- const result = await adminClient.query<
- UpdateProductVariants.Mutation,
- UpdateProductVariants.Variables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: firstVariant.id,
- assetIds: ['T_4', 'T_3'],
- featuredAssetId: 'T_4',
- },
- ],
- });
- const updatedVariant = result.updateProductVariants[0];
- if (!updatedVariant) {
- fail('no updated variant returned.');
- return;
- }
- expect(updatedVariant.assets.map(a => a.id)).toEqual(['T_4', 'T_3']);
- expect(updatedVariant.featuredAsset!.id).toBe('T_4');
- });
- it('updateProductVariants updates taxCategory and price', async () => {
- const firstVariant = variants[0];
- const result = await adminClient.query<
- UpdateProductVariants.Mutation,
- UpdateProductVariants.Variables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: firstVariant.id,
- price: 105,
- taxCategoryId: 'T_2',
- },
- ],
- });
- const updatedVariant = result.updateProductVariants[0];
- if (!updatedVariant) {
- fail('no updated variant returned.');
- return;
- }
- expect(updatedVariant.price).toBe(105);
- expect(updatedVariant.taxCategory.id).toBe('T_2');
- });
- it('updateProductVariants updates facetValues', async () => {
- const firstVariant = variants[0];
- const result = await adminClient.query<
- UpdateProductVariants.Mutation,
- UpdateProductVariants.Variables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: firstVariant.id,
- facetValueIds: ['T_1'],
- },
- ],
- });
- const updatedVariant = result.updateProductVariants[0];
- if (!updatedVariant) {
- fail('no updated variant returned.');
- return;
- }
- expect(updatedVariant.facetValues.length).toBe(1);
- expect(updatedVariant.facetValues[0].id).toBe('T_1');
- });
- it(
- 'updateProductVariants throws with an invalid variant id',
- assertThrowsWithMessage(
- () =>
- adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
- UPDATE_PRODUCT_VARIANTS,
- {
- input: [
- {
- id: 'T_999',
- translations: variants[0].translations,
- sku: 'ABC',
- price: 432,
- },
- ],
- },
- ),
- `No ProductVariant with the id '999' could be found`,
- ),
- );
- let deletedVariant: ProductVariantFragment;
- it('deleteProductVariant', async () => {
- const result1 = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: newProduct.id,
- });
- const sortedVariantIds = result1.product!.variants.map(v => v.id).sort();
- expect(sortedVariantIds).toEqual(['T_35', 'T_36', 'T_37']);
- const { deleteProductVariant } = await adminClient.query<
- DeleteProductVariant.Mutation,
- DeleteProductVariant.Variables
- >(DELETE_PRODUCT_VARIANT, {
- id: sortedVariantIds[0],
- });
- expect(deleteProductVariant.result).toBe(DeletionResult.DELETED);
- const result2 = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: newProduct.id,
- });
- expect(result2.product!.variants.map(v => v.id).sort()).toEqual(['T_36', 'T_37']);
- deletedVariant = result1.product?.variants.find(v => v.id === 'T_35')!;
- });
- /** 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<
- CreateProductVariants.Mutation,
- CreateProductVariants.Variables
- >(CREATE_PRODUCT_VARIANTS, {
- 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);
- expect(createProductVariants[0]!.options.map(o => o.code).sort()).toEqual(
- deletedVariant.options.map(o => o.code).sort(),
- );
- });
- });
- });
- describe('deletion', () => {
- let allProducts: GetProductList.Items[];
- let productToDelete: GetProductList.Items;
- beforeAll(async () => {
- const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- options: {
- sort: {
- id: SortOrder.ASC,
- },
- },
- },
- );
- allProducts = result.products.items;
- });
- it('deletes a product', async () => {
- productToDelete = allProducts[0];
- const result = await adminClient.query<DeleteProduct.Mutation, DeleteProduct.Variables>(
- DELETE_PRODUCT,
- { id: productToDelete.id },
- );
- expect(result.deleteProduct).toEqual({ result: DeletionResult.DELETED });
- });
- it('cannot get a deleted product', async () => {
- const result = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: productToDelete.id,
- });
- expect(result.product).toBe(null);
- });
- it('deleted product omitted from list', async () => {
- const result = await adminClient.query<GetProductList.Query>(GET_PRODUCT_LIST);
- 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<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
- 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<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
- ADD_OPTION_GROUP_TO_PRODUCT,
- {
- optionGroupId: 'T_1',
- productId: productToDelete.id,
- },
- ),
- `No Product with the id '1' could be found`,
- ),
- );
- it(
- 'removeOptionGroupToProduct throws for deleted product',
- assertThrowsWithMessage(
- () =>
- adminClient.query<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- 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<CreateProduct.Mutation, CreateProduct.Variables>(
- CREATE_PRODUCT,
- {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Product reusing deleted slug',
- slug: productToDelete.slug,
- description: 'stuff',
- },
- ],
- },
- },
- );
- expect(result.createProduct.slug).toBe(productToDelete.slug);
- });
- });
- });
- export const REMOVE_OPTION_GROUP_FROM_PRODUCT = gql`
- mutation RemoveOptionGroupFromProduct($productId: ID!, $optionGroupId: ID!) {
- removeOptionGroupFromProduct(productId: $productId, optionGroupId: $optionGroupId) {
- ...ProductWithOptions
- ... on ProductOptionInUseError {
- errorCode
- message
- optionGroupCode
- productVariantCount
- }
- }
- }
- ${PRODUCT_WITH_OPTIONS_FRAGMENT}
- `;
- export const GET_OPTION_GROUP = gql`
- query GetOptionGroup($id: ID!) {
- productOptionGroup(id: $id) {
- id
- code
- options {
- id
- code
- }
- }
- }
- `;
- export const GET_PRODUCT_VARIANT = gql`
- query GetProductVariant($id: ID!) {
- productVariant(id: $id) {
- id
- name
- }
- }
- `;
- export const GET_PRODUCT_VARIANT_LIST = gql`
- query GetProductVariantLIST($options: ProductVariantListOptions) {
- productVariants(options: $options) {
- items {
- id
- name
- sku
- price
- }
- totalItems
- }
- }
- `;
|