| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129 |
- import { omit } from '@vendure/common/lib/omit';
- import { pick } from '@vendure/common/lib/pick';
- import { notNullOrUndefined } from '@vendure/common/lib/shared-utils';
- import { createTestEnvironment } from '@vendure/testing';
- import gql from 'graphql-tag';
- import path from 'path';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
- import {
- AddOptionGroupToProduct,
- CreateProduct,
- CreateProductVariants,
- DeleteProduct,
- DeleteProductVariant,
- DeletionResult,
- GetAssetList,
- GetOptionGroup,
- GetProductList,
- GetProductSimple,
- GetProductWithVariants,
- LanguageCode,
- ProductWithVariants,
- RemoveOptionGroupFromProduct,
- SortOrder,
- UpdateProduct,
- UpdateProductVariants,
- } from './graphql/generated-e2e-admin-types';
- import {
- 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);
- beforeAll(async () => {
- await server.init({
- dataDir: path.join(__dirname, '__data__'),
- 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',
- 'SLR Camera',
- 'Skipping Rope',
- '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',
- 'SLR Camera',
- 'Skipping Rope',
- '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');
- });
- 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('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(result.createProduct).toMatchSnapshot();
- 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('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).toMatchSnapshot();
- });
- 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[0].name).toBe('de Mashed Potato');
- expect(result.updateProduct.translations[1].name).toBe('en Very Mashed Potato');
- expect(result.updateProduct.translations[1].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 result = await adminClient.query<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- optionGroupId: 'T_1',
- productId: newProductWithAssets.id,
- });
- expect(result.removeOptionGroupFromProduct.id).toBe(newProductWithAssets.id);
- expect(result.removeOptionGroupFromProduct.optionGroups.length).toBe(0);
- });
- it(
- 'removeOptionGroupFromProduct errors if the optionGroup is being used by variants',
- assertThrowsWithMessage(
- () =>
- adminClient.query<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- optionGroupId: 'T_3',
- productId: 'T_2',
- }),
- `Cannot remove ProductOptionGroup "curvy-monitor-monitor-size" as it is used by 2 ProductVariants`,
- ),
- );
- 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']))).toEqual([
- { id: optionGroup2.options[0].id },
- { 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' }],
- },
- ],
- });
- expect(createProductVariants[0]!.name).toBe('Variant 2');
- expect(createProductVariants[1]!.name).toBe('Variant 3');
- expect(createProductVariants[0]!.options.map(pick(['id']))).toEqual([
- { id: optionGroup2.options[1].id },
- { id: optionGroup3.options[0].id },
- ]);
- expect(createProductVariants[1]!.options.map(pick(['id']))).toEqual([
- { id: optionGroup2.options[1].id },
- { 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: 16gb, 24-inch'),
- );
- 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 priceBeforeTax', 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`,
- ),
- );
- it('deleteProductVariant', async () => {
- const result1 = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: newProduct.id,
- });
- expect(result1.product!.variants.map(v => v.id)).toEqual(['T_35', 'T_36', 'T_37']);
- const { deleteProductVariant } = await adminClient.query<
- DeleteProductVariant.Mutation,
- DeleteProductVariant.Variables
- >(DELETE_PRODUCT_VARIANT, {
- id: result1.product!.variants[0].id,
- });
- 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)).toEqual(['T_36', 'T_37']);
- });
- });
- });
- describe('deletion', () => {
- let allProducts: GetProductList.Items[];
- let productToDelete: GetProductList.Items;
- beforeAll(async () => {
- const result = await adminClient.query<GetProductList.Query>(GET_PRODUCT_LIST);
- 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`,
- ),
- );
- });
- });
- export const ADD_OPTION_GROUP_TO_PRODUCT = gql`
- mutation AddOptionGroupToProduct($productId: ID!, $optionGroupId: ID!) {
- addOptionGroupToProduct(productId: $productId, optionGroupId: $optionGroupId) {
- id
- optionGroups {
- id
- code
- options {
- id
- code
- }
- }
- }
- }
- `;
- export const REMOVE_OPTION_GROUP_FROM_PRODUCT = gql`
- mutation RemoveOptionGroupFromProduct($productId: ID!, $optionGroupId: ID!) {
- removeOptionGroupFromProduct(productId: $productId, optionGroupId: $optionGroupId) {
- id
- optionGroups {
- id
- code
- options {
- id
- code
- }
- }
- }
- }
- `;
- export const GET_OPTION_GROUP = gql`
- query GetOptionGroup($id: ID!) {
- productOptionGroup(id: $id) {
- id
- code
- options {
- id
- code
- }
- }
- }
- `;
|