Просмотр исходного кода

test(payments-plugin): Fix failing e2e tests

Relates to #2075
Michael Bromley 2 лет назад
Родитель
Сommit
b0f400fddc

+ 100 - 60
packages/payments-plugin/e2e/mollie-payment.e2e-spec.ts

@@ -1,11 +1,23 @@
 import { OrderStatus } from '@mollie/api-client';
-import { ChannelService, DefaultLogger, LogLevel, mergeConfig, OrderService, RequestContext } from '@vendure/core';
+import {
+    ChannelService,
+    DefaultLogger,
+    LogLevel,
+    mergeConfig,
+    OrderService,
+    RequestContext,
+} from '@vendure/core';
 import {
     SettlePaymentMutation,
-    SettlePaymentMutationVariables
+    SettlePaymentMutationVariables,
 } from '@vendure/core/e2e/graphql/generated-e2e-admin-types';
 import { SETTLE_PAYMENT } from '@vendure/core/e2e/graphql/shared-definitions';
-import { createTestEnvironment, E2E_DEFAULT_CHANNEL_TOKEN, SimpleGraphQLClient, TestServer } from '@vendure/testing';
+import {
+    createTestEnvironment,
+    E2E_DEFAULT_CHANNEL_TOKEN,
+    SimpleGraphQLClient,
+    TestServer,
+} from '@vendure/testing';
 import nock from 'nock';
 import fetch from 'node-fetch';
 import path from 'path';
@@ -20,7 +32,13 @@ import { CREATE_PAYMENT_METHOD, GET_CUSTOMER_LIST, GET_ORDER_PAYMENTS } from './
 import { CreatePaymentMethod, GetCustomerList, GetCustomerListQuery } from './graphql/generated-admin-types';
 import { AddItemToOrder, GetOrderByCode, TestOrderFragmentFragment } from './graphql/generated-shop-types';
 import { ADD_ITEM_TO_ORDER, GET_ORDER_BY_CODE } from './graphql/shop-queries';
