| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754 |
- import { LanguageCode } from '@vendure/common/lib/generated-types';
- import { mergeConfig, orderPercentageDiscount } from '@vendure/core';
- import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } from '@vendure/testing';
- import path from 'path';
- import { afterAll, beforeAll, describe, expect, it } from 'vitest';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
- import { testSuccessfulPaymentMethod } from './fixtures/test-payment-methods';
- import { TokenActiveOrderPlugin } from './fixtures/test-plugins/token-active-order-plugin';
- import { ResultOf } from './graphql/graphql-admin';
- import { graphql, ResultOf as ShopResultOf } from './graphql/graphql-shop';
- import { createPromotionDocument, getCustomerListDocument } from './graphql/shared-definitions';
- import { addItemToOrderDocument, getActiveOrderDocument } from './graphql/shop-definitions';
- import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
- describe('custom ActiveOrderStrategy', () => {
- const { server, adminClient, shopClient } = createTestEnvironment(
- mergeConfig(testConfig(), {
- plugins: [TokenActiveOrderPlugin],
- paymentOptions: {
- paymentMethodHandlers: [testSuccessfulPaymentMethod],
- },
- customFields: {
- Order: [
- {
- name: 'message',
- type: 'string',
- nullable: true,
- },
- ],
- },
- }),
- );
- type OrderResult = Extract<
- ShopResultOf<typeof addItemToOrderWithTokenDocument>['addItemToOrder'],
- { __typename?: 'Order' }
- >;
- const orderResultGuard: ErrorResultGuard<OrderResult> = createErrorResultGuard(
- input => !('errorCode' in input) && !('message' in input),
- );
- let customers: ResultOf<typeof getCustomerListDocument>['customers']['items'];
- beforeAll(async () => {
- await server.init({
- initialData: {
- ...initialData,
- paymentMethods: [
- {
- name: testSuccessfulPaymentMethod.code,
- handler: { code: testSuccessfulPaymentMethod.code, arguments: [] },
- },
- ],
- },
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
- customerCount: 3,
- });
- await adminClient.asSuperAdmin();
- const result = await adminClient.query(getCustomerListDocument);
- customers = result.customers.items;
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- it('activeOrder with no createActiveOrder defined returns null', async () => {
- const { activeOrder } = await shopClient.query(getActiveOrderDocument);
- expect(activeOrder).toBeNull();
- });
- it(
- 'addItemToOrder with no createActiveOrder throws',
- assertThrowsWithMessage(async () => {
- await shopClient.query(addItemToOrderDocument, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- }, 'No active Order could be determined nor created'),
- );
- it('activeOrder with valid input', async () => {
- const { createOrder } = await shopClient.query(createCustomOrderDocument, {
- customerId: customers[1].id,
- });
- expect(createOrder).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- });
- await shopClient.asUserWithCredentials(customers[1].emailAddress, 'test');
- const { activeOrder } = await shopClient.query(activeOrderByTokenDocument, {
- // @ts-expect-error
- input: {
- orderToken: { token: 'token-2' },
- },
- });
- expect(activeOrder).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- });
- });
- it('activeOrder with invalid input', async () => {
- await shopClient.asUserWithCredentials(customers[1].emailAddress, 'test');
- const { activeOrder } = await shopClient.query(activeOrderByTokenDocument, {
- // @ts-expect-error
- input: {
- orderToken: { token: 'invalid' },
- },
- });
- expect(activeOrder).toBeNull();
- });
- it('activeOrder with invalid condition', async () => {
- // wrong customer logged in
- await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');
- const { activeOrder } = await shopClient.query(activeOrderByTokenDocument, {
- // @ts-expect-error
- input: {
- orderToken: { token: 'token-2' },
- },
- });
- expect(activeOrder).toBeNull();
- });
- describe('happy path', () => {
- const activeOrderInput = 'activeOrderInput: { orderToken: { token: "token-2" } }';
- const TEST_COUPON_CODE = 'TESTCOUPON';
- let firstOrderLineId: string;
- beforeAll(async () => {
- await shopClient.asUserWithCredentials(customers[1].emailAddress, 'test');
- const result = await adminClient.query(createPromotionDocument, {
- input: {
- enabled: true,
- couponCode: TEST_COUPON_CODE,
- conditions: [],
- actions: [
- {
- code: orderPercentageDiscount.code,
- arguments: [{ name: 'discount', value: '100' }],
- },
- ],
- translations: [{ languageCode: LanguageCode.en, name: 'Free with test coupon' }],
- },
- });
- });
- it('addItemToOrder', async () => {
- const { addItemToOrder } = await shopClient.query(addItemToOrderWithTokenDocument, {
- productVariantId: 'T_1',
- quantity: 1,
- activeOrderInput: { orderToken: { token: 'token-2' } } as any,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- lines: [
- {
- id: 'T_1',
- productVariant: { id: 'T_1' },
- },
- ],
- });
- if (!addItemToOrder.lines) {
- throw new Error('No lines found');
- }
- firstOrderLineId = addItemToOrder.lines[0].id;
- });
- it('adjustOrderLine', async () => {
- const { adjustOrderLine } = await shopClient.query(adjustOrderLineWithTokenDocument, {
- orderLineId: firstOrderLineId,
- quantity: 2,
- activeOrderInput: { orderToken: { token: 'token-2' } } as any,
- });
- orderResultGuard.assertSuccess(adjustOrderLine);
- expect(adjustOrderLine).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- lines: [
- {
- quantity: 2,
- productVariant: { id: 'T_1' },
- },
- ],
- });
- });
- it('removeOrderLine', async () => {
- const { removeOrderLine } = await shopClient.query(removeOrderLineWithTokenDocument, {
- orderLineId: firstOrderLineId,
- activeOrderInput: { orderToken: { token: 'token-2' } } as any,
- });
- orderResultGuard.assertSuccess(removeOrderLine);
- expect(removeOrderLine).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- lines: [],
- });
- });
- it('removeAllOrderLines', async () => {
- const { addItemToOrder } = await shopClient.query(addItemToOrderWithTokenDocument, {
- productVariantId: 'T_1',
- quantity: 1,
- activeOrderInput: { orderToken: { token: 'token-2' } } as any,
- });
- orderResultGuard.assertSuccess(addItemToOrder);
- expect(addItemToOrder.lines.length).toBe(1);
- const { removeAllOrderLines } = await shopClient.query(removeAllOrderLinesWithTokenDocument, {
- activeOrderInput: { orderToken: { token: 'token-2' } } as any,
- });
- orderResultGuard.assertSuccess(removeAllOrderLines);
- expect(removeAllOrderLines.lines.length).toBe(0);
- });
- it('applyCouponCode', async () => {
- await shopClient.query(addItemToOrderWithTokenDocument, {
- productVariantId: 'T_1',
- quantity: 1,
- activeOrderInput: { orderToken: { token: 'token-2' } } as any,
- });
- const { applyCouponCode } = await shopClient.query(applyCouponCodeWithTokenDocument, {
- couponCode: TEST_COUPON_CODE,
- activeOrderInput: { orderToken: { token: 'token-2' } } as any,
- });
- orderResultGuard.assertSuccess(applyCouponCode);
- expect(applyCouponCode).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- couponCodes: [TEST_COUPON_CODE],
- discounts: [{ description: 'Free with test coupon' }],
- });
- });
- it('removeCouponCode', async () => {
- const { removeCouponCode } = await shopClient.query(removeCouponCodeWithTokenDocument, {
- couponCode: TEST_COUPON_CODE,
- activeOrderInput: { orderToken: { token: 'token-2' } } as any,
- });
- if (!removeCouponCode) {
- throw new Error('No removeCouponCode found');
- }
- orderResultGuard.assertSuccess(removeCouponCode);
- expect(removeCouponCode).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- couponCodes: [],
- discounts: [],
- });
- });
- it('setOrderShippingAddress', async () => {
- const { setOrderShippingAddress } = await shopClient.query(
- setOrderShippingAddressWithTokenDocument,
- {
- input: {
- streetLine1: 'Shipping Street',
- countryCode: 'AT',
- },
- activeOrderInput: { orderToken: { token: 'token-2' } } as any,
- },
- );
- orderResultGuard.assertSuccess(setOrderShippingAddress);
- expect(setOrderShippingAddress).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- shippingAddress: {
- streetLine1: 'Shipping Street',
- country: 'Austria',
- },
- });
- });
- it('setOrderBillingAddress', async () => {
- const { setOrderBillingAddress } = await shopClient.query(
- setOrderBillingAddressWithTokenDocument,
- {
- input: {
- streetLine1: 'Billing Street',
- countryCode: 'AT',
- },
- activeOrderInput: { orderToken: { token: 'token-2' } } as any,
- },
- );
- orderResultGuard.assertSuccess(setOrderBillingAddress);
- expect(setOrderBillingAddress).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- billingAddress: {
- streetLine1: 'Billing Street',
- country: 'Austria',
- },
- });
- });
- it('unsetOrderShippingAddress', async () => {
- const { unsetOrderShippingAddress } = await shopClient.query(
- unsetOrderShippingAddressWithTokenDocument,
- {
- // @ts-expect-error
- activeOrderInput: { orderToken: { token: 'token-2' } },
- },
- );
- orderResultGuard.assertSuccess(unsetOrderShippingAddress);
- expect(unsetOrderShippingAddress).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- shippingAddress: {
- streetLine1: null,
- country: null,
- },
- });
- });
- it('unsetOrderBillingAddress', async () => {
- const { unsetOrderBillingAddress } = await shopClient.query(
- unsetOrderBillingAddressWithTokenDocument,
- {
- // @ts-expect-error
- activeOrderInput: { orderToken: { token: 'token-2' } },
- },
- );
- orderResultGuard.assertSuccess(unsetOrderBillingAddress);
- expect(unsetOrderBillingAddress).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- billingAddress: {
- streetLine1: null,
- country: null,
- },
- });
- });
- it('eligibleShippingMethods', async () => {
- const { eligibleShippingMethods } = await shopClient.query(
- eligibleShippingMethodsWithTokenDocument,
- {
- // @ts-expect-error
- activeOrderInput: { orderToken: { token: 'token-2' } },
- },
- );
- expect(eligibleShippingMethods).toEqual([
- {
- id: 'T_1',
- name: 'Standard Shipping',
- priceWithTax: 500,
- },
- {
- id: 'T_2',
- name: 'Express Shipping',
- priceWithTax: 1000,
- },
- {
- id: 'T_3',
- name: 'Express Shipping (Taxed)',
- priceWithTax: 1200,
- },
- ]);
- });
- it('setOrderShippingMethod', async () => {
- const { setOrderShippingMethod } = await shopClient.query(
- setOrderShippingMethodWithTokenDocument,
- {
- shippingMethodId: ['T_1'],
- // @ts-expect-error
- activeOrderInput: { orderToken: { token: 'token-2' } },
- },
- );
- orderResultGuard.assertSuccess(setOrderShippingMethod);
- expect(setOrderShippingMethod).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- shippingLines: [{ price: 500 }],
- });
- });
- it('setOrderCustomFields', async () => {
- const { setOrderCustomFields } = await shopClient.query(setOrderCustomFieldsWithTokenDocument, {
- input: { customFields: { message: 'foo' } },
- // @ts-expect-error
- activeOrderInput: { orderToken: { token: 'token-2' } },
- });
- orderResultGuard.assertSuccess(setOrderCustomFields);
- expect(setOrderCustomFields).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- customFields: { message: 'foo' },
- });
- });
- it('eligiblePaymentMethods', async () => {
- const { eligiblePaymentMethods } = await shopClient.query(
- eligiblePaymentMethodsWithTokenDocument,
- {
- // @ts-expect-error
- activeOrderInput: { orderToken: { token: 'token-2' } },
- },
- );
- expect(eligiblePaymentMethods).toEqual([
- {
- id: 'T_1',
- name: 'test-payment-method',
- code: 'test-payment-method',
- },
- ]);
- });
- it('nextOrderStates', async () => {
- const { nextOrderStates } = await shopClient.query(nextOrderStatesWithTokenDocument, {
- // @ts-expect-error
- activeOrderInput: { orderToken: { token: 'token-2' } },
- });
- expect(nextOrderStates).toEqual(['ArrangingPayment', 'Cancelled']);
- });
- it('transitionOrderToState', async () => {
- const { transitionOrderToState } = await shopClient.query(
- transitionOrderToStateWithTokenDocument,
- {
- state: 'ArrangingPayment',
- // @ts-expect-error
- activeOrderInput: { orderToken: { token: 'token-2' } },
- },
- );
- if (!transitionOrderToState) {
- throw new Error('No transitionOrderToState found');
- }
- orderResultGuard.assertSuccess(transitionOrderToState);
- expect(transitionOrderToState).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- state: 'ArrangingPayment',
- });
- });
- it('addPaymentToOrder', async () => {
- const { addPaymentToOrder } = await shopClient.query(addPaymentToOrderWithTokenDocument, {
- input: { method: 'test-payment-method', metadata: {} },
- // @ts-expect-error
- activeOrderInput: { orderToken: { token: 'token-2' } },
- });
- if (!addPaymentToOrder) {
- throw new Error('No addPaymentToOrder found');
- }
- orderResultGuard.assertSuccess(addPaymentToOrder);
- expect(addPaymentToOrder).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- payments: [
- {
- state: 'Settled',
- },
- ],
- state: 'PaymentSettled',
- });
- });
- });
- });
- export const activeOrderByTokenDocument = graphql(`
- query ActiveOrderByToken($input: ActiveOrderInput) {
- activeOrder(activeOrderInput: $input) {
- id
- orderToken
- }
- }
- `);
- export const createCustomOrderDocument = graphql(`
- mutation CreateCustomOrder($customerId: ID!) {
- createOrder(customerId: $customerId) {
- id
- orderToken
- }
- }
- `);
- const addItemToOrderWithTokenDocument = graphql(`
- mutation AddItemToOrderWithToken(
- $productVariantId: ID!
- $quantity: Int!
- $activeOrderInput: ActiveOrderInput
- ) {
- addItemToOrder(
- productVariantId: $productVariantId
- quantity: $quantity
- activeOrderInput: $activeOrderInput
- ) {
- ... on Order {
- id
- orderToken
- lines {
- id
- productVariant {
- id
- }
- }
- }
- }
- }
- `);
- const adjustOrderLineWithTokenDocument = graphql(`
- mutation AdjustOrderLineWithToken(
- $orderLineId: ID!
- $quantity: Int!
- $activeOrderInput: ActiveOrderInput
- ) {
- adjustOrderLine(orderLineId: $orderLineId, quantity: $quantity, activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- lines {
- quantity
- productVariant {
- id
- }
- }
- }
- }
- }
- `);
- const removeOrderLineWithTokenDocument = graphql(`
- mutation RemoveOrderLineWithToken($orderLineId: ID!, $activeOrderInput: ActiveOrderInput) {
- removeOrderLine(orderLineId: $orderLineId, activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- lines {
- id
- }
- }
- }
- }
- `);
- const removeAllOrderLinesWithTokenDocument = graphql(`
- mutation RemoveAllOrderLinesWithToken($activeOrderInput: ActiveOrderInput) {
- removeAllOrderLines(activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- lines {
- id
- }
- }
- }
- }
- `);
- const applyCouponCodeWithTokenDocument = graphql(`
- mutation ApplyCouponCodeWithToken($couponCode: String!, $activeOrderInput: ActiveOrderInput) {
- applyCouponCode(couponCode: $couponCode, activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- couponCodes
- discounts {
- description
- }
- }
- }
- }
- `);
- const removeCouponCodeWithTokenDocument = graphql(`
- mutation RemoveCouponCodeWithToken($couponCode: String!, $activeOrderInput: ActiveOrderInput) {
- removeCouponCode(couponCode: $couponCode, activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- couponCodes
- discounts {
- description
- }
- }
- }
- }
- `);
- const setOrderShippingAddressWithTokenDocument = graphql(`
- mutation SetOrderShippingAddressWithToken(
- $input: CreateAddressInput!
- $activeOrderInput: ActiveOrderInput
- ) {
- setOrderShippingAddress(input: $input, activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- shippingAddress {
- streetLine1
- country
- }
- }
- }
- }
- `);
- const setOrderBillingAddressWithTokenDocument = graphql(`
- mutation SetOrderBillingAddressWithToken(
- $input: CreateAddressInput!
- $activeOrderInput: ActiveOrderInput
- ) {
- setOrderBillingAddress(input: $input, activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- billingAddress {
- streetLine1
- country
- }
- }
- }
- }
- `);
- const unsetOrderShippingAddressWithTokenDocument = graphql(`
- mutation UnsetOrderShippingAddressWithToken($activeOrderInput: ActiveOrderInput) {
- unsetOrderShippingAddress(activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- shippingAddress {
- streetLine1
- country
- }
- }
- }
- }
- `);
- const unsetOrderBillingAddressWithTokenDocument = graphql(`
- mutation UnsetOrderBillingAddressWithToken($activeOrderInput: ActiveOrderInput) {
- unsetOrderBillingAddress(activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- billingAddress {
- streetLine1
- country
- }
- }
- }
- }
- `);
- const eligibleShippingMethodsWithTokenDocument = graphql(`
- query EligibleShippingMethodsWithToken($activeOrderInput: ActiveOrderInput) {
- eligibleShippingMethods(activeOrderInput: $activeOrderInput) {
- id
- name
- priceWithTax
- }
- }
- `);
- const setOrderShippingMethodWithTokenDocument = graphql(`
- mutation SetOrderShippingMethodWithToken($shippingMethodId: [ID!]!, $activeOrderInput: ActiveOrderInput) {
- setOrderShippingMethod(shippingMethodId: $shippingMethodId, activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- shippingLines {
- price
- }
- }
- }
- }
- `);
- const setOrderCustomFieldsWithTokenDocument = graphql(`
- mutation SetOrderCustomFieldsWithToken($input: UpdateOrderInput!, $activeOrderInput: ActiveOrderInput) {
- setOrderCustomFields(input: $input, activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- customFields {
- message
- }
- }
- }
- }
- `);
- const eligiblePaymentMethodsWithTokenDocument = graphql(`
- query EligiblePaymentMethodsWithToken($activeOrderInput: ActiveOrderInput) {
- eligiblePaymentMethods(activeOrderInput: $activeOrderInput) {
- id
- name
- code
- }
- }
- `);
- const nextOrderStatesWithTokenDocument = graphql(`
- query NextOrderStatesWithToken($activeOrderInput: ActiveOrderInput) {
- nextOrderStates(activeOrderInput: $activeOrderInput)
- }
- `);
- const transitionOrderToStateWithTokenDocument = graphql(`
- mutation TransitionOrderToStateWithToken($state: String!, $activeOrderInput: ActiveOrderInput) {
- transitionOrderToState(state: $state, activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- state
- }
- }
- }
- `);
- const addPaymentToOrderWithTokenDocument = graphql(`
- mutation AddPaymentToOrderWithToken($input: PaymentInput!, $activeOrderInput: ActiveOrderInput) {
- addPaymentToOrder(input: $input, activeOrderInput: $activeOrderInput) {
- ... on Order {
- id
- orderToken
- state
- payments {
- state
- }
- }
- }
- }
- `);
|