| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800 |
- /* tslint:disable:no-non-null-assertion */
- import { omit } from '@vendure/common/lib/omit';
- import { pick } from '@vendure/common/lib/pick';
- import {
- containsProducts,
- customerGroup,
- DefaultLogger,
- defaultShippingCalculator,
- defaultShippingEligibilityChecker,
- discountOnItemWithFacets,
- hasFacetValues,
- LogLevel,
- manualFulfillmentHandler,
- minimumOrderAmount,
- orderPercentageDiscount,
- productsPercentageDiscount,
- } from '@vendure/core';
- import {
- createErrorResultGuard,
- createTestEnvironment,
- E2E_DEFAULT_CHANNEL_TOKEN,
- ErrorResultGuard,
- } from '@vendure/testing';
- import gql from 'graphql-tag';
- import path from 'path';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
- import { freeShipping } from '../src/config/promotion/actions/free-shipping-action';
- import { orderFixedDiscount } from '../src/config/promotion/actions/order-fixed-discount-action';
- import { testSuccessfulPaymentMethod } from './fixtures/test-payment-methods';
- import {
- AssignProductsToChannel,
- AssignPromotionToChannel,
- CancelOrderMutation,
- CancelOrderMutationVariables,
- ChannelFragment,
- CreateChannel,
- CreateCustomerGroup,
- CreatePromotion,
- CreatePromotionInput,
- CreateShippingMethod,
- CurrencyCode,
- GetFacetList,
- GetProductsWithVariantPrices,
- HistoryEntryType,
- LanguageCode,
- PromotionFragment,
- RemoveCustomersFromGroup,
- } from './graphql/generated-e2e-admin-types';
- import {
- AddItemToOrder,
- AdjustItemQuantity,
- AdjustmentType,
- ApplyCouponCode,
- ApplyCouponCodeMutation,
- ApplyCouponCodeMutationVariables,
- ErrorCode,
- GetActiveOrder,
- GetOrderPromotionsByCode,
- RemoveCouponCode,
- RemoveItemFromOrder,
- SetCustomerForOrder,
- SetShippingMethod,
- SetShippingMethodMutation,
- SetShippingMethodMutationVariables,
- TestOrderFragmentFragment,
- TestOrderWithPaymentsFragment,
- UpdatedOrderFragment,
- } from './graphql/generated-e2e-shop-types';
- import {
- ASSIGN_PRODUCT_TO_CHANNEL,
- ASSIGN_PROMOTIONS_TO_CHANNEL,
- CANCEL_ORDER,
- CREATE_CHANNEL,
- CREATE_CUSTOMER_GROUP,
- CREATE_PROMOTION,
- CREATE_SHIPPING_METHOD,
- GET_FACET_LIST,
- GET_PRODUCTS_WITH_VARIANT_PRICES,
- REMOVE_CUSTOMERS_FROM_GROUP,
- } from './graphql/shared-definitions';
- import {
- ADD_ITEM_TO_ORDER,
- ADJUST_ITEM_QUANTITY,
- APPLY_COUPON_CODE,
- GET_ACTIVE_ORDER,
- GET_ORDER_PROMOTIONS_BY_CODE,
- REMOVE_COUPON_CODE,
- REMOVE_ITEM_FROM_ORDER,
- SET_CUSTOMER,
- SET_SHIPPING_METHOD,
- } from './graphql/shop-definitions';
- import { addPaymentToOrder, proceedToArrangingPayment } from './utils/test-order-utils';
- describe('Promotions applied to Orders', () => {
- const { server, adminClient, shopClient } = createTestEnvironment({
- ...testConfig(),
- // logger: new DefaultLogger({ level: LogLevel.Info }),
- paymentOptions: {
- paymentMethodHandlers: [testSuccessfulPaymentMethod],
- },
- });
- const freeOrderAction = {
- code: orderPercentageDiscount.code,
- arguments: [{ name: 'discount', value: '100' }],
- };
- const minOrderAmountCondition = (min: number) => ({
- code: minimumOrderAmount.code,
- arguments: [
- { name: 'amount', value: min.toString() },
- { name: 'taxInclusive', value: 'true' },
- ],
- });
- type OrderSuccessResult = UpdatedOrderFragment | TestOrderFragmentFragment;
- const orderResultGuard: ErrorResultGuard<OrderSuccessResult> = createErrorResultGuard(
- input => !!input.lines,
- );
- let products: GetProductsWithVariantPrices.Items[];
- beforeAll(async () => {
- await server.init({
- initialData: {
- ...initialData,
- paymentMethods: [
- {
- name: testSuccessfulPaymentMethod.code,
- handler: { code: testSuccessfulPaymentMethod.code, arguments: [] },
- },
- ],
- },
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-promotions.csv'),
- customerCount: 2,
- });
- await adminClient.asSuperAdmin();
- await getProducts();
- await createGlobalPromotions();
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- describe('coupon codes', () => {
- const TEST_COUPON_CODE = 'TESTCOUPON';
- const EXPIRED_COUPON_CODE = 'EXPIRED';
- let promoFreeWithCoupon: PromotionFragment;
- let promoFreeWithExpiredCoupon: PromotionFragment;
- beforeAll(async () => {
- promoFreeWithCoupon = await createPromotion({
- enabled: true,
- name: 'Free with test coupon',
- couponCode: TEST_COUPON_CODE,
- conditions: [],
- actions: [freeOrderAction],
- });
- promoFreeWithExpiredCoupon = await createPromotion({
- enabled: true,
- name: 'Expired coupon',
- endsAt: new Date(2010, 0, 0),
- couponCode: EXPIRED_COUPON_CODE,
- conditions: [],
- actions: [freeOrderAction],
- });
- await shopClient.asAnonymousUser();
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- });
- afterAll(async () => {
- await deletePromotion(promoFreeWithCoupon.id);
- await deletePromotion(promoFreeWithExpiredCoupon.id);
- });
- it('applyCouponCode returns error result when code is nonexistant', async () => {
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode: 'bad code',
- });
- orderResultGuard.assertErrorResult(applyCouponCode);
- expect(applyCouponCode.message).toBe('Coupon code "bad code" is not valid');
- expect(applyCouponCode.errorCode).toBe(ErrorCode.COUPON_CODE_INVALID_ERROR);
- });
- it('applyCouponCode returns error when code is expired', async () => {
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode: EXPIRED_COUPON_CODE,
- });
- orderResultGuard.assertErrorResult(applyCouponCode);
- expect(applyCouponCode.message).toBe(`Coupon code "${EXPIRED_COUPON_CODE}" has expired`);
- expect(applyCouponCode.errorCode).toBe(ErrorCode.COUPON_CODE_EXPIRED_ERROR);
- });
- it('coupon code application is case-sensitive', async () => {
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode: TEST_COUPON_CODE.toLowerCase(),
- });
- orderResultGuard.assertErrorResult(applyCouponCode);
- expect(applyCouponCode.message).toBe(
- `Coupon code "${TEST_COUPON_CODE.toLowerCase()}" is not valid`,
- );
- expect(applyCouponCode.errorCode).toBe(ErrorCode.COUPON_CODE_INVALID_ERROR);
- });
- it('applies a valid coupon code', async () => {
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode: TEST_COUPON_CODE,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
- expect(applyCouponCode!.discounts.length).toBe(1);
- expect(applyCouponCode!.discounts[0].description).toBe('Free with test coupon');
- expect(applyCouponCode!.totalWithTax).toBe(0);
- });
- it('order history records application', async () => {
- const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
- expect(activeOrder!.history.items.map(i => omit(i, ['id']))).toEqual([
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'Created',
- to: 'AddingItems',
- },
- },
- {
- type: HistoryEntryType.ORDER_COUPON_APPLIED,
- data: {
- couponCode: TEST_COUPON_CODE,
- promotionId: 'T_3',
- },
- },
- ]);
- });
- it('de-duplicates existing codes', async () => {
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode: TEST_COUPON_CODE,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
- });
- it('removes a coupon code', async () => {
- const { removeCouponCode } = await shopClient.query<
- RemoveCouponCode.Mutation,
- RemoveCouponCode.Variables
- >(REMOVE_COUPON_CODE, {
- couponCode: TEST_COUPON_CODE,
- });
- expect(removeCouponCode!.discounts.length).toBe(0);
- expect(removeCouponCode!.totalWithTax).toBe(6000);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/649
- it('discounts array cleared after coupon code removed', async () => {
- const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
- expect(activeOrder?.discounts).toEqual([]);
- });
- it('order history records removal', async () => {
- const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
- expect(activeOrder!.history.items.map(i => omit(i, ['id']))).toEqual([
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'Created',
- to: 'AddingItems',
- },
- },
- {
- type: HistoryEntryType.ORDER_COUPON_APPLIED,
- data: {
- couponCode: TEST_COUPON_CODE,
- promotionId: 'T_3',
- },
- },
- {
- type: HistoryEntryType.ORDER_COUPON_REMOVED,
- data: {
- couponCode: TEST_COUPON_CODE,
- },
- },
- ]);
- });
- it('does not record removal of coupon code that was not added', async () => {
- const { removeCouponCode } = await shopClient.query<
- RemoveCouponCode.Mutation,
- RemoveCouponCode.Variables
- >(REMOVE_COUPON_CODE, {
- couponCode: 'NOT_THERE',
- });
- expect(removeCouponCode!.history.items.map(i => omit(i, ['id']))).toEqual([
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'Created',
- to: 'AddingItems',
- },
- },
- {
- type: HistoryEntryType.ORDER_COUPON_APPLIED,
- data: {
- couponCode: TEST_COUPON_CODE,
- promotionId: 'T_3',
- },
- },
- {
- type: HistoryEntryType.ORDER_COUPON_REMOVED,
- data: {
- couponCode: TEST_COUPON_CODE,
- },
- },
- ]);
- });
- describe('coupon codes in other channels', () => {
- const OTHER_CHANNEL_TOKEN = 'other-channel';
- const OTHER_CHANNEL_COUPON_CODE = 'OTHER_CHANNEL_CODE';
- beforeAll(async () => {
- const { createChannel } = await adminClient.query<
- CreateChannel.Mutation,
- CreateChannel.Variables
- >(CREATE_CHANNEL, {
- input: {
- code: 'other-channel',
- currencyCode: CurrencyCode.GBP,
- pricesIncludeTax: false,
- defaultTaxZoneId: 'T_1',
- defaultShippingZoneId: 'T_1',
- defaultLanguageCode: LanguageCode.en,
- token: OTHER_CHANNEL_TOKEN,
- },
- });
- await createPromotion({
- enabled: true,
- name: 'Other Channel Promo',
- couponCode: OTHER_CHANNEL_COUPON_CODE,
- conditions: [],
- actions: [freeOrderAction],
- });
- });
- afterAll(() => {
- shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1692
- it('does not allow a couponCode from another channel', async () => {
- shopClient.setChannelToken(OTHER_CHANNEL_TOKEN);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode: OTHER_CHANNEL_COUPON_CODE,
- });
- orderResultGuard.assertErrorResult(applyCouponCode);
- expect(applyCouponCode!.errorCode).toEqual('COUPON_CODE_INVALID_ERROR');
- });
- });
- });
- describe('default PromotionConditions', () => {
- beforeEach(async () => {
- await shopClient.asAnonymousUser();
- });
- it('minimumOrderAmount', async () => {
- const promotion = await createPromotion({
- enabled: true,
- name: 'Free if order total greater than 100',
- conditions: [minOrderAmountCondition(10000)],
- actions: [freeOrderAction],
- });
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.totalWithTax).toBe(6000);
- expect(addItemToOrder!.discounts.length).toBe(0);
- const { adjustOrderLine } = await shopClient.query<
- AdjustItemQuantity.Mutation,
- AdjustItemQuantity.Variables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: addItemToOrder!.lines[0].id,
- quantity: 2,
- });
- orderResultGuard.assertSuccess(adjustOrderLine);
- expect(adjustOrderLine!.totalWithTax).toBe(0);
- expect(adjustOrderLine!.discounts[0].description).toBe('Free if order total greater than 100');
- expect(adjustOrderLine!.discounts[0].amountWithTax).toBe(-12000);
- await deletePromotion(promotion.id);
- });
- it('atLeastNWithFacets', async () => {
- const { facets } = await adminClient.query<GetFacetList.Query>(GET_FACET_LIST);
- const saleFacetValue = facets.items[0].values[0];
- const promotion = await createPromotion({
- enabled: true,
- name: 'Free if order contains 2 items with Sale facet value',
- conditions: [
- {
- code: hasFacetValues.code,
- arguments: [
- { name: 'minimum', value: '2' },
- { name: 'facets', value: `["${saleFacetValue.id}"]` },
- ],
- },
- ],
- actions: [freeOrderAction],
- });
- const { addItemToOrder: res1 } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-sale-100').id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(res1);
- expect(res1!.totalWithTax).toBe(120);
- expect(res1!.discounts.length).toBe(0);
- const { addItemToOrder: res2 } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-sale-1000').id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(res2);
- expect(res2!.totalWithTax).toBe(0);
- expect(res2!.discounts.length).toBe(1);
- expect(res2!.totalWithTax).toBe(0);
- expect(res2!.discounts[0].description).toBe(
- 'Free if order contains 2 items with Sale facet value',
- );
- expect(res2!.discounts[0].amountWithTax).toBe(-1320);
- await deletePromotion(promotion.id);
- });
- it('containsProducts', async () => {
- const item5000 = getVariantBySlug('item-5000')!;
- const item1000 = getVariantBySlug('item-1000')!;
- const promotion = await createPromotion({
- enabled: true,
- name: 'Free if buying 3 or more offer products',
- conditions: [
- {
- code: containsProducts.code,
- arguments: [
- { name: 'minimum', value: '3' },
- {
- name: 'productVariantIds',
- value: JSON.stringify([item5000.id, item1000.id]),
- },
- ],
- },
- ],
- actions: [freeOrderAction],
- });
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: item5000.id,
- quantity: 1,
- });
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: item1000.id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.totalWithTax).toBe(7200);
- expect(addItemToOrder!.discounts.length).toBe(0);
- const { adjustOrderLine } = await shopClient.query<
- AdjustItemQuantity.Mutation,
- AdjustItemQuantity.Variables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: addItemToOrder!.lines.find(l => l.productVariant.id === item5000.id)!.id,
- quantity: 2,
- });
- orderResultGuard.assertSuccess(adjustOrderLine);
- expect(adjustOrderLine!.total).toBe(0);
- expect(adjustOrderLine!.discounts[0].description).toBe('Free if buying 3 or more offer products');
- expect(adjustOrderLine!.discounts[0].amountWithTax).toBe(-13200);
- await deletePromotion(promotion.id);
- });
- it('customerGroup', async () => {
- const { createCustomerGroup } = await adminClient.query<
- CreateCustomerGroup.Mutation,
- CreateCustomerGroup.Variables
- >(CREATE_CUSTOMER_GROUP, {
- input: { name: 'Test Group', customerIds: ['T_1'] },
- });
- await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
- const promotion = await createPromotion({
- enabled: true,
- name: 'Free for group members',
- conditions: [
- {
- code: customerGroup.code,
- arguments: [{ name: 'customerGroupId', value: createCustomerGroup.id }],
- },
- ],
- actions: [freeOrderAction],
- });
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.totalWithTax).toBe(0);
- expect(addItemToOrder!.discounts.length).toBe(1);
- expect(addItemToOrder!.discounts[0].description).toBe('Free for group members');
- expect(addItemToOrder!.discounts[0].amountWithTax).toBe(-6000);
- await adminClient.query<RemoveCustomersFromGroup.Mutation, RemoveCustomersFromGroup.Variables>(
- REMOVE_CUSTOMERS_FROM_GROUP,
- {
- groupId: createCustomerGroup.id,
- customerIds: ['T_1'],
- },
- );
- const { adjustOrderLine } = await shopClient.query<
- AdjustItemQuantity.Mutation,
- AdjustItemQuantity.Variables
- >(ADJUST_ITEM_QUANTITY, {
- orderLineId: addItemToOrder!.lines[0].id,
- quantity: 2,
- });
- orderResultGuard.assertSuccess(adjustOrderLine);
- expect(adjustOrderLine!.totalWithTax).toBe(12000);
- expect(adjustOrderLine!.discounts.length).toBe(0);
- await deletePromotion(promotion.id);
- });
- });
- describe('default PromotionActions', () => {
- const TAX_INCLUDED_CHANNEL_TOKEN = 'tax_included_channel';
- let taxIncludedChannel: ChannelFragment;
- beforeAll(async () => {
- // Create a channel where the prices include tax, so we can ensure
- // that PromotionActions are working as expected when taxes are included
- const { createChannel } = await adminClient.query<
- CreateChannel.Mutation,
- CreateChannel.Variables
- >(CREATE_CHANNEL, {
- input: {
- code: 'tax-included-channel',
- currencyCode: CurrencyCode.GBP,
- pricesIncludeTax: true,
- defaultTaxZoneId: 'T_1',
- defaultShippingZoneId: 'T_1',
- defaultLanguageCode: LanguageCode.en,
- token: TAX_INCLUDED_CHANNEL_TOKEN,
- },
- });
- taxIncludedChannel = createChannel as ChannelFragment;
- await adminClient.query<AssignProductsToChannel.Mutation, AssignProductsToChannel.Variables>(
- ASSIGN_PRODUCT_TO_CHANNEL,
- {
- input: {
- channelId: taxIncludedChannel.id,
- priceFactor: 1,
- productIds: products.map(p => p.id),
- },
- },
- );
- });
- beforeEach(async () => {
- await shopClient.asAnonymousUser();
- });
- async function assignPromotionToTaxIncludedChannel(promotionId: string | string[]) {
- await adminClient.query<AssignPromotionToChannel.Mutation, AssignPromotionToChannel.Variables>(
- ASSIGN_PROMOTIONS_TO_CHANNEL,
- {
- input: {
- promotionIds: Array.isArray(promotionId) ? promotionId : [promotionId],
- channelId: taxIncludedChannel.id,
- },
- },
- );
- }
- describe('orderPercentageDiscount', () => {
- const couponCode = '50%_off_order';
- let promotion: PromotionFragment;
- beforeAll(async () => {
- promotion = await createPromotion({
- enabled: true,
- name: '20% discount on order',
- couponCode,
- conditions: [],
- actions: [
- {
- code: orderPercentageDiscount.code,
- arguments: [{ name: 'discount', value: '20' }],
- },
- ],
- });
- await assignPromotionToTaxIncludedChannel(promotion.id);
- });
- afterAll(async () => {
- await deletePromotion(promotion.id);
- });
- it('prices exclude tax', async () => {
- shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.totalWithTax).toBe(6000);
- expect(addItemToOrder!.discounts.length).toBe(0);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.discounts.length).toBe(1);
- expect(applyCouponCode!.discounts[0].description).toBe('20% discount on order');
- expect(applyCouponCode!.totalWithTax).toBe(4800);
- });
- it('prices include tax', async () => {
- shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.totalWithTax).toBe(6000);
- expect(addItemToOrder!.discounts.length).toBe(0);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.discounts.length).toBe(1);
- expect(applyCouponCode!.discounts[0].description).toBe('20% discount on order');
- expect(applyCouponCode!.totalWithTax).toBe(4800);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1773
- it('decimal percentage', async () => {
- const decimalPercentageCouponCode = 'DPCC';
- await createPromotion({
- enabled: true,
- name: '10.5% discount on order',
- couponCode: decimalPercentageCouponCode,
- conditions: [],
- actions: [
- {
- code: orderPercentageDiscount.code,
- arguments: [{ name: 'discount', value: '10.5' }],
- },
- ],
- });
- shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.totalWithTax).toBe(6000);
- expect(addItemToOrder!.discounts.length).toBe(0);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode: decimalPercentageCouponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.discounts.length).toBe(1);
- expect(applyCouponCode!.discounts[0].description).toBe('10.5% discount on order');
- expect(applyCouponCode!.totalWithTax).toBe(5370);
- });
- });
- describe('orderFixedDiscount', () => {
- const couponCode = '10_off_order';
- let promotion: PromotionFragment;
- beforeAll(async () => {
- promotion = await createPromotion({
- enabled: true,
- name: '$10 discount on order',
- couponCode,
- conditions: [],
- actions: [
- {
- code: orderFixedDiscount.code,
- arguments: [{ name: 'discount', value: '1000' }],
- },
- ],
- });
- await assignPromotionToTaxIncludedChannel(promotion.id);
- });
- afterAll(async () => {
- await deletePromotion(promotion.id);
- shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- });
- it('prices exclude tax', async () => {
- shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.total).toBe(5000);
- expect(addItemToOrder!.totalWithTax).toBe(6000);
- expect(addItemToOrder!.discounts.length).toBe(0);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.discounts.length).toBe(1);
- expect(applyCouponCode!.discounts[0].description).toBe('$10 discount on order');
- expect(applyCouponCode!.total).toBe(4000);
- expect(applyCouponCode!.totalWithTax).toBe(4800);
- });
- it('prices include tax', async () => {
- shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.totalWithTax).toBe(6000);
- expect(addItemToOrder!.discounts.length).toBe(0);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.discounts.length).toBe(1);
- expect(applyCouponCode!.discounts[0].description).toBe('$10 discount on order');
- expect(applyCouponCode!.totalWithTax).toBe(5000);
- });
- it('does not result in negative total when shipping is included', async () => {
- shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-100').id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.totalWithTax).toBe(120);
- expect(addItemToOrder!.discounts.length).toBe(0);
- const { setOrderShippingMethod } = await shopClient.query<
- SetShippingMethodMutation,
- SetShippingMethodMutationVariables
- >(SET_SHIPPING_METHOD, {
- id: 'T_1',
- });
- orderResultGuard.assertSuccess(setOrderShippingMethod);
- expect(setOrderShippingMethod.totalWithTax).toBe(620);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.discounts.length).toBe(1);
- expect(applyCouponCode!.discounts[0].description).toBe('$10 discount on order');
- expect(applyCouponCode!.subTotalWithTax).toBe(0);
- expect(applyCouponCode!.totalWithTax).toBe(500); // shipping price
- });
- });
- describe('discountOnItemWithFacets', () => {
- const couponCode = '50%_off_sale_items';
- let promotion: PromotionFragment;
- function getItemSale1Line<
- T extends Array<
- UpdatedOrderFragment['lines'][number] | TestOrderFragmentFragment['lines'][number]
- >,
- >(lines: T): T[number] {
- return lines.find(l => l.productVariant.id === getVariantBySlug('item-sale-100').id)!;
- }
- beforeAll(async () => {
- const { facets } = await adminClient.query<GetFacetList.Query>(GET_FACET_LIST);
- const saleFacetValue = facets.items[0].values[0];
- promotion = await createPromotion({
- enabled: true,
- name: '50% off sale items',
- couponCode,
- conditions: [],
- actions: [
- {
- code: discountOnItemWithFacets.code,
- arguments: [
- { name: 'discount', value: '50' },
- { name: 'facets', value: `["${saleFacetValue.id}"]` },
- ],
- },
- ],
- });
- await assignPromotionToTaxIncludedChannel(promotion.id);
- });
- afterAll(async () => {
- await deletePromotion(promotion.id);
- shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- });
- it('prices exclude tax', async () => {
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-1000').id,
- quantity: 1,
- });
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-sale-1000').id,
- quantity: 1,
- });
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-sale-100').id,
- quantity: 2,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.discounts.length).toBe(0);
- expect(getItemSale1Line(addItemToOrder!.lines).discounts.length).toBe(0);
- expect(addItemToOrder!.total).toBe(2200);
- expect(addItemToOrder!.totalWithTax).toBe(2640);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.total).toBe(1600);
- expect(applyCouponCode!.totalWithTax).toBe(1920);
- expect(getItemSale1Line(applyCouponCode!.lines).discounts.length).toBe(1); // 1x promotion
- const { removeCouponCode } = await shopClient.query<
- RemoveCouponCode.Mutation,
- RemoveCouponCode.Variables
- >(REMOVE_COUPON_CODE, {
- couponCode,
- });
- expect(getItemSale1Line(removeCouponCode!.lines).discounts.length).toBe(0);
- expect(removeCouponCode!.total).toBe(2200);
- expect(removeCouponCode!.totalWithTax).toBe(2640);
- const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
- expect(getItemSale1Line(activeOrder!.lines).discounts.length).toBe(0);
- expect(activeOrder!.total).toBe(2200);
- expect(activeOrder!.totalWithTax).toBe(2640);
- });
- it('prices include tax', async () => {
- shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-1000').id,
- quantity: 1,
- });
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-sale-1000').id,
- quantity: 1,
- });
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-sale-100').id,
- quantity: 2,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.discounts.length).toBe(0);
- expect(getItemSale1Line(addItemToOrder!.lines).discounts.length).toBe(0);
- expect(addItemToOrder!.total).toBe(2200);
- expect(addItemToOrder!.totalWithTax).toBe(2640);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.total).toBe(1600);
- expect(applyCouponCode!.totalWithTax).toBe(1920);
- expect(getItemSale1Line(applyCouponCode!.lines).discounts.length).toBe(1); // 1x promotion
- const { removeCouponCode } = await shopClient.query<
- RemoveCouponCode.Mutation,
- RemoveCouponCode.Variables
- >(REMOVE_COUPON_CODE, {
- couponCode,
- });
- expect(getItemSale1Line(removeCouponCode!.lines).discounts.length).toBe(0);
- expect(removeCouponCode!.total).toBe(2200);
- expect(removeCouponCode!.totalWithTax).toBe(2640);
- const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
- expect(getItemSale1Line(activeOrder!.lines).discounts.length).toBe(0);
- expect(activeOrder!.total).toBe(2200);
- expect(activeOrder!.totalWithTax).toBe(2640);
- });
- });
- describe('productsPercentageDiscount', () => {
- const couponCode = '50%_off_product';
- let promotion: PromotionFragment;
- beforeAll(async () => {
- promotion = await createPromotion({
- enabled: true,
- name: '50% off product',
- couponCode,
- conditions: [],
- actions: [
- {
- code: productsPercentageDiscount.code,
- arguments: [
- { name: 'discount', value: '50' },
- {
- name: 'productVariantIds',
- value: `["${getVariantBySlug('item-5000').id}"]`,
- },
- ],
- },
- ],
- });
- await assignPromotionToTaxIncludedChannel(promotion.id);
- });
- afterAll(async () => {
- await deletePromotion(promotion.id);
- shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- });
- it('prices exclude tax', async () => {
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.discounts.length).toBe(0);
- expect(addItemToOrder!.lines[0].discounts.length).toBe(0);
- expect(addItemToOrder!.total).toBe(5000);
- expect(addItemToOrder!.totalWithTax).toBe(6000);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.total).toBe(2500);
- expect(applyCouponCode!.totalWithTax).toBe(3000);
- expect(applyCouponCode!.lines[0].discounts.length).toBe(1); // 1x promotion
- });
- it('prices include tax', async () => {
- shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.discounts.length).toBe(0);
- expect(addItemToOrder!.lines[0].discounts.length).toBe(0);
- expect(addItemToOrder!.total).toBe(5000);
- expect(addItemToOrder!.totalWithTax).toBe(6000);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.total).toBe(2500);
- expect(applyCouponCode!.totalWithTax).toBe(3000);
- expect(applyCouponCode!.lines[0].discounts.length).toBe(1); // 1x promotion
- });
- });
- describe('freeShipping', () => {
- const couponCode = 'FREE_SHIPPING';
- let promotion: PromotionFragment;
- // The test shipping method needs to be created in each Channel, since ShippingMethods
- // are ChannelAware
- async function createTestShippingMethod(channelToken: string) {
- adminClient.setChannelToken(channelToken);
- const result = await adminClient.query<
- CreateShippingMethod.Mutation,
- CreateShippingMethod.Variables
- >(CREATE_SHIPPING_METHOD, {
- input: {
- code: 'test-method',
- fulfillmentHandler: manualFulfillmentHandler.code,
- checker: {
- code: defaultShippingEligibilityChecker.code,
- arguments: [
- {
- name: 'orderMinimum',
- value: '0',
- },
- ],
- },
- calculator: {
- code: defaultShippingCalculator.code,
- arguments: [
- { name: 'rate', value: '345' },
- { name: 'includesTax', value: 'auto' },
- { name: 'taxRate', value: '20' },
- ],
- },
- translations: [
- { languageCode: LanguageCode.en, name: 'test method', description: '' },
- ],
- },
- });
- adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- return result.createShippingMethod;
- }
- beforeAll(async () => {
- promotion = await createPromotion({
- enabled: true,
- name: 'Free shipping',
- couponCode,
- conditions: [],
- actions: [
- {
- code: freeShipping.code,
- arguments: [],
- },
- ],
- });
- await assignPromotionToTaxIncludedChannel(promotion.id);
- });
- afterAll(async () => {
- await deletePromotion(promotion.id);
- shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- });
- it('prices exclude tax', async () => {
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- const method = await createTestShippingMethod(E2E_DEFAULT_CHANNEL_TOKEN);
- const { setOrderShippingMethod } = await shopClient.query<
- SetShippingMethod.Mutation,
- SetShippingMethod.Variables
- >(SET_SHIPPING_METHOD, {
- id: method.id,
- });
- orderResultGuard.assertSuccess(setOrderShippingMethod);
- expect(setOrderShippingMethod.discounts).toEqual([]);
- expect(setOrderShippingMethod.shipping).toBe(345);
- expect(setOrderShippingMethod.shippingWithTax).toBe(414);
- expect(setOrderShippingMethod.total).toBe(5345);
- expect(setOrderShippingMethod.totalWithTax).toBe(6414);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode.discounts.length).toBe(1);
- expect(applyCouponCode.discounts[0].description).toBe('Free shipping');
- expect(applyCouponCode.shipping).toBe(0);
- expect(applyCouponCode.shippingWithTax).toBe(0);
- expect(applyCouponCode.total).toBe(5000);
- expect(applyCouponCode.totalWithTax).toBe(6000);
- });
- it('prices include tax', async () => {
- shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- const method = await createTestShippingMethod(TAX_INCLUDED_CHANNEL_TOKEN);
- const { setOrderShippingMethod } = await shopClient.query<
- SetShippingMethod.Mutation,
- SetShippingMethod.Variables
- >(SET_SHIPPING_METHOD, {
- id: method.id,
- });
- orderResultGuard.assertSuccess(setOrderShippingMethod);
- expect(setOrderShippingMethod.discounts).toEqual([]);
- expect(setOrderShippingMethod.shipping).toBe(287);
- expect(setOrderShippingMethod.shippingWithTax).toBe(345);
- expect(setOrderShippingMethod.total).toBe(5287);
- expect(setOrderShippingMethod.totalWithTax).toBe(6345);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode.discounts.length).toBe(1);
- expect(applyCouponCode.discounts[0].description).toBe('Free shipping');
- expect(applyCouponCode.shipping).toBe(0);
- expect(applyCouponCode.shippingWithTax).toBe(0);
- expect(applyCouponCode.total).toBe(5000);
- expect(applyCouponCode.totalWithTax).toBe(6000);
- });
- // https://github.com/vendure-ecommerce/vendure/pull/1150
- it('shipping discounts get correctly removed', async () => {
- shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- const method = await createTestShippingMethod(TAX_INCLUDED_CHANNEL_TOKEN);
- const { setOrderShippingMethod } = await shopClient.query<
- SetShippingMethod.Mutation,
- SetShippingMethod.Variables
- >(SET_SHIPPING_METHOD, {
- id: method.id,
- });
- orderResultGuard.assertSuccess(setOrderShippingMethod);
- expect(setOrderShippingMethod.discounts).toEqual([]);
- expect(setOrderShippingMethod.shippingWithTax).toBe(345);
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode.discounts.length).toBe(1);
- expect(applyCouponCode.discounts[0].description).toBe('Free shipping');
- expect(applyCouponCode.shippingWithTax).toBe(0);
- const { removeCouponCode } = await shopClient.query<
- RemoveCouponCode.Mutation,
- RemoveCouponCode.Variables
- >(REMOVE_COUPON_CODE, {
- couponCode,
- });
- orderResultGuard.assertSuccess(removeCouponCode);
- expect(removeCouponCode.discounts).toEqual([]);
- expect(removeCouponCode.shippingWithTax).toBe(345);
- });
- });
- describe('multiple promotions simultaneously', () => {
- const saleItem50pcOffCoupon = 'CODE1';
- const order15pcOffCoupon = 'CODE2';
- let promotion1: PromotionFragment;
- let promotion2: PromotionFragment;
- beforeAll(async () => {
- const { facets } = await adminClient.query<GetFacetList.Query>(GET_FACET_LIST);
- const saleFacetValue = facets.items[0].values[0];
- promotion1 = await createPromotion({
- enabled: true,
- name: 'item promo',
- couponCode: saleItem50pcOffCoupon,
- conditions: [],
- actions: [
- {
- code: discountOnItemWithFacets.code,
- arguments: [
- { name: 'discount', value: '50' },
- { name: 'facets', value: `["${saleFacetValue.id}"]` },
- ],
- },
- ],
- });
- promotion2 = await createPromotion({
- enabled: true,
- name: 'order promo',
- couponCode: order15pcOffCoupon,
- conditions: [],
- actions: [
- {
- code: orderPercentageDiscount.code,
- arguments: [{ name: 'discount', value: '15' }],
- },
- ],
- });
- await assignPromotionToTaxIncludedChannel([promotion1.id, promotion2.id]);
- });
- afterAll(async () => {
- await deletePromotion(promotion1.id);
- await deletePromotion(promotion2.id);
- shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
- });
- it('prices exclude tax', async () => {
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-sale-1000').id,
- quantity: 2,
- });
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- // Apply the OrderItem-level promo
- const { applyCouponCode: apply1 } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode: saleItem50pcOffCoupon,
- });
- orderResultGuard.assertSuccess(apply1);
- const saleItemLine = apply1.lines.find(
- l => l.productVariant.id === getVariantBySlug('item-sale-1000').id,
- )!;
- expect(saleItemLine.discounts.length).toBe(1); // 1x promotion
- expect(
- saleItemLine.discounts.find(a => a.type === AdjustmentType.PROMOTION)?.description,
- ).toBe('item promo');
- expect(apply1.discounts.length).toBe(1);
- expect(apply1.total).toBe(6000);
- expect(apply1.totalWithTax).toBe(7200);
- // Apply the Order-level promo
- const { applyCouponCode: apply2 } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode: order15pcOffCoupon,
- });
- orderResultGuard.assertSuccess(apply2);
- expect(apply2.discounts.map(d => d.description).sort()).toEqual([
- 'item promo',
- 'order promo',
- ]);
- expect(apply2.total).toBe(5100);
- expect(apply2.totalWithTax).toBe(6120);
- });
- it('prices include tax', async () => {
- shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-sale-1000').id,
- quantity: 2,
- });
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- // Apply the OrderItem-level promo
- const { applyCouponCode: apply1 } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode: saleItem50pcOffCoupon,
- });
- orderResultGuard.assertSuccess(apply1);
- const saleItemLine = apply1.lines.find(
- l => l.productVariant.id === getVariantBySlug('item-sale-1000').id,
- )!;
- expect(saleItemLine.discounts.length).toBe(1); // 1x promotion
- expect(
- saleItemLine.discounts.find(a => a.type === AdjustmentType.PROMOTION)?.description,
- ).toBe('item promo');
- expect(apply1.discounts.length).toBe(1);
- expect(apply1.total).toBe(6000);
- expect(apply1.totalWithTax).toBe(7200);
- // Apply the Order-level promo
- const { applyCouponCode: apply2 } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, {
- couponCode: order15pcOffCoupon,
- });
- orderResultGuard.assertSuccess(apply2);
- expect(apply2.discounts.map(d => d.description).sort()).toEqual([
- 'item promo',
- 'order promo',
- ]);
- expect(apply2.total).toBe(5100);
- expect(apply2.totalWithTax).toBe(6120);
- });
- });
- });
- describe('per-customer usage limit', () => {
- const TEST_COUPON_CODE = 'TESTCOUPON';
- const orderGuard: ErrorResultGuard<TestOrderWithPaymentsFragment> = createErrorResultGuard(
- input => !!input.lines,
- );
- let promoWithUsageLimit: PromotionFragment;
- beforeAll(async () => {
- promoWithUsageLimit = await createPromotion({
- enabled: true,
- name: 'Free with test coupon',
- couponCode: TEST_COUPON_CODE,
- perCustomerUsageLimit: 1,
- conditions: [],
- actions: [freeOrderAction],
- });
- });
- afterAll(async () => {
- await deletePromotion(promoWithUsageLimit.id);
- });
- async function createNewActiveOrder() {
- const { addItemToOrder } = await shopClient.query<
- AddItemToOrder.Mutation,
- AddItemToOrder.Variables
- >(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- });
- return addItemToOrder;
- }
- describe('guest customer', () => {
- const GUEST_EMAIL_ADDRESS = 'guest@test.com';
- let orderCode: string;
- function addGuestCustomerToOrder() {
- return shopClient.query<SetCustomerForOrder.Mutation, SetCustomerForOrder.Variables>(
- SET_CUSTOMER,
- {
- input: {
- emailAddress: GUEST_EMAIL_ADDRESS,
- firstName: 'Guest',
- lastName: 'Customer',
- },
- },
- );
- }
- it('allows initial usage', async () => {
- await shopClient.asAnonymousUser();
- await createNewActiveOrder();
- await addGuestCustomerToOrder();
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.totalWithTax).toBe(0);
- expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
- await proceedToArrangingPayment(shopClient);
- const order = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
- orderGuard.assertSuccess(order);
- expect(order.state).toBe('PaymentSettled');
- expect(order.active).toBe(false);
- orderCode = order.code;
- });
- it('adds Promotions to Order once payment arranged', async () => {
- const { orderByCode } = await shopClient.query<
- GetOrderPromotionsByCode.Query,
- GetOrderPromotionsByCode.Variables
- >(GET_ORDER_PROMOTIONS_BY_CODE, {
- code: orderCode,
- });
- expect(orderByCode!.promotions.map(pick(['name']))).toEqual([
- { name: 'Free with test coupon' },
- ]);
- });
- it('returns error result when usage exceeds limit', async () => {
- await shopClient.asAnonymousUser();
- await createNewActiveOrder();
- await addGuestCustomerToOrder();
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
- orderResultGuard.assertErrorResult(applyCouponCode);
- expect(applyCouponCode.message).toEqual(
- 'Coupon code cannot be used more than once per customer',
- );
- });
- it('removes couponCode from order when adding customer after code applied', async () => {
- await shopClient.asAnonymousUser();
- await createNewActiveOrder();
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.totalWithTax).toBe(0);
- expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
- await addGuestCustomerToOrder();
- const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
- expect(activeOrder!.couponCodes).toEqual([]);
- expect(activeOrder!.totalWithTax).toBe(6000);
- });
- it('does not remove valid couponCode when setting guest customer', async () => {
- await shopClient.asAnonymousUser();
- await createNewActiveOrder();
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.totalWithTax).toBe(0);
- expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
- await shopClient.query<SetCustomerForOrder.Mutation, SetCustomerForOrder.Variables>(
- SET_CUSTOMER,
- {
- input: {
- emailAddress: 'new-guest@test.com',
- firstName: 'New Guest',
- lastName: 'Customer',
- },
- },
- );
- const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
- expect(activeOrder!.couponCodes).toEqual([TEST_COUPON_CODE]);
- expect(applyCouponCode!.totalWithTax).toBe(0);
- });
- });
- describe('signed-in customer', () => {
- function logInAsRegisteredCustomer() {
- return shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
- }
- let orderId: string;
- it('allows initial usage', async () => {
- await logInAsRegisteredCustomer();
- await createNewActiveOrder();
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.totalWithTax).toBe(0);
- expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
- await proceedToArrangingPayment(shopClient);
- const order = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
- orderGuard.assertSuccess(order);
- orderId = order.id;
- expect(order.state).toBe('PaymentSettled');
- expect(order.active).toBe(false);
- });
- it('returns error result when usage exceeds limit', async () => {
- await logInAsRegisteredCustomer();
- await createNewActiveOrder();
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
- orderResultGuard.assertErrorResult(applyCouponCode);
- expect(applyCouponCode.message).toEqual(
- 'Coupon code cannot be used more than once per customer',
- );
- expect(applyCouponCode.errorCode).toBe(ErrorCode.COUPON_CODE_LIMIT_ERROR);
- });
- it('removes couponCode from order when logging in after code applied', async () => {
- await shopClient.asAnonymousUser();
- await createNewActiveOrder();
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
- expect(applyCouponCode!.totalWithTax).toBe(0);
- await logInAsRegisteredCustomer();
- const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
- expect(activeOrder!.totalWithTax).toBe(6000);
- expect(activeOrder!.couponCodes).toEqual([]);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1466
- it('cancelled orders do not count against usage limit', async () => {
- const { cancelOrder } = await adminClient.query<
- CancelOrderMutation,
- CancelOrderMutationVariables
- >(CANCEL_ORDER, {
- input: {
- orderId,
- cancelShipping: true,
- reason: 'request',
- },
- });
- orderResultGuard.assertSuccess(cancelOrder);
- expect(cancelOrder.state).toBe('Cancelled');
- await logInAsRegisteredCustomer();
- await createNewActiveOrder();
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCode.Mutation,
- ApplyCouponCode.Variables
- >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode!.totalWithTax).toBe(0);
- expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
- });
- });
- });
- // https://github.com/vendure-ecommerce/vendure/issues/710
- it('removes order-level discount made invalid by removing OrderLine', async () => {
- const promotion = await createPromotion({
- enabled: true,
- name: 'Test Promo',
- conditions: [minOrderAmountCondition(10000)],
- actions: [
- {
- code: orderFixedDiscount.code,
- arguments: [{ name: 'discount', value: '1000' }],
- },
- ],
- });
- await shopClient.asAnonymousUser();
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-1000').id,
- quantity: 8,
- });
- const { addItemToOrder } = await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(
- ADD_ITEM_TO_ORDER,
- {
- productVariantId: getVariantBySlug('item-5000').id,
- quantity: 1,
- },
- );
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder!.discounts.length).toBe(1);
- expect(addItemToOrder!.discounts[0].description).toBe('Test Promo');
- const { activeOrder: check1 } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
- expect(check1!.discounts.length).toBe(1);
- expect(check1!.discounts[0].description).toBe('Test Promo');
- const { removeOrderLine } = await shopClient.query<
- RemoveItemFromOrder.Mutation,
- RemoveItemFromOrder.Variables
- >(REMOVE_ITEM_FROM_ORDER, {
- orderLineId: addItemToOrder.lines[1].id,
- });
- orderResultGuard.assertSuccess(removeOrderLine);
- expect(removeOrderLine.discounts.length).toBe(0);
- const { activeOrder: check2 } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
- expect(check2!.discounts.length).toBe(0);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1492
- it('correctly handles pro-ration of variants with 0 price', async () => {
- const couponCode = '20%_off_order';
- const promotion = await createPromotion({
- enabled: true,
- name: '20% discount on order',
- couponCode,
- conditions: [],
- actions: [
- {
- code: orderPercentageDiscount.code,
- arguments: [{ name: 'discount', value: '20' }],
- },
- ],
- });
- await shopClient.asAnonymousUser();
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-100').id,
- quantity: 1,
- });
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: getVariantBySlug('item-0').id,
- quantity: 1,
- });
- const { applyCouponCode } = await shopClient.query<
- ApplyCouponCodeMutation,
- ApplyCouponCodeMutationVariables
- >(APPLY_COUPON_CODE, { couponCode });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode.totalWithTax).toBe(96);
- });
- async function getProducts() {
- const result = await adminClient.query<GetProductsWithVariantPrices.Query>(
- GET_PRODUCTS_WITH_VARIANT_PRICES,
- {
- options: {
- take: 10,
- skip: 0,
- },
- },
- );
- products = result.products.items;
- }
- async function createGlobalPromotions() {
- const { facets } = await adminClient.query<GetFacetList.Query>(GET_FACET_LIST);
- const saleFacetValue = facets.items[0].values[0];
- await createPromotion({
- enabled: true,
- name: 'Promo not yet started',
- startsAt: new Date(2199, 0, 0),
- conditions: [minOrderAmountCondition(100)],
- actions: [freeOrderAction],
- });
- const deletedPromotion = await createPromotion({
- enabled: true,
- name: 'Deleted promotion',
- conditions: [minOrderAmountCondition(100)],
- actions: [freeOrderAction],
- });
- await deletePromotion(deletedPromotion.id);
- }
- async function createPromotion(input: CreatePromotionInput): Promise<PromotionFragment> {
- const result = await adminClient.query<CreatePromotion.Mutation, CreatePromotion.Variables>(
- CREATE_PROMOTION,
- {
- input,
- },
- );
- return result.createPromotion as PromotionFragment;
- }
- function getVariantBySlug(
- slug: 'item-100' | 'item-1000' | 'item-5000' | 'item-sale-100' | 'item-sale-1000' | 'item-0',
- ): GetProductsWithVariantPrices.Variants {
- return products.find(p => p.slug === slug)!.variants[0];
- }
- async function deletePromotion(promotionId: string) {
- await adminClient.query(gql`
- mutation DeletePromotionAdHoc1 {
- deletePromotion(id: "${promotionId}") {
- result
- }
- }
- `);
- }
- });
|