-import { addManualPayment, CREATE_MOLLIE_PAYMENT_INTENT, GET_MOLLIE_PAYMENT_METHODS, refundOrderLine, setShipping } from './payment-helpers';
+import {
+    addManualPayment,
+    CREATE_MOLLIE_PAYMENT_INTENT,
+    GET_MOLLIE_PAYMENT_METHODS,
+    refundOrderLine,
+    setShipping,
+} from './payment-helpers';
 
 describe('Mollie payments', () => {
     const mockData = {
@@ -37,11 +55,13 @@ describe('Mollie payments', () => {
             },
             lines: [],
             _embedded: {
-                payments: [{
-                    id: 'tr_mockPayment',
-                    status: 'paid',
-                    resource: 'payment',
-                }],
+                payments: [
+                    {
+                        id: 'tr_mockPayment',
+                        status: 'paid',
+                        resource: 'payment',
+                    },
+                ],
             },
             resource: 'order',
             mode: 'test',
@@ -79,7 +99,8 @@ describe('Mollie payments', () => {
                                 type: 'application/hal+json',
                             },
                         },
-                    }],
+                    },
+                ],
             },
             _links: {
                 self: {
@@ -100,11 +121,10 @@ describe('Mollie payments', () => {
     let customers: GetCustomerListQuery['customers']['items'];
     let order: TestOrderFragmentFragment;
     let serverPort: number;
+    const SURCHARGE_AMOUNT = -20000;
     beforeAll(async () => {
         const devConfig = mergeConfig(testConfig(), {
             plugins: [MolliePlugin.init({ vendureHost: mockData.host })],
-            // Uncomment next line to debug e2e
-            logger: new DefaultLogger({level: LogLevel.Verbose})
         });
         const env = createTestEnvironment(devConfig);
         serverPort = devConfig.apiOptions.port;
@@ -138,10 +158,13 @@ describe('Mollie payments', () => {
 
     it('Should prepare an order', async () => {
         await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');
-        const { addItemToOrder } = await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
-            productVariantId: 'T_5',
-            quantity: 10,
-        });
+        const { addItemToOrder } = await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(
+            ADD_ITEM_TO_ORDER,
+            {
+                productVariantId: 'T_5',
+                quantity: 10,
+            },
+        );
         order = addItemToOrder as TestOrderFragmentFragment;
         // Add surcharge
         const ctx = new RequestContext({
@@ -152,29 +175,31 @@ describe('Mollie payments', () => {
         });
         await server.app.get(OrderService).addSurchargeToOrder(ctx, 1, {
             description: 'Negative test surcharge',
-            listPrice: -20000,
+            listPrice: SURCHARGE_AMOUNT,
         });
         expect(order.code).toBeDefined();
     });
 
     it('Should add a Mollie paymentMethod', async () => {
-        const { createPaymentMethod } = await adminClient.query<CreatePaymentMethod.Mutation,
-            CreatePaymentMethod.Variables>(CREATE_PAYMENT_METHOD, {
-                input: {
-                    code: mockData.methodCode,
-                    name: 'Mollie payment test',
-                    description: 'This is a Mollie test payment method',
-                    enabled: true,
-                    handler: {
-                        code: molliePaymentHandler.code,
-                        arguments: [
-                            { name: 'redirectUrl', value: mockData.redirectUrl },
-                            { name: 'apiKey', value: mockData.apiKey },
-                            { name: 'autoCapture', value: 'false' },
-                        ],
-                    },
+        const { createPaymentMethod } = await adminClient.query<
+            CreatePaymentMethod.Mutation,
+            CreatePaymentMethod.Variables
+        >(CREATE_PAYMENT_METHOD, {
+            input: {
+                code: mockData.methodCode,
+                name: 'Mollie payment test',
+                description: 'This is a Mollie test payment method',
+                enabled: true,
+                handler: {
+                    code: molliePaymentHandler.code,
+                    arguments: [
+                        { name: 'redirectUrl', value: mockData.redirectUrl },
+                        { name: 'apiKey', value: mockData.apiKey },
+                        { name: 'autoCapture', value: 'false' },
+                    ],
                 },
-            });
+            },
+        });
         expect(createPaymentMethod.code).toBe(mockData.methodCode);
     });
 
@@ -207,7 +232,7 @@ describe('Mollie payments', () => {
                 trackInventory: 'TRUE',
                 outOfStockThreshold: 0,
                 stockOnHand: 1,
-            }
+            },
         });
         expect(updateProductVariants[0].stockOnHand).toBe(1);
         const { createMolliePaymentIntent: result } = await shopClient.query(CREATE_MOLLIE_PAYMENT_INTENT, {
@@ -221,7 +246,7 @@ describe('Mollie payments', () => {
             input: {
                 id: 'T_5',
                 trackInventory: 'FALSE',
-            }
+            },
         }));
         expect(updateProductVariants[0].trackInventory).toBe('FALSE');
     });
@@ -239,7 +264,9 @@ describe('Mollie payments', () => {
                 paymentMethodCode: mockData.methodCode,
             },
         });
-        expect(createMolliePaymentIntent).toEqual({ url: 'https://www.mollie.com/payscreen/select-method/mock-payment' });
+        expect(createMolliePaymentIntent).toEqual({
+            url: 'https://www.mollie.com/payscreen/select-method/mock-payment',
+        });
         expect(mollieRequest?.orderNumber).toEqual(order.code);
         expect(mollieRequest?.redirectUrl).toEqual(`${mockData.redirectUrl}/${order.code}`);
         expect(mollieRequest?.webhookUrl).toEqual(
@@ -257,9 +284,7 @@ describe('Mollie payments', () => {
     });
 
     it('Should get payment url with Mollie method', async () => {
-        nock('https://api.mollie.com/')
-            .post('/v2/orders')
-            .reply(200, mockData.mollieOrderResponse);
+        nock('https://api.mollie.com/').post('/v2/orders').reply(200, mockData.mollieOrderResponse);
         await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');
         await setShipping(shopClient);
         const { createMolliePaymentIntent } = await shopClient.query(CREATE_MOLLIE_PAYMENT_INTENT, {
@@ -268,7 +293,9 @@ describe('Mollie payments', () => {
                 molliePaymentMethodCode: 'ideal',
             },
         });
-        expect(createMolliePaymentIntent).toEqual({ url: 'https://www.mollie.com/payscreen/select-method/mock-payment' });
+        expect(createMolliePaymentIntent).toEqual({
+            url: 'https://www.mollie.com/payscreen/select-method/mock-payment',
+        });
     });
 
     it('Should get payment url with deducted amount if a payment is already made', async () => {
@@ -319,7 +346,9 @@ describe('Mollie payments', () => {
     });
 
     it('Should have Mollie metadata on payment', async () => {
-        const { order: { payments } } = await adminClient.query(GET_ORDER_PAYMENTS, { id: order.id });
+        const {
+            order: { payments },
+        } = await adminClient.query(GET_ORDER_PAYMENTS, { id: order.id });
         const metadata = payments[1].metadata;
         expect(metadata.mode).toBe(mockData.mollieOrderResponse.mode);
         expect(metadata.method).toBe(mockData.mollieOrderResponse.method);
@@ -335,8 +364,14 @@ describe('Mollie payments', () => {
         nock('https://api.mollie.com/')
             .post('/v2/payments/tr_mockPayment/refunds')
             .reply(200, { status: 'failed', resource: 'payment' });
-        // tslint:disable-next-line:no-non-null-assertion
-        const refund = await refundOrderLine(adminClient, order.lines[0].id, 1, order!.payments[1].id);
+        const refund = await refundOrderLine(
+            adminClient,
+            order.lines[0].id,
+            1,
+            // tslint:disable-next-line:no-non-null-assertion
+            order!.payments[1].id,
+            SURCHARGE_AMOUNT,
+        );
         expect(refund.state).toBe('Failed');
     });
 
@@ -351,16 +386,20 @@ describe('Mollie payments', () => {
                 return true;
             })
             .reply(200, { status: 'pending', resource: 'payment' });
-        const refund = await refundOrderLine(adminClient, order.lines[0].id, 10, order.payments[1].id);
+        const refund = await refundOrderLine(
+            adminClient,
+            order.lines[0].id,
+            10,
+            order.payments[1].id,
+            SURCHARGE_AMOUNT,
+        );
         expect(mollieRequest?.amount.value).toBe('909.90'); // Only refund mollie amount, not the gift card
         expect(refund.total).toBe(90990);
         expect(refund.state).toBe('Settled');
     });
 
     it('Should get available paymentMethods', async () => {
-        nock('https://api.mollie.com/')
-            .get('/v2/methods')
-            .reply(200, mockData.molliePaymentMethodsResponse);
+        nock('https://api.mollie.com/').get('/v2/methods').reply(200, mockData.molliePaymentMethodsResponse);
         await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');
         const { molliePaymentMethods } = await shopClient.query(GET_MOLLIE_PAYMENT_METHODS, {
             input: {
@@ -376,10 +415,13 @@ describe('Mollie payments', () => {
 
     it('Should prepare a new order', async () => {
         await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');
-        const { addItemToOrder } = await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
-            productVariantId: 'T_1',
-            quantity: 2,
-        });
+        const { addItemToOrder } = await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(
+            ADD_ITEM_TO_ORDER,
+            {
+                productVariantId: 'T_1',
+                quantity: 2,
+            },
+        );
         order = addItemToOrder as TestOrderFragmentFragment;
         await setShipping(shopClient);
         expect(order.code).toBeDefined();
@@ -426,13 +468,13 @@ describe('Mollie payments', () => {
                 return true;
             })
             .reply(200, { resource: 'shipment', lines: [] });
-        const { settlePayment } = await adminClient.query<SettlePaymentMutation, SettlePaymentMutationVariables>(
-            SETTLE_PAYMENT,
-            {
-                // tslint:disable-next-line:no-non-null-assertion
-                id: order.payments![0].id,
-            },
-        );
+        const { settlePayment } = await adminClient.query<
+            SettlePaymentMutation,
+            SettlePaymentMutationVariables
+        >(SETTLE_PAYMENT, {
+            // tslint:disable-next-line:no-non-null-assertion
+            id: order.payments![0].id,
+        });
         const { orderByCode } = await shopClient.query<GetOrderByCode.Query, GetOrderByCode.Variables>(
             GET_ORDER_BY_CODE,
             {
@@ -444,6 +486,4 @@ describe('Mollie payments', () => {
         expect(createShipmentBody).toBeDefined();
         expect(order.state).toBe('PaymentSettled');
     });
-
-
 });

+ 8 - 6
packages/payments-plugin/e2e/payment-helpers.ts

@@ -18,7 +18,6 @@ import {
     TRANSITION_TO_STATE,
 } from './graphql/shop-queries';
 
-
 export async function setShipping(shopClient: SimpleGraphQLClient): Promise<void> {
     await shopClient.query(SET_SHIPPING_ADDRESS, {
         input: {
@@ -52,6 +51,7 @@ export async function refundOrderLine(
     orderLineId: string,
     quantity: number,
     paymentId: string,
+    adjustment: number,
 ): Promise<RefundFragment> {
     const { refundOrder } = await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(
         REFUND_ORDER,
@@ -59,7 +59,7 @@ export async function refundOrderLine(
             input: {
                 lines: [{ orderLineId, quantity }],
                 shipping: 0,
-                adjustment: 0,
+                adjustment,
                 paymentId,
             },
         },
@@ -83,8 +83,8 @@ export async function addManualPayment(server: TestServer, orderId: ID, amount:
         // tslint:disable-next-line:no-non-null-assertion
         orderId: order!.id,
         metadata: {
-            bogus: 'test'
-        }
+            bogus: 'test',
+        },
     });
 }
 
@@ -99,7 +99,8 @@ export const CREATE_MOLLIE_PAYMENT_INTENT = gql`
                 message
             }
         }
-    }`;
+    }
+`;
 
 export const GET_MOLLIE_PAYMENT_METHODS = gql`
     query molliePaymentMethods($input: MolliePaymentMethodsInput!) {
@@ -121,4 +122,5 @@ export const GET_MOLLIE_PAYMENT_METHODS = gql`
                 svg
             }
         }
-    }`;
+    }
+`;