| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161 |
- 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_VARIANT_FRAGMENT, PRODUCT_WITH_OPTIONS_FRAGMENT } from './graphql/fragments';
- import {
- AddOptionGroupToProduct,
- ChannelFragment,
- CreateProduct,
- CreateProductOptionGroupMutation,
- CreateProductOptionGroupMutationVariables,
- CreateProductVariants,
- DeleteProduct,
- DeleteProductVariant,
- DeletionResult,
- ErrorCode,
- GetAssetList,
- GetOptionGroup,
- GetProductList,
- GetProductSimple,
- GetProductVariant,
- GetProductVariantList,
- GetProductWithVariantList,
- GetProductWithVariants,
- GetProductWithVariantsQuery,
- GetProductWithVariantsQueryVariables,
- LanguageCode,
- ProductVariantFragment,
- ProductVariantListOptions,
- ProductWithOptionsFragment,
- ProductWithVariants,
- RemoveOptionGroupFromProduct,
- SortOrder,
- UpdateChannel,
- UpdateGlobalSettings,
- UpdateProduct,
- UpdateProductVariants,
- } from './graphql/generated-e2e-admin-types';
- import {
- ADD_OPTION_GROUP_TO_PRODUCT,
- CREATE_PRODUCT,
- CREATE_PRODUCT_OPTION_GROUP,
- CREATE_PRODUCT_VARIANTS,
- DELETE_PRODUCT,
- DELETE_PRODUCT_VARIANT,
- GET_ASSET_LIST,
- GET_PRODUCT_LIST,
- GET_PRODUCT_SIMPLE,
- GET_PRODUCT_VARIANT_LIST,
- GET_PRODUCT_WITH_VARIANTS,
- UPDATE_CHANNEL,
- UPDATE_GLOBAL_SETTINGS,
- 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(),
- // logger: new DefaultLogger(),
- });
- const removeOptionGuard: ErrorResultGuard<ProductWithOptionsFragment> = createErrorResultGuard(
- input => !!input.optionGroups,
- );
- const updateChannelGuard: ErrorResultGuard<ChannelFragment> = createErrorResultGuard(input => !!input.id);
- 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/820
- it('by slug with multiple assets', async () => {
- const { product: product1 } = await adminClient.query<
- GetProductSimple.Query,
- GetProductSimple.Variables
- >(GET_PRODUCT_SIMPLE, { id: 'T_1' });
- const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
- UPDATE_PRODUCT,
- {
- input: {
- id: product1!.id,
- assetIds: ['T_1', 'T_2', 'T_3'],
- },
- },
- );
- const { product } = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, { slug: product1!.slug });
- if (!product) {
- fail('Product not found');
- return;
- }
- 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<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();
- });
- it('returns null when slug not found', async () => {
- const result = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- slug: 'bad_slug',
- });
- expect(result.product).toBeNull();
- });
- describe('product query with translations', () => {
- let translatedProduct: ProductWithVariants.Fragment;
- let en_translation: ProductWithVariants.Translations;
- let de_translation: ProductWithVariants.Translations;
- beforeAll(async () => {
- const result = await adminClient.query<CreateProduct.Mutation, CreateProduct.Variables>(
- CREATE_PRODUCT,
- {
- 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;
- en_translation = translatedProduct.translations.find(
- t => t.languageCode === LanguageCode.en,
- )!;
- de_translation = translatedProduct.translations.find(
- t => t.languageCode === LanguageCode.de,
- )!;
- });
- it('en slug without translation arg', async () => {
- const { product } = await adminClient.query<
- GetProductSimple.Query,
- GetProductSimple.Variables
- >(GET_PRODUCT_SIMPLE, { slug: en_translation.slug });
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.slug).toBe(en_translation.slug);
- });
- it('de slug without translation arg', async () => {
- const { product } = await adminClient.query<
- GetProductSimple.Query,
- GetProductSimple.Variables
- >(GET_PRODUCT_SIMPLE, { slug: de_translation.slug });
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.slug).toBe(en_translation.slug);
- });
- it('en slug with translation en', async () => {
- const { product } = await adminClient.query<
- GetProductSimple.Query,
- GetProductSimple.Variables
- >(GET_PRODUCT_SIMPLE, { slug: en_translation.slug }, { languageCode: LanguageCode.en });
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.slug).toBe(en_translation.slug);
- });
- it('de slug with translation en', async () => {
- const { product } = await adminClient.query<
- GetProductSimple.Query,
- GetProductSimple.Variables
- >(GET_PRODUCT_SIMPLE, { slug: de_translation.slug }, { languageCode: LanguageCode.en });
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.slug).toBe(en_translation.slug);
- });
- it('en slug with translation de', async () => {
- const { product } = await adminClient.query<
- GetProductSimple.Query,
- GetProductSimple.Variables
- >(GET_PRODUCT_SIMPLE, { slug: en_translation.slug }, { languageCode: LanguageCode.de });
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.slug).toBe(de_translation.slug);
- });
- it('de slug with translation de', async () => {
- const { product } = await adminClient.query<
- GetProductSimple.Query,
- GetProductSimple.Variables
- >(GET_PRODUCT_SIMPLE, { slug: de_translation.slug }, { languageCode: LanguageCode.de });
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.slug).toBe(de_translation.slug);
- });
- it('de slug with translation ru', async () => {
- const { product } = await adminClient.query<
- GetProductSimple.Query,
- GetProductSimple.Variables
- >(GET_PRODUCT_SIMPLE, { slug: de_translation.slug }, { languageCode: LanguageCode.ru });
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.slug).toBe(en_translation.slug);
- });
- });
- describe('product.variants', () => {
- it('returns product variants', async () => {
- const { product } = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: 'T_1',
- });
- expect(product?.variants.length).toBe(4);
- });
- it('returns product variants in existing language', async () => {
- const { product } = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(
- GET_PRODUCT_WITH_VARIANTS,
- {
- 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<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(
- GET_PRODUCT_WITH_VARIANTS,
- {
- 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<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: 'T_1',
- });
- expect(product?.variants.length).toBe(4);
- });
- it('returns product variants in existing language', async () => {
- const { product } = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(
- GET_PRODUCT_WITH_VARIANTS,
- {
- 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<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(
- GET_PRODUCT_WITH_VARIANTS,
- {
- 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<
- GetProductWithVariantList.Query,
- GetProductWithVariantList.Variables
- >(GET_PRODUCT_WITH_VARIANT_LIST, {
- 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<
- GetProductWithVariantList.Query,
- GetProductWithVariantList.Variables
- >(
- GET_PRODUCT_WITH_VARIANT_LIST,
- {
- 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<
- GetProductWithVariantList.Query,
- GetProductWithVariantList.Variables
- >(
- GET_PRODUCT_WITH_VARIANT_LIST,
- {
- id: 'T_1',
- },
- { languageCode: LanguageCode.ru },
- );
- expect(product?.variantList.items.length).toBe(4);
- });
- it('filter & sort', async () => {
- const { product } = await adminClient.query<
- GetProductWithVariantList.Query,
- GetProductWithVariantList.Variables
- >(GET_PRODUCT_WITH_VARIANT_LIST, {
- 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<
- 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,
- 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<
- 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,
- 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<
- GetProductVariantList.Query,
- GetProductVariantList.Variables
- >(GET_PRODUCT_VARIANT_LIST, {
- options: {
- take: 3,
- sort: {
- priceWithTax: SortOrder.ASC,
- },
- },
- });
- expect(productVariants.items).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<
- GetProductVariantList.Query,
- GetProductVariantList.Variables
- >(GET_PRODUCT_VARIANT_LIST, {
- options: {
- take: 3,
- filter: {
- price: {
- between: {
- start: 1400,
- end: 1500,
- },
- },
- },
- },
- });
- expect(productVariants.items).toEqual([
- {
- id: 'T_20',
- name: 'Tripod',
- price: 1498,
- priceWithTax: 1798,
- sku: 'B00XI87KV8',
- },
- ]);
- });
- it('filter by priceWithTax', async () => {
- const { productVariants } = await adminClient.query<
- GetProductVariantList.Query,
- GetProductVariantList.Variables
- >(GET_PRODUCT_VARIANT_LIST, {
- 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).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<
- GetProductVariantList.Query,
- GetProductVariantList.Variables
- >(GET_PRODUCT_VARIANT_LIST, {
- options: {
- take: 3,
- sort: {
- price: SortOrder.ASC,
- },
- },
- productId: 'T_1',
- });
- expect(productVariants.items).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<
- 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 newTranslatedProduct: ProductWithVariants.Fragment;
- 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',
- ]);
- newTranslatedProduct = 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 optionGroup = await createOptionGroup('Quark-type', ['Charm', 'Strange']);
- const result = await adminClient.query<
- AddOptionGroupToProduct.Mutation,
- AddOptionGroupToProduct.Variables
- >(ADD_OPTION_GROUP_TO_PRODUCT, {
- 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<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- optionGroupId: optionGroup.id,
- productId: newProduct.id,
- });
- removeOptionGuard.assertSuccess(removeOptionGroupFromProduct);
- });
- it(
- 'addOptionGroupToProduct errors with an invalid productId',
- assertThrowsWithMessage(
- () =>
- adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
- ADD_OPTION_GROUP_TO_PRODUCT,
- {
- 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<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
- ADD_OPTION_GROUP_TO_PRODUCT,
- {
- 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<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 optionGroup = await createOptionGroup('Length', ['Short', 'Long']);
- const { addOptionGroupToProduct } = await adminClient.query<
- AddOptionGroupToProduct.Mutation,
- AddOptionGroupToProduct.Variables
- >(ADD_OPTION_GROUP_TO_PRODUCT, {
- optionGroupId: optionGroup.id,
- productId: newProductWithAssets.id,
- });
- expect(addOptionGroupToProduct.optionGroups.length).toBe(1);
- const { removeOptionGroupFromProduct } = await adminClient.query<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- 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<
- 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 succeeds if all related ProductVariants are also deleted', async () => {
- const { product } = await adminClient.query<
- GetProductWithVariantsQuery,
- GetProductWithVariantsQueryVariables
- >(GET_PRODUCT_WITH_VARIANTS, { id: 'T_2' });
- // Delete all variants for that product
- for (const variant of product!.variants) {
- await adminClient.query<DeleteProductVariant.Mutation, DeleteProductVariant.Variables>(
- DELETE_PRODUCT_VARIANT,
- {
- id: variant.id,
- },
- );
- }
- const { removeOptionGroupFromProduct } = await adminClient.query<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- optionGroupId: product!.optionGroups[0].id,
- productId: product!.id,
- });
- removeOptionGuard.assertSuccess(removeOptionGroupFromProduct);
- });
- 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 () => {
- 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<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
- ADD_OPTION_GROUP_TO_PRODUCT,
- {
- optionGroupId: optionGroup2.id,
- productId: newProduct.id,
- },
- );
- await adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
- ADD_OPTION_GROUP_TO_PRODUCT,
- {
- optionGroupId: optionGroup3.id,
- productId: newProduct.id,
- },
- );
- });
- 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: group-2, group-3'),
- );
- 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: group-2, group-3'),
- );
- 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 with the selected options already exists: Variant 1'),
- );
- 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);
- });
- // 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];
- const { updateProductVariants } = await adminClient.query<
- UpdateProductVariants.Mutation,
- UpdateProductVariants.Variables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: firstVariant.id,
- translations: firstVariant.translations,
- sku: 'ABCD',
- price: 432,
- },
- ],
- });
- const updatedVariant = updateProductVariants.find(v => v?.id === variants[0].id);
- expect(updatedVariant?.updatedAt).not.toBe(updatedVariant?.createdAt);
- });
- 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(),
- );
- });
- // https://github.com/vendure-ecommerce/vendure/issues/980
- it('creating variants in a non-default language', async () => {
- const { createProduct } = await adminClient.query<
- CreateProduct.Mutation,
- CreateProduct.Variables
- >(CREATE_PRODUCT, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.de,
- name: 'Ananas',
- slug: 'ananas',
- description: 'Yummy Ananas',
- },
- ],
- },
- });
- const { createProductVariants } = await adminClient.query<
- CreateProductVariants.Mutation,
- CreateProductVariants.Variables
- >(CREATE_PRODUCT_VARIANTS, {
- 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<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(
- GET_PRODUCT_WITH_VARIANTS,
- {
- 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?: ProductVariantListOptions,
- ) {
- return adminClient.query<
- GetProductWithVariantList.Query,
- GetProductWithVariantList.Variables
- >(GET_PRODUCT_WITH_VARIANT_LIST, { id, variantListOptions }, { languageCode });
- }
- beforeAll(async () => {
- await adminClient.query<UpdateGlobalSettings.Mutation, UpdateGlobalSettings.Variables>(
- UPDATE_GLOBAL_SETTINGS,
- {
- input: {
- availableLanguages: [LanguageCode.en, LanguageCode.de],
- },
- },
- );
- const { createProduct } = await adminClient.query<
- CreateProduct.Mutation,
- CreateProduct.Variables
- >(CREATE_PRODUCT, {
- input: {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'Bottle',
- slug: 'bottle',
- description: 'A container for liquids',
- },
- ],
- },
- });
- productId = createProduct.id;
- await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
- CREATE_PRODUCT_VARIANTS,
- {
- 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<UpdateChannel.Mutation, UpdateChannel.Variables>(UPDATE_CHANNEL, {
- input: {
- id: 'T_1',
- defaultLanguageCode: LanguageCode.en,
- },
- });
- });
- it('returns all variants', async () => {
- const { product: product1 } = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(
- GET_PRODUCT_WITH_VARIANTS,
- {
- 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<
- UpdateChannel.Mutation,
- UpdateChannel.Variables
- >(UPDATE_CHANNEL, {
- input: {
- id: 'T_1',
- defaultLanguageCode: LanguageCode.de,
- },
- });
- updateChannelGuard.assertSuccess(updateChannel);
- expect(updateChannel.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: GetProductList.Items[];
- let productToDelete: GetProductWithVariants.Product;
- 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 () => {
- const { product } = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: allProducts[0].id,
- });
- const result = await adminClient.query<DeleteProduct.Mutation, DeleteProduct.Variables>(
- DELETE_PRODUCT,
- { id: product!.id },
- );
- expect(result.deleteProduct).toEqual({ result: DeletionResult.DELETED });
- productToDelete = product!;
- });
- it('cannot get a deleted product', async () => {
- const { product } = await adminClient.query<
- GetProductWithVariants.Query,
- GetProductWithVariants.Variables
- >(GET_PRODUCT_WITH_VARIANTS, {
- 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<
- GetProductVariant.Query,
- GetProductVariant.Variables
- >(GET_PRODUCT_VARIANT, {
- id: variant.id,
- });
- expect(productVariant).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);
- });
- // 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<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).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<GetProductSimple.Query, GetProductSimple.Variables>(
- GET_PRODUCT_SIMPLE,
- { slug: productToDelete.slug },
- );
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.slug).toBe(productToDelete.slug);
- });
- });
- async function createOptionGroup(name: string, options: string[]) {
- const { createProductOptionGroup } = await adminClient.query<
- CreateProductOptionGroupMutation,
- CreateProductOptionGroupMutationVariables
- >(CREATE_PRODUCT_OPTION_GROUP, {
- input: {
- code: name.toLowerCase(),
- translations: [{ languageCode: LanguageCode.en, name }],
- options: options.map(option => ({
- code: option.toLowerCase(),
- translations: [{ languageCode: LanguageCode.en, name: option }],
- })),
- },
- });
- return createProductOptionGroup;
- }
- });
- 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_WITH_VARIANT_LIST = gql`
- query GetProductWithVariantList($id: ID, $variantListOptions: ProductVariantListOptions) {
- product(id: $id) {
- id
- variantList(options: $variantListOptions) {
- items {
- ...ProductVariant
- }
- totalItems
- }
- }
- }
- ${PRODUCT_VARIANT_FRAGMENT}
- `;
|