| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580 |
- import { LanguageCode } from '@vendure/common/lib/generated-types';
- import { mergeConfig, orderPercentageDiscount } from '@vendure/core';
- import { createTestEnvironment } from '@vendure/testing';
- import gql from 'graphql-tag';
- import path from 'path';
- import { afterAll, beforeAll, describe, expect, it } from 'vitest';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
- import { testSuccessfulPaymentMethod } from './fixtures/test-payment-methods';
- import { TokenActiveOrderPlugin } from './fixtures/test-plugins/token-active-order-plugin';
- import {
- CreatePromotionMutation,
- CreatePromotionMutationVariables,
- GetCustomerListQuery,
- } from './graphql/generated-e2e-admin-types';
- import {
- AddItemToOrderMutation,
- AddItemToOrderMutationVariables,
- GetActiveOrderQuery,
- } from './graphql/generated-e2e-shop-types';
- import { CREATE_PROMOTION, GET_CUSTOMER_LIST } from './graphql/shared-definitions';
- import { ADD_ITEM_TO_ORDER, GET_ACTIVE_ORDER } 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,
- },
- ],
- },
- }),
- );
- let customers: GetCustomerListQuery['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<GetCustomerListQuery>(GET_CUSTOMER_LIST);
- 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<GetActiveOrderQuery>(GET_ACTIVE_ORDER);
- expect(activeOrder).toBeNull();
- });
- it(
- 'addItemToOrder with no createActiveOrder throws',
- assertThrowsWithMessage(async () => {
- await shopClient.query<AddItemToOrderMutation, AddItemToOrderMutationVariables>(
- ADD_ITEM_TO_ORDER,
- {
- productVariantId: 'T_1',
- quantity: 1,
- },
- );
- }, 'No active Order could be determined nor created'),
- );
- it('activeOrder with valid input', async () => {
- const { createOrder } = await shopClient.query(gql`
- mutation CreateCustomOrder {
- createOrder(customerId: "${customers[1].id}") {
- id
- orderToken
- }
- }
- `);
- expect(createOrder).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- });
- await shopClient.asUserWithCredentials(customers[1].emailAddress, 'test');
- const { activeOrder } = await shopClient.query(ACTIVE_ORDER_BY_TOKEN, {
- 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(ACTIVE_ORDER_BY_TOKEN, {
- 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(ACTIVE_ORDER_BY_TOKEN, {
- 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<CreatePromotionMutation, CreatePromotionMutationVariables>(
- CREATE_PROMOTION,
- {
- 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(gql`
- mutation {
- addItemToOrder(productVariantId: "T_1", quantity: 1, ${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- lines {
- id
- productVariant { id }
- }
- }
- }
- }
- `);
- expect(addItemToOrder).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- lines: [
- {
- id: 'T_1',
- productVariant: { id: 'T_1' },
- },
- ],
- });
- firstOrderLineId = addItemToOrder.lines[0].id;
- });
- it('adjustOrderLine', async () => {
- const { adjustOrderLine } = await shopClient.query(gql`
- mutation {
- adjustOrderLine(orderLineId: "${firstOrderLineId}", quantity: 2, ${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- lines {
- quantity
- productVariant { id }
- }
- }
- }
- }
- `);
- expect(adjustOrderLine).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- lines: [
- {
- quantity: 2,
- productVariant: { id: 'T_1' },
- },
- ],
- });
- });
- it('removeOrderLine', async () => {
- const { removeOrderLine } = await shopClient.query(gql`
- mutation {
- removeOrderLine(orderLineId: "${firstOrderLineId}", ${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- lines {
- id
- }
- }
- }
- }
- `);
- expect(removeOrderLine).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- lines: [],
- });
- });
- it('removeAllOrderLines', async () => {
- const { addItemToOrder } = await shopClient.query(gql`
- mutation {
- addItemToOrder(productVariantId: "T_1", quantity: 1, ${activeOrderInput}) {
- ...on Order { lines { id } }
- }
- }
- `);
- expect(addItemToOrder.lines.length).toBe(1);
- const { removeAllOrderLines } = await shopClient.query(gql`
- mutation {
- removeAllOrderLines(${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- lines { id }
- }
- }
- }
- `);
- expect(removeAllOrderLines.lines.length).toBe(0);
- });
- it('applyCouponCode', async () => {
- await shopClient.query(gql`
- mutation {
- addItemToOrder(productVariantId: "T_1", quantity: 1, ${activeOrderInput}) {
- ...on Order { lines { id } }
- }
- }
- `);
- const { applyCouponCode } = await shopClient.query(gql`
- mutation {
- applyCouponCode(couponCode: "${TEST_COUPON_CODE}", ${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- couponCodes
- discounts {
- description
- }
- }
- }
- }
- `);
- 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(gql`
- mutation {
- removeCouponCode(couponCode: "${TEST_COUPON_CODE}", ${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- couponCodes
- discounts {
- description
- }
- }
- }
- }
- `);
- expect(removeCouponCode).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- couponCodes: [],
- discounts: [],
- });
- });
- it('setOrderShippingAddress', async () => {
- const { setOrderShippingAddress } = await shopClient.query(gql`
- mutation {
- setOrderShippingAddress(input: {
- streetLine1: "Shipping Street"
- countryCode: "AT"
- }, ${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- shippingAddress {
- streetLine1
- country
- }
- }
- }
- }
- `);
- expect(setOrderShippingAddress).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- shippingAddress: {
- streetLine1: 'Shipping Street',
- country: 'Austria',
- },
- });
- });
- it('setOrderBillingAddress', async () => {
- const { setOrderBillingAddress } = await shopClient.query(gql`
- mutation {
- setOrderBillingAddress(input: {
- streetLine1: "Billing Street"
- countryCode: "AT"
- }, ${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- billingAddress {
- streetLine1
- country
- }
- }
- }
- }
- `);
- expect(setOrderBillingAddress).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- billingAddress: {
- streetLine1: 'Billing Street',
- country: 'Austria',
- },
- });
- });
- it('unsetOrderShippingAddress', async () => {
- const { unsetOrderShippingAddress } = await shopClient.query(gql`
- mutation {
- unsetOrderShippingAddress(${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- shippingAddress {
- streetLine1
- country
- }
- }
- }
- }
- `);
- expect(unsetOrderShippingAddress).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- shippingAddress: {
- streetLine1: null,
- country: null,
- },
- });
- });
- it('unsetOrderBillingAddress', async () => {
- const { unsetOrderBillingAddress } = await shopClient.query(gql`
- mutation {
- unsetOrderBillingAddress(${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- billingAddress {
- streetLine1
- country
- }
- }
- }
- }
- `);
- expect(unsetOrderBillingAddress).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- billingAddress: {
- streetLine1: null,
- country: null,
- },
- });
- });
- it('eligibleShippingMethods', async () => {
- const { eligibleShippingMethods } = await shopClient.query(gql`
- query {
- eligibleShippingMethods(${activeOrderInput}) {
- id
- name
- priceWithTax
- }
- }
- `);
- 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(gql`
- mutation {
- setOrderShippingMethod(shippingMethodId: "T_1", ${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- shippingLines {
- price
- }
- }
- }
- }
- `);
- expect(setOrderShippingMethod).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- shippingLines: [{ price: 500 }],
- });
- });
- it('setOrderCustomFields', async () => {
- const { setOrderCustomFields } = await shopClient.query(gql`
- mutation {
- setOrderCustomFields(input: { customFields: { message: "foo" } }, ${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- customFields { message }
- }
- }
- }
- `);
- expect(setOrderCustomFields).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- customFields: { message: 'foo' },
- });
- });
- it('eligiblePaymentMethods', async () => {
- const { eligiblePaymentMethods } = await shopClient.query(gql`
- query {
- eligiblePaymentMethods(${activeOrderInput}) {
- id
- name
- code
- }
- }
- `);
- expect(eligiblePaymentMethods).toEqual([
- {
- id: 'T_1',
- name: 'test-payment-method',
- code: 'test-payment-method',
- },
- ]);
- });
- it('nextOrderStates', async () => {
- const { nextOrderStates } = await shopClient.query(gql`
- query {
- nextOrderStates(${activeOrderInput})
- }
- `);
- expect(nextOrderStates).toEqual(['ArrangingPayment', 'Cancelled']);
- });
- it('transitionOrderToState', async () => {
- const { transitionOrderToState } = await shopClient.query(gql`
- mutation {
- transitionOrderToState(state: "ArrangingPayment", ${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- state
- }
- }
- }
- `);
- expect(transitionOrderToState).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- state: 'ArrangingPayment',
- });
- });
- it('addPaymentToOrder', async () => {
- const { addPaymentToOrder } = await shopClient.query(gql`
- mutation {
- addPaymentToOrder(input: { method: "test-payment-method", metadata: {}}, ${activeOrderInput}) {
- ...on Order {
- id
- orderToken
- state
- payments {
- state
- }
- }
- }
- }
- `);
- expect(addPaymentToOrder).toEqual({
- id: 'T_1',
- orderToken: 'token-2',
- payments: [
- {
- state: 'Settled',
- },
- ],
- state: 'PaymentSettled',
- });
- });
- });
- });
- export const ACTIVE_ORDER_BY_TOKEN = gql`
- query ActiveOrderByToken($input: ActiveOrderInput) {
- activeOrder(activeOrderInput: $input) {
- id
- orderToken
- }
- }
- `;
|