| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844 |
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
- import { pick } from '@vendure/common/lib/pick';
- import {
- Asset,
- defaultShippingCalculator,
- defaultShippingEligibilityChecker,
- manualFulfillmentHandler,
- mergeConfig,
- } from '@vendure/core';
- import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } from '@vendure/testing';
- import { fail } from 'assert';
- import gql from 'graphql-tag';
- import path from 'path';
- import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
- import {
- testErrorPaymentMethod,
- testFailingPaymentMethod,
- testSuccessfulPaymentMethod,
- } from './fixtures/test-payment-methods';
- import {
- countryCodeShippingEligibilityChecker,
- hydratingShippingEligibilityChecker,
- } from './fixtures/test-shipping-eligibility-checkers';
- import * as Codegen from './graphql/generated-e2e-admin-types';
- import {
- CreateAddressInput,
- CreateShippingMethodDocument,
- CreateShippingMethodInput,
- GlobalFlag,
- LanguageCode,
- } from './graphql/generated-e2e-admin-types';
- import * as CodegenShop from './graphql/generated-e2e-shop-types';
- import { ErrorCode, RemoveItemFromOrderDocument } from './graphql/generated-e2e-shop-types';
- import {
- ATTEMPT_LOGIN,
- CANCEL_ORDER,
- CREATE_SHIPPING_METHOD,
- DELETE_PRODUCT,
- DELETE_PRODUCT_VARIANT,
- DELETE_SHIPPING_METHOD,
- GET_COUNTRY_LIST,
- GET_CUSTOMER,
- GET_CUSTOMER_LIST,
- GET_PRODUCT_WITH_VARIANTS,
- GET_SHIPPING_METHOD_LIST,
- UPDATE_COUNTRY,
- UPDATE_PRODUCT,
- UPDATE_PRODUCT_VARIANTS,
- } from './graphql/shared-definitions';
- import {
- ADD_ITEM_TO_ORDER,
- ADD_PAYMENT,
- ADJUST_ITEM_QUANTITY,
- GET_ACTIVE_ORDER,
- GET_ACTIVE_ORDER_ADDRESSES,
- GET_ACTIVE_ORDER_ORDERS,
- GET_ACTIVE_ORDER_PAYMENTS,
- GET_ACTIVE_ORDER_WITH_PAYMENTS,
- GET_AVAILABLE_COUNTRIES,
- GET_ELIGIBLE_SHIPPING_METHODS,
- GET_NEXT_STATES,
- GET_ORDER_BY_CODE,
- REMOVE_ALL_ORDER_LINES,
- REMOVE_ITEM_FROM_ORDER,
- SET_BILLING_ADDRESS,
- SET_CUSTOMER,
- SET_SHIPPING_ADDRESS,
- SET_SHIPPING_METHOD,
- TRANSITION_TO_STATE,
- UNSET_BILLING_ADDRESS,
- UNSET_SHIPPING_ADDRESS,
- UPDATED_ORDER_FRAGMENT,
- } from './graphql/shop-definitions';
- import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
- describe('Shop orders', () => {
- const { server, adminClient, shopClient } = createTestEnvironment(
- mergeConfig(testConfig(), {
- paymentOptions: {
- paymentMethodHandlers: [
- testSuccessfulPaymentMethod,
- testFailingPaymentMethod,
- testErrorPaymentMethod,
- ],
- },
- shippingOptions: {
- shippingEligibilityCheckers: [
- defaultShippingEligibilityChecker,
- countryCodeShippingEligibilityChecker,
- hydratingShippingEligibilityChecker,
- ],
- },
- customFields: {
- Order: [
- { name: 'giftWrap', type: 'boolean', defaultValue: false },
- { name: 'orderImage', type: 'relation', entity: Asset },
- ],
- OrderLine: [
- { name: 'notes', type: 'string' },
- { name: 'privateField', type: 'string', public: false },
- { name: 'lineImage', type: 'relation', entity: Asset },
- { name: 'lineImages', type: 'relation', list: true, entity: Asset },
- { name: 'dropShip', type: 'boolean', defaultValue: false },
- ],
- },
- orderOptions: {
- orderItemsLimit: 199,
- },
- }),
- );
- type OrderSuccessResult =
- | CodegenShop.UpdatedOrderFragment
- | CodegenShop.TestOrderFragmentFragment
- | CodegenShop.TestOrderWithPaymentsFragment
- | CodegenShop.ActiveOrderCustomerFragment
- | CodegenShop.OrderWithAddressesFragment;
- const orderResultGuard: ErrorResultGuard<OrderSuccessResult> = createErrorResultGuard(
- input => !!input.lines,
- );
- beforeAll(async () => {
- await server.init({
- initialData: {
- ...initialData,
- paymentMethods: [
- {
- name: testSuccessfulPaymentMethod.code,
- handler: { code: testSuccessfulPaymentMethod.code, arguments: [] },
- },
- {
- name: testFailingPaymentMethod.code,
- handler: { code: testFailingPaymentMethod.code, arguments: [] },
- },
- {
- name: testErrorPaymentMethod.code,
- handler: { code: testErrorPaymentMethod.code, arguments: [] },
- },
- ],
- },
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
- customerCount: 3,
- });
- await adminClient.asSuperAdmin();
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- it('availableCountries returns enabled countries', async () => {
- // disable Austria
- const { countries } = await adminClient.query<Codegen.GetCountryListQuery>(GET_COUNTRY_LIST, {});
- const AT = countries.items.find(c => c.code === 'AT')!;
- await adminClient.query<Codegen.UpdateCountryMutation, Codegen.UpdateCountryMutationVariables>(
- UPDATE_COUNTRY,
- {
- input: {
- id: AT.id,
- enabled: false,
- },
- },
- );
- const result =
- await shopClient.query<CodegenShop.GetAvailableCountriesQuery>(GET_AVAILABLE_COUNTRIES);
- expect(result.availableCountries.length).toBe(countries.items.length - 1);
- expect(result.availableCountries.find(c => c.id === AT.id)).toBeUndefined();
- });
- describe('ordering as anonymous user', () => {
- let firstOrderLineId: string;
- let createdCustomerId: string;
- let orderCode: string;
- it('addItemToOrder starts with no session token', () => {
- expect(shopClient.getAuthToken()).toBeFalsy();
- });
- it('activeOrder returns null before any items have been added', async () => {
- const result = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result.activeOrder).toBeNull();
- });
- it('activeOrder creates an anonymous session', () => {
- expect(shopClient.getAuthToken()).not.toBe('');
- });
- it('addItemToOrder creates a new Order with an item', async () => {
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(1);
- expect(addItemToOrder.lines[0].quantity).toBe(1);
- expect(addItemToOrder.lines[0].productVariant.id).toBe('T_1');
- expect(addItemToOrder.lines[0].id).toBe('T_1');
- firstOrderLineId = addItemToOrder.lines[0].id;
- orderCode = addItemToOrder.code;
- });
- it(
- 'addItemToOrder errors with an invalid productVariantId',
- assertThrowsWithMessage(
- () =>
- shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_999',
- quantity: 1,
- }),
- 'No ProductVariant with the id "999" could be found',
- ),
- );
- it('addItemToOrder errors with a negative quantity', async () => {
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_999',
- quantity: -3,
- });
- orderResultGuard.assertErrorResult(addItemToOrder);
- expect(addItemToOrder.message).toEqual('The quantity for an OrderItem cannot be negative');
- expect(addItemToOrder.errorCode).toEqual(ErrorCode.NEGATIVE_QUANTITY_ERROR);
- });
- it('addItemToOrder with an existing productVariantId adds quantity to the existing OrderLine', async () => {
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 2,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(1);
- expect(addItemToOrder.lines[0].quantity).toBe(3);
- });
- describe('OrderLine customFields', () => {
- const GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS = gql`
- query {
- activeOrder {
- lines {
- id
- customFields {
- notes
- lineImage {
- id
- }
- lineImages {
- id
- }
- }
- }
- }
- }
- `;
- it('addItemToOrder with private customFields errors', async () => {
- try {
- await shopClient.query<CodegenShop.AddItemToOrderMutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_2',
- quantity: 1,
- customFields: {
- privateField: 'oh no!',
- },
- },
- );
- fail('Should have thrown');
- } catch (e: any) {
- expect(e.response.errors[0].extensions.code).toBe('BAD_USER_INPUT');
- }
- });
- it('addItemToOrder with equal customFields adds quantity to the existing OrderLine', async () => {
- const { addItemToOrder: add1 } = await shopClient.query<CodegenShop.AddItemToOrderMutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_2',
- quantity: 1,
- customFields: {
- notes: 'note1',
- },
- },
- );
- orderResultGuard.assertSuccess(add1);
- expect(add1.lines.length).toBe(2);
- expect(add1.lines[1].quantity).toBe(1);
- const { addItemToOrder: add2 } = await shopClient.query<CodegenShop.AddItemToOrderMutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_2',
- quantity: 1,
- customFields: {
- notes: 'note1',
- },
- },
- );
- orderResultGuard.assertSuccess(add2);
- expect(add2.lines.length).toBe(2);
- expect(add2.lines[1].quantity).toBe(2);
- await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: add2.lines[1].id,
- });
- });
- it('addItemToOrder with different customFields adds quantity to a new OrderLine', async () => {
- const { addItemToOrder: add1 } = await shopClient.query<CodegenShop.AddItemToOrderMutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_3',
- quantity: 1,
- customFields: {
- notes: 'note2',
- },
- },
- );
- orderResultGuard.assertSuccess(add1);
- expect(add1.lines.length).toBe(2);
- expect(add1.lines[1].quantity).toBe(1);
- const { addItemToOrder: add2 } = await shopClient.query<CodegenShop.AddItemToOrderMutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_3',
- quantity: 1,
- customFields: {
- notes: 'note3',
- },
- },
- );
- orderResultGuard.assertSuccess(add2);
- expect(add2.lines.length).toBe(3);
- expect(add2.lines[1].quantity).toBe(1);
- expect(add2.lines[2].quantity).toBe(1);
- await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: add2.lines[1].id,
- });
- await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: add2.lines[2].id,
- });
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1670
- it('adding a second item after adjusting custom field adds new OrderLine', async () => {
- const { addItemToOrder: add1 } = await shopClient.query<AddItemToOrder.Mutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_3',
- quantity: 1,
- },
- );
- orderResultGuard.assertSuccess(add1);
- expect(add1.lines.length).toBe(2);
- expect(add1.lines[1].quantity).toBe(1);
- const { adjustOrderLine } = await shopClient.query(ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS, {
- orderLineId: add1.lines[1].id,
- quantity: 1,
- customFields: {
- notes: 'updated notes',
- },
- });
- expect(adjustOrderLine.lines[1].customFields).toEqual({
- lineImage: null,
- lineImages: [],
- notes: 'updated notes',
- });
- const { activeOrder: ao1 } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
- expect(ao1.lines[1].customFields).toEqual({
- lineImage: null,
- lineImages: [],
- notes: 'updated notes',
- });
- const updatedNotesLineId = ao1.lines[1].id;
- const { addItemToOrder: add2 } = await shopClient.query<AddItemToOrder.Mutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_3',
- quantity: 1,
- },
- );
- orderResultGuard.assertSuccess(add2);
- expect(add2.lines.length).toBe(3);
- expect(add2.lines[1].quantity).toBe(1);
- expect(add2.lines[2].quantity).toBe(1);
- const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
- expect(activeOrder.lines.find((l: any) => l.id === updatedNotesLineId)?.customFields).toEqual(
- {
- lineImage: null,
- lineImages: [],
- notes: 'updated notes',
- },
- );
- // clean up
- await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: add2.lines[1].id,
- });
- const { removeOrderLine } = await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: add2.lines[2].id,
- });
- orderResultGuard.assertSuccess(removeOrderLine);
- expect(removeOrderLine.lines.length).toBe(1);
- });
- it('addItemToOrder with relation customField', async () => {
- const { addItemToOrder } = await shopClient.query<CodegenShop.AddItemToOrderMutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_3',
- quantity: 1,
- customFields: {
- lineImageId: 'T_1',
- },
- },
- );
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(2);
- expect(addItemToOrder.lines[1].quantity).toBe(1);
- const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
- expect(activeOrder.lines[1].customFields.lineImage).toEqual({ id: 'T_1' });
- });
- it('addItemToOrder with equal relation customField adds to quantity', async () => {
- const { addItemToOrder } = await shopClient.query<CodegenShop.AddItemToOrderMutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_3',
- quantity: 1,
- customFields: {
- lineImageId: 'T_1',
- },
- },
- );
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(2);
- expect(addItemToOrder.lines[1].quantity).toBe(2);
- const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
- expect(activeOrder.lines[1].customFields.lineImage).toEqual({ id: 'T_1' });
- });
- it('addItemToOrder with different relation customField adds new line', async () => {
- const { addItemToOrder } = await shopClient.query<CodegenShop.AddItemToOrderMutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_3',
- quantity: 1,
- customFields: {
- lineImageId: 'T_2',
- },
- },
- );
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(3);
- expect(addItemToOrder.lines[2].quantity).toBe(1);
- const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
- expect(activeOrder.lines[2].customFields.lineImage).toEqual({ id: 'T_2' });
- });
- it('adjustOrderLine updates relation reference', async () => {
- const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
- const { adjustOrderLine } = await shopClient.query(ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS, {
- orderLineId: activeOrder.lines[2].id,
- quantity: 1,
- customFields: {
- lineImageId: 'T_1',
- },
- });
- expect(adjustOrderLine.lines[2].customFields.lineImage).toEqual({ id: 'T_1' });
- await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: activeOrder.lines[2].id,
- });
- const { removeOrderLine } = await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: activeOrder.lines[1].id,
- });
- orderResultGuard.assertSuccess(removeOrderLine);
- expect(removeOrderLine.lines.length).toBe(1);
- });
- it('addItemToOrder with list relation customField', async () => {
- const { addItemToOrder } = await shopClient.query<CodegenShop.AddItemToOrderMutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_3',
- quantity: 1,
- customFields: {
- lineImagesIds: ['T_1', 'T_2'],
- },
- },
- );
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(2);
- expect(addItemToOrder.lines[1].quantity).toBe(1);
- const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
- expect(activeOrder.lines[1].customFields.lineImages.length).toBe(2);
- expect(activeOrder.lines[1].customFields.lineImages).toContainEqual({ id: 'T_1' });
- expect(activeOrder.lines[1].customFields.lineImages).toContainEqual({ id: 'T_2' });
- });
- it('addItemToOrder with equal list relation customField adds to quantity', async () => {
- const { addItemToOrder } = await shopClient.query<CodegenShop.AddItemToOrderMutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_3',
- quantity: 1,
- customFields: {
- lineImagesIds: ['T_1', 'T_2'],
- },
- },
- );
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(2);
- expect(addItemToOrder.lines[1].quantity).toBe(2);
- const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
- expect(activeOrder.lines[1].customFields.lineImages.length).toBe(2);
- expect(activeOrder.lines[1].customFields.lineImages).toContainEqual({ id: 'T_1' });
- expect(activeOrder.lines[1].customFields.lineImages).toContainEqual({ id: 'T_2' });
- });
- it('addItemToOrder with different list relation customField adds new line', async () => {
- const { addItemToOrder } = await shopClient.query<CodegenShop.AddItemToOrderMutation>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: 'T_3',
- quantity: 1,
- customFields: {
- lineImagesIds: ['T_1'],
- },
- },
- );
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(3);
- expect(addItemToOrder.lines[2].quantity).toBe(1);
- const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
- expect(activeOrder.lines[2].customFields.lineImages).toEqual([{ id: 'T_1' }]);
- await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: activeOrder.lines[2].id,
- });
- const { removeOrderLine } = await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: activeOrder.lines[1].id,
- });
- orderResultGuard.assertSuccess(removeOrderLine);
- expect(removeOrderLine.lines.length).toBe(1);
- });
- });
- it('addItemToOrder errors when going beyond orderItemsLimit', async () => {
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 200,
- });
- orderResultGuard.assertErrorResult(addItemToOrder);
- expect(addItemToOrder.message).toBe(
- 'Cannot add items. An order may consist of a maximum of 199 items',
- );
- expect(addItemToOrder.errorCode).toBe(ErrorCode.ORDER_LIMIT_ERROR);
- });
- it('adjustOrderLine adjusts the quantity', async () => {
- const { adjustOrderLine } = await shopClient.query<
- CodegenShop.AdjustItemQuantityMutation,
- CodegenShop.AdjustItemQuantityMutationVariables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: firstOrderLineId,
- quantity: 50,
- });
- orderResultGuard.assertSuccess(adjustOrderLine);
- expect(adjustOrderLine.lines.length).toBe(1);
- expect(adjustOrderLine.lines[0].quantity).toBe(50);
- });
- it('adjustOrderLine with quantity 0 removes the line', async () => {
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_3',
- quantity: 3,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(2);
- expect(addItemToOrder.lines.map(i => i.productVariant.id)).toEqual(['T_1', 'T_3']);
- const { adjustOrderLine } = await shopClient.query<
- CodegenShop.AdjustItemQuantityMutation,
- CodegenShop.AdjustItemQuantityMutationVariables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: addItemToOrder?.lines[1].id,
- quantity: 0,
- });
- orderResultGuard.assertSuccess(adjustOrderLine);
- expect(adjustOrderLine.lines.length).toBe(1);
- expect(adjustOrderLine.lines.map(i => i.productVariant.id)).toEqual(['T_1']);
- });
- it('adjustOrderLine with quantity > stockOnHand only allows user to have stock on hand', async () => {
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_3',
- quantity: 111,
- });
- orderResultGuard.assertErrorResult(addItemToOrder);
- // Insufficient stock error should return because there are only 100 available
- expect(addItemToOrder.errorCode).toBe('INSUFFICIENT_STOCK_ERROR');
- // But it should still add the item to the order
- expect(addItemToOrder.order.lines[1].quantity).toBe(100);
- const { adjustOrderLine } = await shopClient.query<
- CodegenShop.AdjustItemQuantityMutation,
- CodegenShop.AdjustItemQuantityMutationVariables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: addItemToOrder.order.lines[1].id,
- quantity: 101,
- });
- orderResultGuard.assertErrorResult(adjustOrderLine);
- expect(adjustOrderLine.errorCode).toBe('INSUFFICIENT_STOCK_ERROR');
- expect(adjustOrderLine.message).toBe(
- 'Only 100 items were added to the order due to insufficient stock',
- );
- const order = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(order.activeOrder?.lines[1].quantity).toBe(100);
- // clean up
- const { adjustOrderLine: adjustLine2 } = await shopClient.query<
- CodegenShop.AdjustItemQuantityMutation,
- CodegenShop.AdjustItemQuantityMutationVariables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: addItemToOrder.order.lines[1].id,
- quantity: 0,
- });
- orderResultGuard.assertSuccess(adjustLine2);
- expect(adjustLine2.lines.length).toBe(1);
- expect(adjustLine2.lines.map(i => i.productVariant.id)).toEqual(['T_1']);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/2702
- it('stockOnHand check works with multiple order lines with different custom fields', async () => {
- const variantId = 'T_27';
- const { updateProductVariants } = await adminClient.query<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: variantId,
- stockOnHand: 10,
- outOfStockThreshold: 0,
- useGlobalOutOfStockThreshold: false,
- trackInventory: GlobalFlag.TRUE,
- },
- ],
- });
- expect(updateProductVariants[0]?.stockOnHand).toBe(10);
- expect(updateProductVariants[0]?.id).toBe('T_27');
- expect(updateProductVariants[0]?.trackInventory).toBe(GlobalFlag.TRUE);
- const { addItemToOrder: add1 } = await shopClient.query<CodegenShop.AddItemToOrderMutation, any>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: variantId,
- quantity: 9,
- customFields: {
- notes: 'abc',
- },
- },
- );
- orderResultGuard.assertSuccess(add1);
- expect(add1.lines.length).toBe(2);
- expect(add1.lines[1].quantity).toBe(9);
- expect(add1.lines[1].productVariant.id).toBe(variantId);
- const { addItemToOrder: add2 } = await shopClient.query<CodegenShop.AddItemToOrderMutation, any>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: variantId,
- quantity: 2,
- customFields: {
- notes: 'def',
- },
- },
- );
- orderResultGuard.assertErrorResult(add2);
- expect(add2.errorCode).toBe('INSUFFICIENT_STOCK_ERROR');
- expect(add2.message).toBe('Only 1 item was added to the order due to insufficient stock');
- const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(activeOrder?.lines.length).toBe(3);
- expect(activeOrder?.lines[1].quantity).toBe(9);
- expect(activeOrder?.lines[2].quantity).toBe(1);
- // clean up
- await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: activeOrder!.lines[1].id,
- });
- await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: activeOrder!.lines[2].id,
- });
- });
- it('adjustOrderLine handles stockOnHand correctly with multiple order lines with different custom fields when out of stock', async () => {
- const variantId = 'T_27';
- const { updateProductVariants } = await adminClient.query<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: variantId,
- stockOnHand: 10,
- outOfStockThreshold: 0,
- useGlobalOutOfStockThreshold: false,
- trackInventory: GlobalFlag.TRUE,
- },
- ],
- });
- expect(updateProductVariants[0]?.stockOnHand).toBe(10);
- expect(updateProductVariants[0]?.id).toBe('T_27');
- expect(updateProductVariants[0]?.trackInventory).toBe(GlobalFlag.TRUE);
- const { addItemToOrder: add1 } = await shopClient.query<CodegenShop.AddItemToOrderMutation, any>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: variantId,
- quantity: 5,
- customFields: {
- notes: 'abc',
- },
- },
- );
- orderResultGuard.assertSuccess(add1);
- expect(add1.lines.length).toBe(2);
- expect(add1.lines[1].quantity).toBe(5);
- expect(add1.lines[1].productVariant.id).toBe(variantId);
- const { addItemToOrder: add2 } = await shopClient.query<CodegenShop.AddItemToOrderMutation, any>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: variantId,
- quantity: 5,
- customFields: {
- notes: 'def',
- },
- },
- );
- orderResultGuard.assertSuccess(add2);
- expect(add2.lines.length).toBe(3);
- expect(add2.lines[2].quantity).toBe(5);
- expect(add2.lines[2].productVariant.id).toBe(variantId);
- const { adjustOrderLine } = await shopClient.query<
- CodegenShop.AdjustItemQuantityMutation,
- CodegenShop.AdjustItemQuantityMutationVariables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: add2.lines[1].id,
- quantity: 10,
- });
- orderResultGuard.assertErrorResult(adjustOrderLine);
- expect(adjustOrderLine.message).toBe(
- 'Only 5 items were added to the order due to insufficient stock',
- );
- expect(adjustOrderLine.errorCode).toBe(ErrorCode.INSUFFICIENT_STOCK_ERROR);
- const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(activeOrder?.lines.length).toBe(3);
- expect(activeOrder?.lines[1].quantity).toBe(5);
- expect(activeOrder?.lines[2].quantity).toBe(5);
- // clean up
- await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: activeOrder!.lines[1].id,
- });
- await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: activeOrder!.lines[2].id,
- });
- });
- it('adjustOrderLine handles stockOnHand correctly with multiple order lines with different custom fields', async () => {
- const variantId = 'T_27';
- const { updateProductVariants } = await adminClient.query<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: variantId,
- stockOnHand: 10,
- outOfStockThreshold: 0,
- useGlobalOutOfStockThreshold: false,
- trackInventory: GlobalFlag.TRUE,
- },
- ],
- });
- expect(updateProductVariants[0]?.stockOnHand).toBe(10);
- expect(updateProductVariants[0]?.id).toBe('T_27');
- expect(updateProductVariants[0]?.trackInventory).toBe(GlobalFlag.TRUE);
- const { addItemToOrder: add1 } = await shopClient.query<CodegenShop.AddItemToOrderMutation, any>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: variantId,
- quantity: 5,
- customFields: {
- notes: 'abc',
- },
- },
- );
- orderResultGuard.assertSuccess(add1);
- expect(add1.lines.length).toBe(2);
- expect(add1.lines[1].quantity).toBe(5);
- expect(add1.lines[1].productVariant.id).toBe(variantId);
- const { addItemToOrder: add2 } = await shopClient.query<CodegenShop.AddItemToOrderMutation, any>(
- ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
- {
- productVariantId: variantId,
- quantity: 5,
- customFields: {
- notes: 'def',
- },
- },
- );
- orderResultGuard.assertSuccess(add2);
- expect(add2.lines.length).toBe(3);
- expect(add2.lines[2].quantity).toBe(5);
- expect(add2.lines[2].productVariant.id).toBe(variantId);
- const { adjustOrderLine } = await shopClient.query<
- CodegenShop.AdjustItemQuantityMutation,
- CodegenShop.AdjustItemQuantityMutationVariables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: add2.lines[1].id,
- quantity: 3,
- });
- orderResultGuard.assertSuccess(adjustOrderLine);
- expect(adjustOrderLine?.lines.length).toBe(3);
- expect(adjustOrderLine?.lines[1].quantity).toBe(3);
- expect(adjustOrderLine?.lines[2].quantity).toBe(5);
- // clean up
- await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: adjustOrderLine.lines[1].id,
- });
- await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: adjustOrderLine.lines[2].id,
- });
- });
- it('adjustOrderLine errors when going beyond orderItemsLimit', async () => {
- const { adjustOrderLine } = await shopClient.query<
- CodegenShop.AdjustItemQuantityMutation,
- CodegenShop.AdjustItemQuantityMutationVariables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: firstOrderLineId,
- quantity: 200,
- });
- orderResultGuard.assertErrorResult(adjustOrderLine);
- expect(adjustOrderLine.message).toBe(
- 'Cannot add items. An order may consist of a maximum of 199 items',
- );
- expect(adjustOrderLine.errorCode).toBe(ErrorCode.ORDER_LIMIT_ERROR);
- });
- it('adjustOrderLine errors with a negative quantity', async () => {
- const { adjustOrderLine } = await shopClient.query<
- CodegenShop.AdjustItemQuantityMutation,
- CodegenShop.AdjustItemQuantityMutationVariables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: firstOrderLineId,
- quantity: -3,
- });
- orderResultGuard.assertErrorResult(adjustOrderLine);
- expect(adjustOrderLine.message).toBe('The quantity for an OrderItem cannot be negative');
- expect(adjustOrderLine.errorCode).toBe(ErrorCode.NEGATIVE_QUANTITY_ERROR);
- });
- it(
- 'adjustOrderLine errors with an invalid orderLineId',
- assertThrowsWithMessage(
- () =>
- shopClient.query<
- CodegenShop.AdjustItemQuantityMutation,
- CodegenShop.AdjustItemQuantityMutationVariables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: 'T_999',
- quantity: 5,
- }),
- 'This order does not contain an OrderLine with the id 999',
- ),
- );
- it('removeItemFromOrder removes the correct item', async () => {
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_3',
- quantity: 3,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(2);
- expect(addItemToOrder.lines.map(i => i.productVariant.id)).toEqual(['T_1', 'T_3']);
- const { removeOrderLine } = await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: firstOrderLineId,
- });
- orderResultGuard.assertSuccess(removeOrderLine);
- expect(removeOrderLine.lines.length).toBe(1);
- expect(removeOrderLine.lines.map(i => i.productVariant.id)).toEqual(['T_3']);
- });
- it(
- 'removeItemFromOrder errors with an invalid orderItemId',
- assertThrowsWithMessage(
- () =>
- shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: 'T_999',
- }),
- 'This order does not contain an OrderLine with the id 999',
- ),
- );
- it('nextOrderStates returns next valid states', async () => {
- const result = await shopClient.query<CodegenShop.GetNextOrderStatesQuery>(GET_NEXT_STATES);
- expect(result.nextOrderStates).toEqual(['ArrangingPayment', 'Cancelled']);
- });
- it('transitionOrderToState returns error result for invalid state', async () => {
- const { transitionOrderToState } = await shopClient.query<
- CodegenShop.TransitionToStateMutation,
- CodegenShop.TransitionToStateMutationVariables
- >(TRANSITION_TO_STATE, { state: 'Completed' });
- orderResultGuard.assertErrorResult(transitionOrderToState);
- expect(transitionOrderToState!.message).toBe(
- 'Cannot transition Order from "AddingItems" to "Completed"',
- );
- expect(transitionOrderToState!.errorCode).toBe(ErrorCode.ORDER_STATE_TRANSITION_ERROR);
- });
- it('attempting to transition to ArrangingPayment returns error result when Order has no Customer', async () => {
- const { transitionOrderToState } = await shopClient.query<
- CodegenShop.TransitionToStateMutation,
- CodegenShop.TransitionToStateMutationVariables
- >(TRANSITION_TO_STATE, { state: 'ArrangingPayment' });
- orderResultGuard.assertErrorResult(transitionOrderToState);
- expect(transitionOrderToState!.transitionError).toBe(
- 'Cannot transition Order to the "ArrangingPayment" state without Customer details',
- );
- expect(transitionOrderToState!.errorCode).toBe(ErrorCode.ORDER_STATE_TRANSITION_ERROR);
- });
- it('setCustomerForOrder returns error result on email address conflict', async () => {
- const { customers } = await adminClient.query<Codegen.GetCustomerListQuery>(GET_CUSTOMER_LIST);
- const { setCustomerForOrder } = await shopClient.query<
- CodegenShop.SetCustomerForOrderMutation,
- CodegenShop.SetCustomerForOrderMutationVariables
- >(SET_CUSTOMER, {
- input: {
- emailAddress: customers.items[0].emailAddress,
- firstName: 'Test',
- lastName: 'Person',
- },
- });
- orderResultGuard.assertErrorResult(setCustomerForOrder);
- expect(setCustomerForOrder.message).toBe('The email address is not available.');
- expect(setCustomerForOrder.errorCode).toBe(ErrorCode.EMAIL_ADDRESS_CONFLICT_ERROR);
- });
- it('setCustomerForOrder creates a new Customer and associates it with the Order', async () => {
- const { setCustomerForOrder } = await shopClient.query<
- CodegenShop.SetCustomerForOrderMutation,
- CodegenShop.SetCustomerForOrderMutationVariables
- >(SET_CUSTOMER, {
- input: {
- emailAddress: 'test@test.com',
- firstName: 'Test',
- lastName: 'Person',
- },
- });
- orderResultGuard.assertSuccess(setCustomerForOrder);
- const customer = setCustomerForOrder.customer!;
- expect(customer.firstName).toBe('Test');
- expect(customer.lastName).toBe('Person');
- expect(customer.emailAddress).toBe('test@test.com');
- createdCustomerId = customer.id;
- });
- it('setCustomerForOrder updates the existing customer if Customer already set', async () => {
- const { setCustomerForOrder } = await shopClient.query<
- CodegenShop.SetCustomerForOrderMutation,
- CodegenShop.SetCustomerForOrderMutationVariables
- >(SET_CUSTOMER, {
- input: {
- emailAddress: 'test@test.com',
- firstName: 'Changed',
- lastName: 'Person',
- },
- });
- orderResultGuard.assertSuccess(setCustomerForOrder);
- const customer = setCustomerForOrder.customer!;
- expect(customer.firstName).toBe('Changed');
- expect(customer.lastName).toBe('Person');
- expect(customer.emailAddress).toBe('test@test.com');
- expect(customer.id).toBe(createdCustomerId);
- });
- describe('address handling', () => {
- const shippingAddress: CreateAddressInput = {
- fullName: 'name',
- company: 'company',
- streetLine1: '12 Shipping Street',
- streetLine2: null,
- city: 'foo',
- province: 'bar',
- postalCode: '123456',
- countryCode: 'US',
- phoneNumber: '4444444',
- };
- const billingAddress: CreateAddressInput = {
- fullName: 'name',
- company: 'company',
- streetLine1: '22 Billing Avenue',
- streetLine2: null,
- city: 'foo',
- province: 'bar',
- postalCode: '123456',
- countryCode: 'US',
- phoneNumber: '4444444',
- };
- it('setOrderShippingAddress sets shipping address', async () => {
- const { setOrderShippingAddress } = await shopClient.query<
- CodegenShop.SetShippingAddressMutation,
- CodegenShop.SetShippingAddressMutationVariables
- >(SET_SHIPPING_ADDRESS, {
- input: shippingAddress,
- });
- orderResultGuard.assertSuccess(setOrderShippingAddress);
- expect(setOrderShippingAddress.shippingAddress).toEqual({
- fullName: 'name',
- company: 'company',
- streetLine1: '12 Shipping Street',
- streetLine2: null,
- city: 'foo',
- province: 'bar',
- postalCode: '123456',
- country: 'United States of America',
- phoneNumber: '4444444',
- });
- });
- it('setOrderBillingAddress sets billing address', async () => {
- const { setOrderBillingAddress } = await shopClient.query<
- CodegenShop.SetBillingAddressMutation,
- CodegenShop.SetBillingAddressMutationVariables
- >(SET_BILLING_ADDRESS, {
- input: billingAddress,
- });
- orderResultGuard.assertSuccess(setOrderBillingAddress);
- expect(setOrderBillingAddress.billingAddress).toEqual({
- fullName: 'name',
- company: 'company',
- streetLine1: '22 Billing Avenue',
- streetLine2: null,
- city: 'foo',
- province: 'bar',
- postalCode: '123456',
- country: 'United States of America',
- phoneNumber: '4444444',
- });
- });
- it('unsetOrderShippingAddress unsets shipping address', async () => {
- const { unsetOrderShippingAddress } = await shopClient.query<
- CodegenShop.UnsetShippingAddressMutation,
- CodegenShop.UnsetShippingAddressMutationVariables
- >(UNSET_SHIPPING_ADDRESS);
- orderResultGuard.assertSuccess(unsetOrderShippingAddress);
- expect(unsetOrderShippingAddress.shippingAddress).toEqual({
- fullName: null,
- company: null,
- streetLine1: null,
- streetLine2: null,
- city: null,
- province: null,
- postalCode: null,
- country: null,
- phoneNumber: null,
- });
- // Reset the shipping address for subsequent tests
- await shopClient.query<
- CodegenShop.SetShippingAddressMutation,
- CodegenShop.SetShippingAddressMutationVariables
- >(SET_SHIPPING_ADDRESS, {
- input: shippingAddress,
- });
- });
- it('unsetOrderBillingAddress unsets billing address', async () => {
- const { unsetOrderBillingAddress } = await shopClient.query<
- CodegenShop.UnsetBillingAddressMutation,
- CodegenShop.UnsetBillingAddressMutationVariables
- >(UNSET_BILLING_ADDRESS);
- orderResultGuard.assertSuccess(unsetOrderBillingAddress);
- expect(unsetOrderBillingAddress.billingAddress).toEqual({
- fullName: null,
- company: null,
- streetLine1: null,
- streetLine2: null,
- city: null,
- province: null,
- postalCode: null,
- country: null,
- phoneNumber: null,
- });
- // Reset the billing address for subsequent tests
- await shopClient.query<
- CodegenShop.SetBillingAddressMutation,
- CodegenShop.SetBillingAddressMutationVariables
- >(SET_BILLING_ADDRESS, {
- input: billingAddress,
- });
- });
- });
- it('customer default Addresses are not updated before payment', async () => {
- const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- const { customer } = await adminClient.query<
- Codegen.GetCustomerQuery,
- Codegen.GetCustomerQueryVariables
- >(GET_CUSTOMER, { id: activeOrder!.customer!.id });
- expect(customer!.addresses).toEqual([]);
- });
- it('attempting to transition to ArrangingPayment returns error result when Order has no ShippingMethod', async () => {
- const { transitionOrderToState } = await shopClient.query<
- CodegenShop.TransitionToStateMutation,
- CodegenShop.TransitionToStateMutationVariables
- >(TRANSITION_TO_STATE, { state: 'ArrangingPayment' });
- orderResultGuard.assertErrorResult(transitionOrderToState);
- expect(transitionOrderToState!.transitionError).toBe(
- 'Cannot transition Order to the "ArrangingPayment" state without a ShippingMethod',
- );
- expect(transitionOrderToState!.errorCode).toBe(ErrorCode.ORDER_STATE_TRANSITION_ERROR);
- });
- it('can transition to ArrangingPayment once Customer and ShippingMethod has been set', async () => {
- const { eligibleShippingMethods } = await shopClient.query<CodegenShop.GetShippingMethodsQuery>(
- GET_ELIGIBLE_SHIPPING_METHODS,
- );
- const { setOrderShippingMethod } = await shopClient.query<
- CodegenShop.SetShippingMethodMutation,
- CodegenShop.SetShippingMethodMutationVariables
- >(SET_SHIPPING_METHOD, {
- id: eligibleShippingMethods[0].id,
- });
- orderResultGuard.assertSuccess(setOrderShippingMethod);
- const { transitionOrderToState } = await shopClient.query<
- CodegenShop.TransitionToStateMutation,
- CodegenShop.TransitionToStateMutationVariables
- >(TRANSITION_TO_STATE, { state: 'ArrangingPayment' });
- orderResultGuard.assertSuccess(transitionOrderToState);
- expect(pick(transitionOrderToState, ['id', 'state'])).toEqual({
- id: 'T_1',
- state: 'ArrangingPayment',
- });
- });
- it('adds a successful payment and transitions Order state', async () => {
- const { addPaymentToOrder } = await shopClient.query<
- CodegenShop.AddPaymentToOrderMutation,
- CodegenShop.AddPaymentToOrderMutationVariables
- >(ADD_PAYMENT, {
- input: {
- method: testSuccessfulPaymentMethod.code,
- metadata: {},
- },
- });
- orderResultGuard.assertSuccess(addPaymentToOrder);
- const payment = addPaymentToOrder.payments![0];
- expect(addPaymentToOrder.state).toBe('PaymentSettled');
- expect(addPaymentToOrder.active).toBe(false);
- expect(addPaymentToOrder.payments!.length).toBe(1);
- expect(payment.method).toBe(testSuccessfulPaymentMethod.code);
- expect(payment.state).toBe('Settled');
- });
- it('activeOrder is null after payment', async () => {
- const result = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result.activeOrder).toBeNull();
- });
- it('customer default Addresses are updated after payment', async () => {
- const result = await adminClient.query<
- Codegen.GetCustomerQuery,
- Codegen.GetCustomerQueryVariables
- >(GET_CUSTOMER, {
- id: createdCustomerId,
- });
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- const shippingAddress = result.customer!.addresses!.find(a => a.defaultShippingAddress)!;
- expect(shippingAddress.streetLine1).toBe('12 Shipping Street');
- expect(shippingAddress.postalCode).toBe('123456');
- expect(shippingAddress.defaultBillingAddress).toBe(false);
- expect(shippingAddress.defaultShippingAddress).toBe(true);
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- const billingAddress = result.customer!.addresses!.find(a => a.defaultBillingAddress)!;
- expect(billingAddress.streetLine1).toBe('22 Billing Avenue');
- expect(billingAddress.postalCode).toBe('123456');
- expect(billingAddress.defaultBillingAddress).toBe(true);
- expect(billingAddress.defaultShippingAddress).toBe(false);
- });
- it('sets OrderLine.featuredAsset to that of ProductVariant if defined', async () => {
- const { product } = await adminClient.query<
- Codegen.GetProductWithVariantsQuery,
- Codegen.GetProductWithVariantsQueryVariables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: 'T_4',
- });
- const variantWithFeaturedAsset = product?.variants.find(v => !!v.featuredAsset);
- if (!variantWithFeaturedAsset) {
- fail(`Could not find expected variant with a featuredAsset`);
- return;
- }
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: variantWithFeaturedAsset.id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(1);
- expect(addItemToOrder.lines[0].productVariant.id).toBe(variantWithFeaturedAsset?.id);
- expect(addItemToOrder.lines[0].featuredAsset?.id).toBe(
- variantWithFeaturedAsset.featuredAsset?.id,
- );
- });
- it('sets OrderLine.featuredAsset to that of Product if ProductVariant has no featuredAsset', async () => {
- const { product } = await adminClient.query<
- Codegen.GetProductWithVariantsQuery,
- Codegen.GetProductWithVariantsQueryVariables
- >(GET_PRODUCT_WITH_VARIANTS, {
- id: 'T_4',
- });
- const variantWithoutFeaturedAsset = product?.variants.find(v => !v.featuredAsset);
- if (!variantWithoutFeaturedAsset) {
- fail(`Could not find expected variant without a featuredAsset`);
- return;
- }
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: variantWithoutFeaturedAsset.id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(2);
- expect(addItemToOrder.lines[1].productVariant.id).toBe(variantWithoutFeaturedAsset?.id);
- expect(addItemToOrder.lines[1].featuredAsset?.id).toBe(product?.featuredAsset?.id);
- });
- });
- describe('ordering as authenticated user', () => {
- let firstOrderLineId: string;
- let activeOrder: CodegenShop.UpdatedOrderFragment;
- let authenticatedUserEmailAddress: string;
- let customers: Codegen.GetCustomerListQuery['customers']['items'];
- const password = 'test';
- beforeAll(async () => {
- await adminClient.asSuperAdmin();
- const result = await adminClient.query<
- Codegen.GetCustomerListQuery,
- Codegen.GetCustomerListQueryVariables
- >(GET_CUSTOMER_LIST, {
- options: {
- take: 2,
- },
- });
- customers = result.customers.items;
- authenticatedUserEmailAddress = customers[0].emailAddress;
- await shopClient.asUserWithCredentials(authenticatedUserEmailAddress, password);
- });
- it('activeOrder returns null before any items have been added', async () => {
- const result = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result.activeOrder).toBeNull();
- });
- it('addItemToOrder creates a new Order with an item', async () => {
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(1);
- expect(addItemToOrder.lines[0].quantity).toBe(1);
- expect(addItemToOrder.lines[0].productVariant.id).toBe('T_1');
- activeOrder = addItemToOrder!;
- firstOrderLineId = addItemToOrder.lines[0].id;
- });
- it('activeOrder returns order after item has been added', async () => {
- const result = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result.activeOrder!.id).toBe(activeOrder.id);
- expect(result.activeOrder!.state).toBe('AddingItems');
- });
- it('activeOrder resolves customer user', async () => {
- const result = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result.activeOrder!.customer!.user).toEqual({
- id: 'T_2',
- identifier: 'hayden.zieme12@hotmail.com',
- });
- });
- it('addItemToOrder with an existing productVariantId adds quantity to the existing OrderLine', async () => {
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 2,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(1);
- expect(addItemToOrder.lines[0].quantity).toBe(3);
- });
- it('adjustOrderLine adjusts the quantity', async () => {
- const { adjustOrderLine } = await shopClient.query<
- CodegenShop.AdjustItemQuantityMutation,
- CodegenShop.AdjustItemQuantityMutationVariables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: firstOrderLineId,
- quantity: 50,
- });
- orderResultGuard.assertSuccess(adjustOrderLine);
- expect(adjustOrderLine.lines.length).toBe(1);
- expect(adjustOrderLine.lines[0].quantity).toBe(50);
- });
- it('removeItemFromOrder removes the correct item', async () => {
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_3',
- quantity: 3,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(2);
- expect(addItemToOrder.lines.map(i => i.productVariant.id)).toEqual(['T_1', 'T_3']);
- const { removeOrderLine } = await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: firstOrderLineId,
- });
- orderResultGuard.assertSuccess(removeOrderLine);
- expect(removeOrderLine.lines.length).toBe(1);
- expect(removeOrderLine.lines.map(i => i.productVariant.id)).toEqual(['T_3']);
- });
- it('nextOrderStates returns next valid states', async () => {
- const result = await shopClient.query<CodegenShop.GetNextOrderStatesQuery>(GET_NEXT_STATES);
- expect(result.nextOrderStates).toEqual(['ArrangingPayment', 'Cancelled']);
- });
- it('logging out and back in again resumes the last active order', async () => {
- await shopClient.asAnonymousUser();
- const result1 = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result1.activeOrder).toBeNull();
- await shopClient.asUserWithCredentials(authenticatedUserEmailAddress, password);
- const result2 = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result2.activeOrder!.id).toBe(activeOrder.id);
- });
- it('cannot setCustomerForOrder when already logged in', async () => {
- const { setCustomerForOrder } = await shopClient.query<
- CodegenShop.SetCustomerForOrderMutation,
- CodegenShop.SetCustomerForOrderMutationVariables
- >(SET_CUSTOMER, {
- input: {
- emailAddress: 'newperson@email.com',
- firstName: 'New',
- lastName: 'Person',
- },
- });
- orderResultGuard.assertErrorResult(setCustomerForOrder);
- expect(setCustomerForOrder.message).toBe(
- 'Cannot set a Customer for the Order when already logged in',
- );
- expect(setCustomerForOrder.errorCode).toBe(ErrorCode.ALREADY_LOGGED_IN_ERROR);
- });
- describe('shipping', () => {
- let shippingMethods: CodegenShop.GetShippingMethodsQuery['eligibleShippingMethods'];
- it(
- 'setOrderShippingAddress throws with invalid countryCode',
- assertThrowsWithMessage(() => {
- const address: CreateAddressInput = {
- streetLine1: '12 the street',
- countryCode: 'INVALID',
- };
- return shopClient.query<
- CodegenShop.SetShippingAddressMutation,
- CodegenShop.SetShippingAddressMutationVariables
- >(SET_SHIPPING_ADDRESS, {
- input: address,
- });
- }, 'The countryCode "INVALID" was not recognized'),
- );
- it('setOrderShippingAddress sets shipping address', async () => {
- const address: CreateAddressInput = {
- fullName: 'name',
- company: 'company',
- streetLine1: '12 the street',
- streetLine2: null,
- city: 'foo',
- province: 'bar',
- postalCode: '123456',
- countryCode: 'US',
- phoneNumber: '4444444',
- };
- const { setOrderShippingAddress } = await shopClient.query<
- CodegenShop.SetShippingAddressMutation,
- CodegenShop.SetShippingAddressMutationVariables
- >(SET_SHIPPING_ADDRESS, {
- input: address,
- });
- expect(setOrderShippingAddress.shippingAddress).toEqual({
- fullName: 'name',
- company: 'company',
- streetLine1: '12 the street',
- streetLine2: null,
- city: 'foo',
- province: 'bar',
- postalCode: '123456',
- country: 'United States of America',
- phoneNumber: '4444444',
- });
- });
- it('eligibleShippingMethods lists shipping methods', async () => {
- const result = await shopClient.query<CodegenShop.GetShippingMethodsQuery>(
- GET_ELIGIBLE_SHIPPING_METHODS,
- );
- shippingMethods = result.eligibleShippingMethods;
- expect(shippingMethods).toEqual([
- {
- id: 'T_1',
- price: 500,
- code: 'standard-shipping',
- name: 'Standard Shipping',
- description: '',
- },
- {
- id: 'T_2',
- price: 1000,
- code: 'express-shipping',
- name: 'Express Shipping',
- description: '',
- },
- {
- id: 'T_3',
- price: 1000,
- code: 'express-shipping-taxed',
- name: 'Express Shipping (Taxed)',
- description: '',
- },
- ]);
- });
- it('shipping is initially unset', async () => {
- const result = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result.activeOrder!.shipping).toEqual(0);
- expect(result.activeOrder!.shippingLines).toEqual([]);
- });
- it('setOrderShippingMethod sets the shipping method', async () => {
- const result = await shopClient.query<
- CodegenShop.SetShippingMethodMutation,
- CodegenShop.SetShippingMethodMutationVariables
- >(SET_SHIPPING_METHOD, {
- id: shippingMethods[1].id,
- });
- const activeOrderResult =
- await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- const order = activeOrderResult.activeOrder!;
- expect(order.shipping).toBe(shippingMethods[1].price);
- expect(order.shippingLines[0].shippingMethod.id).toBe(shippingMethods[1].id);
- expect(order.shippingLines[0].shippingMethod.description).toBe(
- shippingMethods[1].description,
- );
- });
- it('shipping method is preserved after adjustOrderLine', async () => {
- const activeOrderResult =
- await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- activeOrder = activeOrderResult.activeOrder!;
- const { adjustOrderLine } = await shopClient.query<
- CodegenShop.AdjustItemQuantityMutation,
- CodegenShop.AdjustItemQuantityMutationVariables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: activeOrder.lines[0].id,
- quantity: 10,
- });
- orderResultGuard.assertSuccess(adjustOrderLine);
- expect(adjustOrderLine.shipping).toBe(shippingMethods[1].price);
- expect(adjustOrderLine.shippingLines[0].shippingMethod.id).toBe(shippingMethods[1].id);
- expect(adjustOrderLine.shippingLines[0].shippingMethod.description).toBe(
- shippingMethods[1].description,
- );
- });
- });
- describe('payment', () => {
- it('attempting add a Payment returns error result when in AddingItems state', async () => {
- const { addPaymentToOrder } = await shopClient.query<
- CodegenShop.AddPaymentToOrderMutation,
- CodegenShop.AddPaymentToOrderMutationVariables
- >(ADD_PAYMENT, {
- input: {
- method: testSuccessfulPaymentMethod.code,
- metadata: {},
- },
- });
- orderResultGuard.assertErrorResult(addPaymentToOrder);
- expect(addPaymentToOrder.message).toBe(
- 'A Payment may only be added when Order is in "ArrangingPayment" state',
- );
- expect(addPaymentToOrder.errorCode).toBe(ErrorCode.ORDER_PAYMENT_STATE_ERROR);
- });
- it('transitions to the ArrangingPayment state', async () => {
- const { transitionOrderToState } = await shopClient.query<
- CodegenShop.TransitionToStateMutation,
- CodegenShop.TransitionToStateMutationVariables
- >(TRANSITION_TO_STATE, { state: 'ArrangingPayment' });
- orderResultGuard.assertSuccess(transitionOrderToState);
- expect(pick(transitionOrderToState, ['id', 'state'])).toEqual({
- id: activeOrder.id,
- state: 'ArrangingPayment',
- });
- });
- it('attempting to add an item returns error result when in ArrangingPayment state', async () => {
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_4',
- quantity: 1,
- });
- orderResultGuard.assertErrorResult(addItemToOrder);
- expect(addItemToOrder.message).toBe(
- 'Order contents may only be modified when in the "AddingItems" state',
- );
- expect(addItemToOrder.errorCode).toBe(ErrorCode.ORDER_MODIFICATION_ERROR);
- });
- it('attempting to modify item quantity returns error result when in ArrangingPayment state', async () => {
- const { adjustOrderLine } = await shopClient.query<
- CodegenShop.AdjustItemQuantityMutation,
- CodegenShop.AdjustItemQuantityMutationVariables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: activeOrder.lines[0].id,
- quantity: 12,
- });
- orderResultGuard.assertErrorResult(adjustOrderLine);
- expect(adjustOrderLine.message).toBe(
- 'Order contents may only be modified when in the "AddingItems" state',
- );
- expect(adjustOrderLine.errorCode).toBe(ErrorCode.ORDER_MODIFICATION_ERROR);
- });
- it('attempting to remove an item returns error result when in ArrangingPayment state', async () => {
- const { removeOrderLine } = await shopClient.query<
- CodegenShop.RemoveItemFromOrderMutation,
- CodegenShop.RemoveItemFromOrderMutationVariables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: activeOrder.lines[0].id,
- });
- orderResultGuard.assertErrorResult(removeOrderLine);
- expect(removeOrderLine.message).toBe(
- 'Order contents may only be modified when in the "AddingItems" state',
- );
- expect(removeOrderLine.errorCode).toBe(ErrorCode.ORDER_MODIFICATION_ERROR);
- });
- it('attempting to remove all items returns error result when in ArrangingPayment state', async () => {
- const { removeAllOrderLines } =
- await shopClient.query<CodegenShop.RemoveAllOrderLinesMutation>(REMOVE_ALL_ORDER_LINES);
- orderResultGuard.assertErrorResult(removeAllOrderLines);
- expect(removeAllOrderLines.message).toBe(
- 'Order contents may only be modified when in the "AddingItems" state',
- );
- expect(removeAllOrderLines.errorCode).toBe(ErrorCode.ORDER_MODIFICATION_ERROR);
- });
- it('attempting to setOrderShippingMethod returns error result when in ArrangingPayment state', async () => {
- const shippingMethodsResult = await shopClient.query<CodegenShop.GetShippingMethodsQuery>(
- GET_ELIGIBLE_SHIPPING_METHODS,
- );
- const shippingMethods = shippingMethodsResult.eligibleShippingMethods;
- const { setOrderShippingMethod } = await shopClient.query<
- CodegenShop.SetShippingMethodMutation,
- CodegenShop.SetShippingMethodMutationVariables
- >(SET_SHIPPING_METHOD, {
- id: shippingMethods[0].id,
- });
- orderResultGuard.assertErrorResult(setOrderShippingMethod);
- expect(setOrderShippingMethod.message).toBe(
- 'Order contents may only be modified when in the "AddingItems" state',
- );
- expect(setOrderShippingMethod.errorCode).toBe(ErrorCode.ORDER_MODIFICATION_ERROR);
- });
- it('adds a declined payment', async () => {
- const { addPaymentToOrder } = await shopClient.query<
- CodegenShop.AddPaymentToOrderMutation,
- CodegenShop.AddPaymentToOrderMutationVariables
- >(ADD_PAYMENT, {
- input: {
- method: testFailingPaymentMethod.code,
- metadata: {
- foo: 'bar',
- },
- },
- });
- orderResultGuard.assertErrorResult(addPaymentToOrder);
- expect(addPaymentToOrder.message).toBe('The payment was declined');
- expect(addPaymentToOrder.errorCode).toBe(ErrorCode.PAYMENT_DECLINED_ERROR);
- expect((addPaymentToOrder as any).paymentErrorMessage).toBe('Insufficient funds');
- const { activeOrder: order } =
- await shopClient.query<CodegenShop.GetActiveOrderWithPaymentsQuery>(
- GET_ACTIVE_ORDER_WITH_PAYMENTS,
- );
- const payment = order!.payments![0];
- expect(order!.state).toBe('ArrangingPayment');
- expect(order!.payments!.length).toBe(1);
- expect(payment.method).toBe(testFailingPaymentMethod.code);
- expect(payment.state).toBe('Declined');
- expect(payment.transactionId).toBe(null);
- expect(payment.metadata).toEqual({
- public: { foo: 'bar' },
- });
- });
- it('adds an error payment and returns error result', async () => {
- const { addPaymentToOrder } = await shopClient.query<
- CodegenShop.AddPaymentToOrderMutation,
- CodegenShop.AddPaymentToOrderMutationVariables
- >(ADD_PAYMENT, {
- input: {
- method: testErrorPaymentMethod.code,
- metadata: {
- foo: 'bar',
- },
- },
- });
- orderResultGuard.assertErrorResult(addPaymentToOrder);
- expect(addPaymentToOrder.message).toBe('The payment failed');
- expect(addPaymentToOrder.errorCode).toBe(ErrorCode.PAYMENT_FAILED_ERROR);
- expect((addPaymentToOrder as any).paymentErrorMessage).toBe('Something went horribly wrong');
- const result =
- await shopClient.query<CodegenShop.GetActiveOrderPaymentsQuery>(
- GET_ACTIVE_ORDER_PAYMENTS,
- );
- const payment = result.activeOrder!.payments![1];
- expect(result.activeOrder!.payments!.length).toBe(2);
- expect(payment.method).toBe(testErrorPaymentMethod.code);
- expect(payment.state).toBe('Error');
- expect(payment.errorMessage).toBe('Something went horribly wrong');
- });
- it('adds a successful payment and transitions Order state', async () => {
- const { addPaymentToOrder } = await shopClient.query<
- CodegenShop.AddPaymentToOrderMutation,
- CodegenShop.AddPaymentToOrderMutationVariables
- >(ADD_PAYMENT, {
- input: {
- method: testSuccessfulPaymentMethod.code,
- metadata: {
- baz: 'quux',
- },
- },
- });
- orderResultGuard.assertSuccess(addPaymentToOrder);
- const payment = addPaymentToOrder.payments!.find(p => p.transactionId === '12345')!;
- expect(addPaymentToOrder.state).toBe('PaymentSettled');
- expect(addPaymentToOrder.active).toBe(false);
- expect(addPaymentToOrder.payments!.length).toBe(3);
- expect(payment.method).toBe(testSuccessfulPaymentMethod.code);
- expect(payment.state).toBe('Settled');
- expect(payment.transactionId).toBe('12345');
- expect(payment.metadata).toEqual({
- public: { baz: 'quux' },
- });
- });
- it('does not create new address when Customer already has address', async () => {
- const { customer } = await adminClient.query<
- Codegen.GetCustomerQuery,
- Codegen.GetCustomerQueryVariables
- >(GET_CUSTOMER, { id: customers[0].id });
- expect(customer!.addresses!.length).toBe(1);
- });
- });
- describe('orderByCode', () => {
- describe('immediately after Order is placed', () => {
- it('works when authenticated', async () => {
- const result = await shopClient.query<
- CodegenShop.GetOrderByCodeQuery,
- CodegenShop.GetOrderByCodeQueryVariables
- >(GET_ORDER_BY_CODE, {
- code: activeOrder.code,
- });
- expect(result.orderByCode!.id).toBe(activeOrder.id);
- });
- it('works when anonymous', async () => {
- await shopClient.asAnonymousUser();
- const result = await shopClient.query<
- CodegenShop.GetOrderByCodeQuery,
- CodegenShop.GetOrderByCodeQueryVariables
- >(GET_ORDER_BY_CODE, {
- code: activeOrder.code,
- });
- expect(result.orderByCode!.id).toBe(activeOrder.id);
- });
- it(
- "throws error for another user's Order",
- assertThrowsWithMessage(async () => {
- authenticatedUserEmailAddress = customers[1].emailAddress;
- await shopClient.asUserWithCredentials(authenticatedUserEmailAddress, password);
- return shopClient.query<
- CodegenShop.GetOrderByCodeQuery,
- CodegenShop.GetOrderByCodeQueryVariables
- >(GET_ORDER_BY_CODE, {
- code: activeOrder.code,
- });
- }, 'You are not currently authorized to perform this action'),
- );
- });
- describe('3 hours after the Order has been placed', () => {
- let dateNowMock: any;
- beforeAll(() => {
- // mock Date.now: add 3 hours
- const nowIn3H = Date.now() + 3 * 3600 * 1000;
- dateNowMock = vi.spyOn(global.Date, 'now').mockImplementation(() => nowIn3H);
- });
- it('still works when authenticated as owner', async () => {
- authenticatedUserEmailAddress = customers[0].emailAddress;
- await shopClient.asUserWithCredentials(authenticatedUserEmailAddress, password);
- const result = await shopClient.query<
- CodegenShop.GetOrderByCodeQuery,
- CodegenShop.GetOrderByCodeQueryVariables
- >(GET_ORDER_BY_CODE, {
- code: activeOrder.code,
- });
- expect(result.orderByCode!.id).toBe(activeOrder.id);
- });
- it(
- 'access denied when anonymous',
- assertThrowsWithMessage(async () => {
- await shopClient.asAnonymousUser();
- await shopClient.query<
- CodegenShop.GetOrderByCodeQuery,
- CodegenShop.GetOrderByCodeQueryVariables
- >(GET_ORDER_BY_CODE, {
- code: activeOrder.code,
- });
- }, 'You are not currently authorized to perform this action'),
- );
- afterAll(() => {
- // restore Date.now
- dateNowMock.mockRestore();
- });
- });
- });
- });
- describe('order merging', () => {
- let customers: Codegen.GetCustomerListQuery['customers']['items'];
- beforeAll(async () => {
- const result = await adminClient.query<Codegen.GetCustomerListQuery>(GET_CUSTOMER_LIST);
- customers = result.customers.items;
- });
- it('merges guest order with no existing order', async () => {
- await shopClient.asAnonymousUser();
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(1);
- expect(addItemToOrder.lines[0].productVariant.id).toBe('T_1');
- await shopClient.query<Codegen.AttemptLoginMutation, Codegen.AttemptLoginMutationVariables>(
- ATTEMPT_LOGIN,
- {
- username: customers[1].emailAddress,
- password: 'test',
- },
- );
- const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(activeOrder!.lines.length).toBe(1);
- expect(activeOrder!.lines[0].productVariant.id).toBe('T_1');
- });
- it('merges guest order with existing order', async () => {
- await shopClient.asAnonymousUser();
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_2',
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(1);
- expect(addItemToOrder.lines[0].productVariant.id).toBe('T_2');
- await shopClient.query<Codegen.AttemptLoginMutation, Codegen.AttemptLoginMutationVariables>(
- ATTEMPT_LOGIN,
- {
- username: customers[1].emailAddress,
- password: 'test',
- },
- );
- const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(activeOrder!.lines.length).toBe(2);
- expect(activeOrder!.lines[0].productVariant.id).toBe('T_1');
- expect(activeOrder!.lines[1].productVariant.id).toBe('T_2');
- });
- /**
- * See https://github.com/vendure-ecommerce/vendure/issues/263
- */
- it('does not merge when logging in to a different account (issue #263)', async () => {
- await shopClient.query<Codegen.AttemptLoginMutation, Codegen.AttemptLoginMutationVariables>(
- ATTEMPT_LOGIN,
- {
- username: customers[2].emailAddress,
- password: 'test',
- },
- );
- const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(activeOrder).toBeNull();
- });
- it('does not merge when logging back to other account (issue #263)', async () => {
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_3',
- quantity: 1,
- });
- await shopClient.query<Codegen.AttemptLoginMutation, Codegen.AttemptLoginMutationVariables>(
- ATTEMPT_LOGIN,
- {
- username: customers[1].emailAddress,
- password: 'test',
- },
- );
- const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(activeOrder!.lines.length).toBe(2);
- expect(activeOrder!.lines[0].productVariant.id).toBe('T_1');
- expect(activeOrder!.lines[1].productVariant.id).toBe('T_2');
- });
- // https://github.com/vendure-ecommerce/vendure/issues/754
- it('handles merging when an existing order has OrderLines', async () => {
- async function setShippingOnActiveOrder() {
- await shopClient.query<
- CodegenShop.SetShippingAddressMutation,
- CodegenShop.SetShippingAddressMutationVariables
- >(SET_SHIPPING_ADDRESS, {
- input: {
- streetLine1: '12 the street',
- countryCode: 'US',
- },
- });
- const { eligibleShippingMethods } =
- await shopClient.query<CodegenShop.GetShippingMethodsQuery>(
- GET_ELIGIBLE_SHIPPING_METHODS,
- );
- await shopClient.query<
- CodegenShop.SetShippingMethodMutation,
- CodegenShop.SetShippingMethodMutationVariables
- >(SET_SHIPPING_METHOD, {
- id: eligibleShippingMethods[1].id,
- });
- }
- // Set up an existing order and add a ShippingLine
- await shopClient.asUserWithCredentials(customers[2].emailAddress, 'test');
- await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_3',
- quantity: 1,
- });
- await setShippingOnActiveOrder();
- // Now start a new guest order
- await shopClient.query(LOG_OUT);
- await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_4',
- quantity: 1,
- });
- await setShippingOnActiveOrder();
- // attempt to log in and merge the guest order with the existing order
- const { login } = await shopClient.query<
- Codegen.AttemptLoginMutation,
- Codegen.AttemptLoginMutationVariables
- >(ATTEMPT_LOGIN, {
- username: customers[2].emailAddress,
- password: 'test',
- });
- expect(login.identifier).toBe(customers[2].emailAddress);
- });
- });
- describe('security of customer data', () => {
- let customers: Codegen.GetCustomerListQuery['customers']['items'];
- beforeAll(async () => {
- const result = await adminClient.query<Codegen.GetCustomerListQuery>(GET_CUSTOMER_LIST);
- customers = result.customers.items;
- });
- it('cannot setCustomOrder to existing non-guest Customer', async () => {
- await shopClient.asAnonymousUser();
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- const { setCustomerForOrder } = await shopClient.query<
- CodegenShop.SetCustomerForOrderMutation,
- CodegenShop.SetCustomerForOrderMutationVariables
- >(SET_CUSTOMER, {
- input: {
- emailAddress: customers[0].emailAddress,
- firstName: 'Evil',
- lastName: 'Hacker',
- },
- });
- orderResultGuard.assertErrorResult(setCustomerForOrder);
- expect(setCustomerForOrder.message).toBe('The email address is not available.');
- expect(setCustomerForOrder.errorCode).toBe(ErrorCode.EMAIL_ADDRESS_CONFLICT_ERROR);
- const { customer } = await adminClient.query<
- Codegen.GetCustomerQuery,
- Codegen.GetCustomerQueryVariables
- >(GET_CUSTOMER, {
- id: customers[0].id,
- });
- expect(customer!.firstName).not.toBe('Evil');
- expect(customer!.lastName).not.toBe('Hacker');
- });
- it('guest cannot access Addresses of guest customer', async () => {
- await shopClient.asAnonymousUser();
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- await shopClient.query<
- CodegenShop.SetCustomerForOrderMutation,
- CodegenShop.SetCustomerForOrderMutationVariables
- >(SET_CUSTOMER, {
- input: {
- emailAddress: 'test@test.com',
- firstName: 'Evil',
- lastName: 'Hacker',
- },
- });
- const { activeOrder } =
- await shopClient.query<CodegenShop.GetCustomerAddressesQuery>(GET_ACTIVE_ORDER_ADDRESSES);
- expect(activeOrder!.customer!.addresses).toEqual([]);
- });
- it('guest cannot access Orders of guest customer', async () => {
- await shopClient.asAnonymousUser();
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- await shopClient.query<
- CodegenShop.SetCustomerForOrderMutation,
- CodegenShop.SetCustomerForOrderMutationVariables
- >(SET_CUSTOMER, {
- input: {
- emailAddress: 'test@test.com',
- firstName: 'Evil',
- lastName: 'Hacker',
- },
- });
- const { activeOrder } =
- await shopClient.query<CodegenShop.GetCustomerOrdersQuery>(GET_ACTIVE_ORDER_ORDERS);
- expect(activeOrder!.customer!.orders.items).toEqual([]);
- });
- });
- describe('order custom fields', () => {
- it('custom fields added to type', async () => {
- await shopClient.asAnonymousUser();
- await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- const { activeOrder } = await shopClient.query(GET_ORDER_CUSTOM_FIELDS);
- expect(activeOrder?.customFields).toEqual({
- orderImage: null,
- giftWrap: false,
- });
- });
- it('setting order custom fields', async () => {
- const { setOrderCustomFields } = await shopClient.query(SET_ORDER_CUSTOM_FIELDS, {
- input: {
- customFields: { giftWrap: true, orderImageId: 'T_1' },
- },
- });
- expect(setOrderCustomFields?.customFields).toEqual({
- orderImage: { id: 'T_1' },
- giftWrap: true,
- });
- const { activeOrder } = await shopClient.query(GET_ORDER_CUSTOM_FIELDS);
- expect(activeOrder?.customFields).toEqual({
- orderImage: { id: 'T_1' },
- giftWrap: true,
- });
- });
- });
- describe('remove all order lines', () => {
- beforeAll(async () => {
- await shopClient.asAnonymousUser();
- await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_2',
- quantity: 3,
- });
- });
- it('should remove all order lines', async () => {
- const { removeAllOrderLines } = await shopClient.query<
- CodegenShop.RemoveAllOrderLinesMutation,
- CodegenShop.RemoveAllOrderLinesMutationVariables
- >(REMOVE_ALL_ORDER_LINES);
- orderResultGuard.assertSuccess(removeAllOrderLines);
- expect(removeAllOrderLines?.total).toBe(0);
- expect(removeAllOrderLines?.lines.length).toBe(0);
- });
- });
- describe('validation of product variant availability', () => {
- const bonsaiProductId = 'T_20';
- const bonsaiVariantId = 'T_34';
- beforeAll(async () => {
- await shopClient.asAnonymousUser();
- });
- it(
- 'addItemToOrder errors when product is disabled',
- assertThrowsWithMessage(async () => {
- await adminClient.query<
- Codegen.UpdateProductMutation,
- Codegen.UpdateProductMutationVariables
- >(UPDATE_PRODUCT, {
- input: {
- id: bonsaiProductId,
- enabled: false,
- },
- });
- await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: bonsaiVariantId,
- quantity: 1,
- });
- }, 'No ProductVariant with the id "34" could be found'),
- );
- it(
- 'addItemToOrder errors when product variant is disabled',
- assertThrowsWithMessage(async () => {
- await adminClient.query<
- Codegen.UpdateProductMutation,
- Codegen.UpdateProductMutationVariables
- >(UPDATE_PRODUCT, {
- input: {
- id: bonsaiProductId,
- enabled: true,
- },
- });
- await adminClient.query<
- Codegen.UpdateProductVariantsMutation,
- Codegen.UpdateProductVariantsMutationVariables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: bonsaiVariantId,
- enabled: false,
- },
- ],
- });
- await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: bonsaiVariantId,
- quantity: 1,
- });
- }, 'No ProductVariant with the id "34" could be found'),
- );
- it(
- 'addItemToOrder errors when product is deleted',
- assertThrowsWithMessage(async () => {
- await adminClient.query<
- Codegen.DeleteProductMutation,
- Codegen.DeleteProductMutationVariables
- >(DELETE_PRODUCT, {
- id: bonsaiProductId,
- });
- await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: bonsaiVariantId,
- quantity: 1,
- });
- }, 'No ProductVariant with the id "34" could be found'),
- );
- it(
- 'addItemToOrder errors when product variant is deleted',
- assertThrowsWithMessage(async () => {
- await adminClient.query<
- Codegen.DeleteProductVariantMutation,
- Codegen.DeleteProductVariantMutationVariables
- >(DELETE_PRODUCT_VARIANT, {
- id: bonsaiVariantId,
- });
- await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: bonsaiVariantId,
- quantity: 1,
- });
- }, 'No ProductVariant with the id "34" could be found'),
- );
- let orderWithDeletedProductVariantId: string;
- it('errors when transitioning to ArrangingPayment with deleted variant', async () => {
- const orchidProductId = 'T_19';
- const orchidVariantId = 'T_33';
- await shopClient.asUserWithCredentials('marques.sawayn@hotmail.com', 'test');
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: orchidVariantId,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- orderWithDeletedProductVariantId = addItemToOrder.id;
- await adminClient.query<Codegen.DeleteProductMutation, Codegen.DeleteProductMutationVariables>(
- DELETE_PRODUCT,
- {
- id: orchidProductId,
- },
- );
- const { transitionOrderToState } = await shopClient.query<
- CodegenShop.TransitionToStateMutation,
- CodegenShop.TransitionToStateMutationVariables
- >(TRANSITION_TO_STATE, {
- state: 'ArrangingPayment',
- });
- orderResultGuard.assertErrorResult(transitionOrderToState);
- expect(transitionOrderToState!.transitionError).toBe(
- 'Cannot transition to "ArrangingPayment" because the Order contains ProductVariants which are no longer available',
- );
- expect(transitionOrderToState!.errorCode).toBe(ErrorCode.ORDER_STATE_TRANSITION_ERROR);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1567
- it('allows transitioning to Cancelled with deleted variant', async () => {
- const { cancelOrder } = await adminClient.query<
- Codegen.CancelOrderMutation,
- Codegen.CancelOrderMutationVariables
- >(CANCEL_ORDER, {
- input: {
- orderId: orderWithDeletedProductVariantId,
- },
- });
- orderResultGuard.assertSuccess(cancelOrder);
- expect(cancelOrder.state).toBe('Cancelled');
- });
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1195
- describe('shipping method invalidation', () => {
- let GBShippingMethodId: string;
- let ATShippingMethodId: string;
- beforeAll(async () => {
- // First we will remove all ShippingMethods and set up 2 specialized ones
- const { shippingMethods } =
- await adminClient.query<Codegen.GetShippingMethodListQuery>(GET_SHIPPING_METHOD_LIST);
- for (const method of shippingMethods.items) {
- await adminClient.query<
- Codegen.DeleteShippingMethodMutation,
- Codegen.DeleteShippingMethodMutationVariables
- >(DELETE_SHIPPING_METHOD, {
- id: method.id,
- });
- }
- function createCountryCodeShippingMethodInput(countryCode: string): CreateShippingMethodInput {
- return {
- code: `${countryCode}-shipping`,
- translations: [
- { languageCode: LanguageCode.en, name: `${countryCode} shipping`, description: '' },
- ],
- fulfillmentHandler: manualFulfillmentHandler.code,
- checker: {
- code: countryCodeShippingEligibilityChecker.code,
- arguments: [{ name: 'countryCode', value: countryCode }],
- },
- calculator: {
- code: defaultShippingCalculator.code,
- arguments: [
- { name: 'rate', value: '1000' },
- { name: 'taxRate', value: '0' },
- { name: 'includesTax', value: 'auto' },
- ],
- },
- };
- }
- // Now create 2 shipping methods, valid only for a single country
- const result1 = await adminClient.query<
- Codegen.CreateShippingMethodMutation,
- Codegen.CreateShippingMethodMutationVariables
- >(CREATE_SHIPPING_METHOD, {
- input: createCountryCodeShippingMethodInput('GB'),
- });
- GBShippingMethodId = result1.createShippingMethod.id;
- const result2 = await adminClient.query<
- Codegen.CreateShippingMethodMutation,
- Codegen.CreateShippingMethodMutationVariables
- >(CREATE_SHIPPING_METHOD, {
- input: createCountryCodeShippingMethodInput('AT'),
- });
- ATShippingMethodId = result2.createShippingMethod.id;
- // Now create an order to GB and set the GB shipping method
- const { addItemToOrder } = await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- await shopClient.query<
- CodegenShop.SetCustomerForOrderMutation,
- CodegenShop.SetCustomerForOrderMutationVariables
- >(SET_CUSTOMER, {
- input: {
- emailAddress: 'test-2@test.com',
- firstName: 'Test',
- lastName: 'Person 2',
- },
- });
- await shopClient.query<
- CodegenShop.SetShippingAddressMutation,
- CodegenShop.SetShippingAddressMutationVariables
- >(SET_SHIPPING_ADDRESS, {
- input: {
- streetLine1: '12 the street',
- countryCode: 'GB',
- },
- });
- await shopClient.query<
- CodegenShop.SetShippingMethodMutation,
- CodegenShop.SetShippingMethodMutationVariables
- >(SET_SHIPPING_METHOD, {
- id: GBShippingMethodId,
- });
- });
- it('if selected method no longer eligible, next best is set automatically', async () => {
- const result1 = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result1.activeOrder?.shippingLines[0].shippingMethod.id).toBe(GBShippingMethodId);
- await shopClient.query<
- CodegenShop.SetShippingAddressMutation,
- CodegenShop.SetShippingAddressMutationVariables
- >(SET_SHIPPING_ADDRESS, {
- input: {
- streetLine1: '12 the street',
- countryCode: 'AT',
- },
- });
- const result2 = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result2.activeOrder?.shippingLines[0].shippingMethod.id).toBe(ATShippingMethodId);
- });
- it('if no method is eligible, shipping lines are cleared', async () => {
- await shopClient.query<
- CodegenShop.SetShippingAddressMutation,
- CodegenShop.SetShippingAddressMutationVariables
- >(SET_SHIPPING_ADDRESS, {
- input: {
- streetLine1: '12 the street',
- countryCode: 'US',
- },
- });
- const result = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result.activeOrder?.shippingLines).toEqual([]);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1441
- it('shipping methods are re-evaluated when all OrderLines are removed', async () => {
- const { createShippingMethod } = await adminClient.query<
- CreateShippingMethod.Mutation,
- CreateShippingMethod.Variables
- >(CREATE_SHIPPING_METHOD, {
- input: {
- code: 'min-price-shipping',
- translations: [
- { languageCode: LanguageCode.en, name: 'min price shipping', description: '' },
- ],
- fulfillmentHandler: manualFulfillmentHandler.code,
- checker: {
- code: defaultShippingEligibilityChecker.code,
- arguments: [{ name: 'orderMinimum', value: '100' }],
- },
- calculator: {
- code: defaultShippingCalculator.code,
- arguments: [
- { name: 'rate', value: '1000' },
- { name: 'taxRate', value: '0' },
- { name: 'includesTax', value: 'auto' },
- ],
- },
- },
- });
- const minPriceShippingMethodId = createShippingMethod.id;
- await shopClient.query<SetShippingMethod.Mutation, SetShippingMethod.Variables>(
- SET_SHIPPING_METHOD,
- {
- id: minPriceShippingMethodId,
- },
- );
- const result1 = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result1.activeOrder?.shippingLines[0].shippingMethod.id).toBe(minPriceShippingMethodId);
- const { removeAllOrderLines } = await shopClient.query<
- CodegenShop.RemoveAllOrderLinesMutation,
- CodegenShop.RemoveAllOrderLinesMutationVariables
- >(REMOVE_ALL_ORDER_LINES);
- orderResultGuard.assertSuccess(removeAllOrderLines);
- expect(removeAllOrderLines.shippingLines.length).toBe(0);
- expect(removeAllOrderLines.shippingWithTax).toBe(0);
- });
- });
- describe('edge cases', () => {
- it('calling setShippingMethod and setBillingMethod in parallel does not introduce race condition', async () => {
- const shippingAddress: CreateAddressInput = {
- fullName: 'name',
- company: 'company',
- streetLine1: '12 Shipping Street',
- streetLine2: null,
- city: 'foo',
- province: 'bar',
- postalCode: '123456',
- countryCode: 'US',
- phoneNumber: '4444444',
- };
- const billingAddress: CreateAddressInput = {
- fullName: 'name',
- company: 'company',
- streetLine1: '22 Billing Avenue',
- streetLine2: null,
- city: 'foo',
- province: 'bar',
- postalCode: '123456',
- countryCode: 'US',
- phoneNumber: '4444444',
- };
- await Promise.all([
- shopClient.query<
- CodegenShop.SetBillingAddressMutation,
- CodegenShop.SetBillingAddressMutationVariables
- >(SET_BILLING_ADDRESS, {
- input: billingAddress,
- }),
- shopClient.query<
- CodegenShop.SetShippingAddressMutation,
- CodegenShop.SetShippingAddressMutationVariables
- >(SET_SHIPPING_ADDRESS, {
- input: shippingAddress,
- }),
- ]);
- const { activeOrder } = await shopClient.query(gql`
- query {
- activeOrder {
- shippingAddress {
- ...OrderAddress
- }
- billingAddress {
- ...OrderAddress
- }
- }
- }
- fragment OrderAddress on OrderAddress {
- fullName
- company
- streetLine1
- streetLine2
- city
- province
- postalCode
- countryCode
- phoneNumber
- }
- `);
- expect(activeOrder.shippingAddress).toEqual(shippingAddress);
- expect(activeOrder.billingAddress).toEqual(billingAddress);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/2548
- it('hydrating Order in the ShippingEligibilityChecker does not break order modification', async () => {
- // First we'll create a ShippingMethod that uses the hydrating checker
- await adminClient.query(CreateShippingMethodDocument, {
- input: {
- code: 'hydrating-checker',
- translations: [
- { languageCode: LanguageCode.en, name: 'hydrating checker', description: '' },
- ],
- fulfillmentHandler: manualFulfillmentHandler.code,
- checker: {
- code: hydratingShippingEligibilityChecker.code,
- arguments: [],
- },
- calculator: {
- code: defaultShippingCalculator.code,
- arguments: [
- { name: 'rate', value: '1000' },
- { name: 'taxRate', value: '0' },
- { name: 'includesTax', value: 'auto' },
- ],
- },
- },
- });
- await shopClient.asAnonymousUser();
- await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- await shopClient.query<
- CodegenShop.AddItemToOrderMutation,
- CodegenShop.AddItemToOrderMutationVariables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_2',
- quantity: 3,
- });
- const result1 = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result1.activeOrder?.lines.map(l => l.linePriceWithTax).sort()).toEqual([155880, 503640]);
- expect(result1.activeOrder?.subTotalWithTax).toBe(659520);
- // set the shipping method that uses the hydrating checker
- const { eligibleShippingMethods } = await shopClient.query<CodegenShop.GetShippingMethodsQuery>(
- GET_ELIGIBLE_SHIPPING_METHODS,
- );
- const { setOrderShippingMethod } = await shopClient.query<
- CodegenShop.SetShippingMethodMutation,
- CodegenShop.SetShippingMethodMutationVariables
- >(SET_SHIPPING_METHOD, {
- id: eligibleShippingMethods.find(m => m.code === 'hydrating-checker')!.id,
- });
- orderResultGuard.assertSuccess(setOrderShippingMethod);
- // Remove an item from the order
- const { removeOrderLine } = await shopClient.query(RemoveItemFromOrderDocument, {
- orderLineId: result1.activeOrder!.lines[0].id,
- });
- orderResultGuard.assertSuccess(removeOrderLine);
- expect(removeOrderLine.lines.length).toBe(1);
- const result2 = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(result2.activeOrder?.lines.map(l => l.linePriceWithTax).sort()).toEqual([503640]);
- expect(result2.activeOrder?.subTotalWithTax).toBe(503640);
- });
- });
- });
- const GET_ORDER_CUSTOM_FIELDS = gql`
- query GetOrderCustomFields {
- activeOrder {
- id
- customFields {
- giftWrap
- orderImage {
- id
- }
- }
- }
- }
- `;
- const SET_ORDER_CUSTOM_FIELDS = gql`
- mutation SetOrderCustomFields($input: UpdateOrderInput!) {
- setOrderCustomFields(input: $input) {
- ... on Order {
- id
- customFields {
- giftWrap
- orderImage {
- id
- }
- }
- }
- ... on ErrorResult {
- errorCode
- message
- }
- }
- }
- `;
- export const LOG_OUT = gql`
- mutation LogOut {
- logout {
- success
- }
- }
- `;
- export const ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS = gql`
- mutation AddItemToOrderWithCustomFields(
- $productVariantId: ID!
- $quantity: Int!
- $customFields: OrderLineCustomFieldsInput
- ) {
- addItemToOrder(
- productVariantId: $productVariantId
- quantity: $quantity
- customFields: $customFields
- ) {
- ...UpdatedOrder
- ... on ErrorResult {
- errorCode
- message
- }
- }
- }
- ${UPDATED_ORDER_FRAGMENT}
- `;
- const ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS = gql`
- mutation ($orderLineId: ID!, $quantity: Int!, $customFields: OrderLineCustomFieldsInput) {
- adjustOrderLine(orderLineId: $orderLineId, quantity: $quantity, customFields: $customFields) {
- ... on Order {
- lines {
- id
- customFields {
- notes
- lineImage {
- id
- }
- lineImages {
- id
- }
- }
- }
- }
- }
- }
- `;
|