| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964 |
- 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,
- CreateProduct,
- CreateProductVariants,
- DeleteProduct,
- DeleteProductVariant,
- DeletionResult,
- ErrorCode,
- GetAssetList,
- GetOptionGroup,
- GetProductList,
- GetProductSimple,
- GetProductVariant,
- GetProductVariantList,
- GetProductWithVariantList,
- GetProductWithVariants,
- LanguageCode,
- ProductVariantFragment,
- ProductWithOptionsFragment,
- ProductWithVariants,
- RemoveOptionGroupFromProduct,
- SortOrder,
- UpdateProduct,
- UpdateProductVariants,
- } from './graphql/generated-e2e-admin-types';
- import {
- ADD_OPTION_GROUP_TO_PRODUCT,
- CREATE_PRODUCT,
- CREATE_PRODUCT_VARIANTS,
- DELETE_PRODUCT,
- DELETE_PRODUCT_VARIANT,
- GET_ASSET_LIST,
- GET_PRODUCT_LIST,
- GET_PRODUCT_SIMPLE,
- GET_PRODUCT_WITH_VARIANTS,
- UPDATE_PRODUCT,
- UPDATE_PRODUCT_VARIANTS,
- } from './graphql/shared-definitions';
- import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
- // tslint:disable:no-non-null-assertion
- describe('Product resolver', () => {
- const { server, adminClient, shopClient } = createTestEnvironment(testConfig());
- const removeOptionGuard: ErrorResultGuard<ProductWithOptionsFragment> = createErrorResultGuard(
- input => !!input.optionGroups,
- );
- beforeAll(async () => {
- await server.init({
- initialData,
- customerCount: 1,
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
- });
- await adminClient.asSuperAdmin();
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- describe('products list query', () => {
- it('returns all products when no options passed', async () => {
- const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {},
- );
- expect(result.products.items.length).toBe(20);
- expect(result.products.totalItems).toBe(20);
- });
- it('limits result set with skip & take', async () => {
- const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- options: {
- skip: 0,
- take: 3,
- },
- },
- );
- expect(result.products.items.length).toBe(3);
- expect(result.products.totalItems).toBe(20);
- });
- it('filters by name admin', async () => {
- const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- options: {
- filter: {
- name: {
- contains: 'skateboard',
- },
- },
- },
- },
- );
- expect(result.products.items.length).toBe(1);
- expect(result.products.items[0].name).toBe('Cruiser Skateboard');
- });
- it('filters multiple admin', async () => {
- const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- options: {
- filter: {
- name: {
- contains: 'camera',
- },
- slug: {
- contains: 'tent',
- },
- },
- },
- },
- );
- expect(result.products.items.length).toBe(0);
- });
- it('sorts by name admin', async () => {
- const result = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- },
- },
- );
- expect(result.products.items.map(p => p.name)).toEqual([
- 'Bonsai Tree',
- 'Boxing Gloves',
- 'Camera Lens',
- 'Clacky Keyboard',
- 'Cruiser Skateboard',
- 'Curvy Monitor',
- 'Football',
- 'Gaming PC',
- 'Hard Drive',
- 'Instant Camera',
- 'Laptop',
- 'Orchid',
- 'Road Bike',
- 'Running Shoe',
- 'Skipping Rope',
- 'Slr Camera',
- 'Spiky Cactus',
- 'Tent',
- 'Tripod',
- 'USB Cable',
- ]);
- });
- it('filters by name shop', async () => {
- const result = await shopClient.query<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- options: {
- filter: {
- name: {
- contains: 'skateboard',
- },
- },
- },
- },
- );
- expect(result.products.items.length).toBe(1);
- expect(result.products.items[0].name).toBe('Cruiser Skateboard');
- });
- it('sorts by name shop', async () => {
- const result = await shopClient.query<GetProductList.Query, GetProductList.Variables>(
- GET_PRODUCT_LIST,
- {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- },
- },
- );
- expect(result.products.items.map(p => p.name)).toEqual([
- 'Bonsai Tree',
- 'Boxing Gloves',
- 'Camera Lens',
- 'Clacky Keyboard',
- 'Cruiser Skateboard',
- 'Curvy Monitor',
- 'Football',
- 'Gaming PC',
- 'Hard Drive',
- 'Instant Camera',
- 'Laptop',
- 'Orchid',
- 'Road Bike',
- 'Running Shoe',
- 'Skipping Rope',
- 'Slr Camera',
- 'Spiky Cactus',
- 'Tent',
- 'Tripod',
- 'USB Cable',
- ]);
- });
- });
- describe('product query', () => {
- it('by id', async () => {
- const { product } = await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
- GET_PRODUCT_SIMPLE,
- { id: 'T_2' },
- );
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.id).toBe('T_2');
- });
- it('by slug', async () => {
- const { product } = await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
- GET_PRODUCT_SIMPLE,
- { slug: 'curvy-monitor' },
- );
- if (!product) {
- fail('Product not found');
- return;
- }
- expect(product.slug).toBe('curvy-monitor');
- });
- // https://github.com/vendure-ecommerce/vendure/issues/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 result = await adminClient.query<
- AddOptionGroupToProduct.Mutation,
- AddOptionGroupToProduct.Variables
- >(ADD_OPTION_GROUP_TO_PRODUCT, {
- optionGroupId: 'T_2',
- productId: newProduct.id,
- });
- expect(result.addOptionGroupToProduct.optionGroups.length).toBe(1);
- expect(result.addOptionGroupToProduct.optionGroups[0].id).toBe('T_2');
- });
- it(
- 'addOptionGroupToProduct errors with an invalid productId',
- assertThrowsWithMessage(
- () =>
- adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
- ADD_OPTION_GROUP_TO_PRODUCT,
- {
- optionGroupId: 'T_1',
- productId: '999',
- },
- ),
- `No Product with the id '999' could be found`,
- ),
- );
- it(
- 'addOptionGroupToProduct errors with an invalid optionGroupId',
- assertThrowsWithMessage(
- () =>
- adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
- ADD_OPTION_GROUP_TO_PRODUCT,
- {
- optionGroupId: '999',
- productId: newProduct.id,
- },
- ),
- `No ProductOptionGroup with the id '999' could be found`,
- ),
- );
- it('removeOptionGroupFromProduct removes an option group', async () => {
- const { addOptionGroupToProduct } = await adminClient.query<
- AddOptionGroupToProduct.Mutation,
- AddOptionGroupToProduct.Variables
- >(ADD_OPTION_GROUP_TO_PRODUCT, {
- optionGroupId: 'T_1',
- productId: newProductWithAssets.id,
- });
- expect(addOptionGroupToProduct.optionGroups.length).toBe(1);
- const { removeOptionGroupFromProduct } = await adminClient.query<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- optionGroupId: 'T_1',
- productId: newProductWithAssets.id,
- });
- removeOptionGuard.assertSuccess(removeOptionGroupFromProduct);
- expect(removeOptionGroupFromProduct.id).toBe(newProductWithAssets.id);
- expect(removeOptionGroupFromProduct.optionGroups.length).toBe(0);
- });
- it('removeOptionGroupFromProduct return error result if the optionGroup is being used by variants', async () => {
- const { removeOptionGroupFromProduct } = await adminClient.query<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- optionGroupId: 'T_3',
- productId: 'T_2',
- });
- removeOptionGuard.assertErrorResult(removeOptionGroupFromProduct);
- expect(removeOptionGroupFromProduct.message).toBe(
- `Cannot remove ProductOptionGroup "curvy-monitor-monitor-size" as it is used by 2 ProductVariants`,
- );
- expect(removeOptionGroupFromProduct.errorCode).toBe(ErrorCode.PRODUCT_OPTION_IN_USE_ERROR);
- expect(removeOptionGroupFromProduct.optionGroupCode).toBe('curvy-monitor-monitor-size');
- expect(removeOptionGroupFromProduct.productVariantCount).toBe(2);
- });
- it(
- 'removeOptionGroupFromProduct errors with an invalid productId',
- assertThrowsWithMessage(
- () =>
- adminClient.query<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- optionGroupId: '1',
- productId: '999',
- }),
- `No Product with the id '999' could be found`,
- ),
- );
- it(
- 'removeOptionGroupFromProduct errors with an invalid optionGroupId',
- assertThrowsWithMessage(
- () =>
- adminClient.query<
- RemoveOptionGroupFromProduct.Mutation,
- RemoveOptionGroupFromProduct.Variables
- >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
- optionGroupId: '999',
- productId: newProduct.id,
- }),
- `No ProductOptionGroup with the id '999' could be found`,
- ),
- );
- describe('variants', () => {
- let variants: CreateProductVariants.CreateProductVariants[];
- let optionGroup2: GetOptionGroup.ProductOptionGroup;
- let optionGroup3: GetOptionGroup.ProductOptionGroup;
- beforeAll(async () => {
- await adminClient.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
- ADD_OPTION_GROUP_TO_PRODUCT,
- {
- optionGroupId: 'T_3',
- productId: newProduct.id,
- },
- );
- const result1 = await adminClient.query<GetOptionGroup.Query, GetOptionGroup.Variables>(
- GET_OPTION_GROUP,
- { id: 'T_2' },
- );
- const result2 = await adminClient.query<GetOptionGroup.Query, GetOptionGroup.Variables>(
- GET_OPTION_GROUP,
- { id: 'T_3' },
- );
- optionGroup2 = result1.productOptionGroup!;
- optionGroup3 = result2.productOptionGroup!;
- });
- it(
- 'createProductVariants throws if optionIds not compatible with product',
- assertThrowsWithMessage(async () => {
- await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
- CREATE_PRODUCT_VARIANTS,
- {
- input: [
- {
- productId: newProduct.id,
- sku: 'PV1',
- optionIds: [],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
- },
- ],
- },
- );
- }, 'ProductVariant optionIds must include one optionId from each of the groups: curvy-monitor-monitor-size, laptop-ram'),
- );
- it(
- 'createProductVariants throws if optionIds are duplicated',
- assertThrowsWithMessage(async () => {
- await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
- CREATE_PRODUCT_VARIANTS,
- {
- input: [
- {
- productId: newProduct.id,
- sku: 'PV1',
- optionIds: [optionGroup2.options[0].id, optionGroup2.options[1].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
- },
- ],
- },
- );
- }, 'ProductVariant optionIds must include one optionId from each of the groups: curvy-monitor-monitor-size, laptop-ram'),
- );
- it('createProductVariants works', async () => {
- const { createProductVariants } = await adminClient.query<
- CreateProductVariants.Mutation,
- CreateProductVariants.Variables
- >(CREATE_PRODUCT_VARIANTS, {
- input: [
- {
- productId: newProduct.id,
- sku: 'PV1',
- optionIds: [optionGroup2.options[0].id, optionGroup3.options[0].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
- },
- ],
- });
- expect(createProductVariants[0]!.name).toBe('Variant 1');
- expect(createProductVariants[0]!.options.map(pick(['id']))).toContainEqual({
- id: optionGroup2.options[0].id,
- });
- expect(createProductVariants[0]!.options.map(pick(['id']))).toContainEqual({
- id: optionGroup3.options[0].id,
- });
- });
- it('createProductVariants adds multiple variants at once', async () => {
- const { createProductVariants } = await adminClient.query<
- CreateProductVariants.Mutation,
- CreateProductVariants.Variables
- >(CREATE_PRODUCT_VARIANTS, {
- input: [
- {
- productId: newProduct.id,
- sku: 'PV2',
- optionIds: [optionGroup2.options[1].id, optionGroup3.options[0].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 2' }],
- },
- {
- productId: newProduct.id,
- sku: 'PV3',
- optionIds: [optionGroup2.options[1].id, optionGroup3.options[1].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 3' }],
- },
- ],
- });
- const variant2 = createProductVariants.find(v => v!.name === 'Variant 2')!;
- const variant3 = createProductVariants.find(v => v!.name === 'Variant 3')!;
- expect(variant2.options.map(pick(['id']))).toContainEqual({ id: optionGroup2.options[1].id });
- expect(variant2.options.map(pick(['id']))).toContainEqual({ id: optionGroup3.options[0].id });
- expect(variant3.options.map(pick(['id']))).toContainEqual({ id: optionGroup2.options[1].id });
- expect(variant3.options.map(pick(['id']))).toContainEqual({ id: optionGroup3.options[1].id });
- variants = createProductVariants.filter(notNullOrUndefined);
- });
- it(
- 'createProductVariants throws if options combination already exists',
- assertThrowsWithMessage(async () => {
- await adminClient.query<CreateProductVariants.Mutation, CreateProductVariants.Variables>(
- CREATE_PRODUCT_VARIANTS,
- {
- input: [
- {
- productId: newProduct.id,
- sku: 'PV2',
- optionIds: [optionGroup2.options[0].id, optionGroup3.options[0].id],
- translations: [{ languageCode: LanguageCode.en, name: 'Variant 2' }],
- },
- ],
- },
- );
- }, 'A ProductVariant 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);
- });
- });
- });
- 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);
- });
- });
- });
- export const REMOVE_OPTION_GROUP_FROM_PRODUCT = gql`
- mutation RemoveOptionGroupFromProduct($productId: ID!, $optionGroupId: ID!) {
- removeOptionGroupFromProduct(productId: $productId, optionGroupId: $optionGroupId) {
- ...ProductWithOptions
- ... on ProductOptionInUseError {
- errorCode
- message
- optionGroupCode
- productVariantCount
- }
- }
- }
- ${PRODUCT_WITH_OPTIONS_FRAGMENT}
- `;
- export const GET_OPTION_GROUP = gql`
- query GetOptionGroup($id: ID!) {
- productOptionGroup(id: $id) {
- id
- code
- options {
- id
- code
- }
- }
- }
- `;
- export const GET_PRODUCT_VARIANT = gql`
- query GetProductVariant($id: ID!) {
- productVariant(id: $id) {
- id
- name
- }
- }
- `;
- export const GET_PRODUCT_VARIANT_LIST = gql`
- query GetProductVariantLIST($options: ProductVariantListOptions, $productId: ID) {
- productVariants(options: $options, productId: $productId) {
- items {
- id
- name
- sku
- price
- priceWithTax
- }
- totalItems
- }
- }
- `;
- 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}
- `;
|