Skip to content

Commit

Permalink
Add test case for helper.converter
Browse files Browse the repository at this point in the history
  • Loading branch information
leungkinghin-ct committed Mar 19, 2024
1 parent cef9f84 commit 2e49dcf
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 41 deletions.
1 change: 0 additions & 1 deletion processor/src/services/adyen-payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ export class AdyenPaymentService extends AbstractPaymentService {
paymentId: ctPayment.id,
});
}

const data = this.createPaymentConverter.convertRequest({
data: opts.data,
cart: ctCart,
Expand Down
14 changes: 7 additions & 7 deletions processor/test/services/adyen-payment.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { DefaultPaymentService } from '@commercetools/connect-payments-sdk/dist/
import { DefaultCartService } from '@commercetools/connect-payments-sdk/dist/commercetools/services/ct-cart.service';
import {
mockGetPaymentResult,
mockGetCartResult,
mockGetPaymentAmount,
mockUpdatePaymentResult,
mockAdyenCreateSessionResponse,
Expand All @@ -15,13 +14,14 @@ import {
mockAdyenCreatePaymentResponse,
mockAdyenRefundPaymentResponse,
} from '../utils/mock-payment-data';

import { mockGetCartResult } from '../utils/mock-cart-data';
import * as Config from '../../src/config/config';
import { AbstractPaymentService } from '../../src/services/abstract-payment.service';
import { AdyenPaymentService } from '../../src/services/adyen-payment.service';
import * as StatusHandler from '@commercetools/connect-payments-sdk/dist/api/handlers/status.handler';
import { PaymentsApi } from '@adyen/api-library/lib/src/services/checkout/paymentsApi';
import { ModificationsApi } from '@adyen/api-library/lib/src/services/checkout/modificationsApi';
import { ApplePayDetails } from '@adyen/api-library/lib/src/typings/checkout/applePayDetails';

import {
CommercetoolsCartService,
Expand All @@ -32,13 +32,13 @@ import { SupportedPaymentComponentsSchemaDTO } from '../../src/dtos/operations/p
import {
CreatePaymentRequestDTO,
CreateSessionRequestDTO,
PaymentMethodsRequestDTO
PaymentMethodsRequestDTO,
} from '../../src/dtos/adyen-payment.dto';

import * as FastifyContext from '../../src/libs/fastify/context/context';
import { PaymentResponse } from '@adyen/api-library/lib/src/typings/checkout/paymentResponse';
import { CreateCheckoutSessionResponse } from '@adyen/api-library/lib/src/typings/checkout/models';
import { getCtSessionIdFromContext } from '../../src/libs/fastify/context/context';
import { KlarnaDetails } from '@adyen/api-library/lib/src/typings/checkout/klarnaDetails';

interface FlexibleConfig {
[key: string]: string; // Adjust the type according to your config values
Expand Down Expand Up @@ -195,12 +195,12 @@ describe('adyen-payment.service', () => {
});

test('createPayment', async () => {
const applePayDetails: ApplePayDetails = {
applePayToken: '123456789',
const klarnaDetails: KlarnaDetails = {
type: KlarnaDetails.TypeEnum.KlarnaAccount,
};
const createPaymentOpts: { data: CreatePaymentRequestDTO } = {
data: {
paymentMethod: applePayDetails,
paymentMethod: klarnaDetails,
},
};

Expand Down
69 changes: 69 additions & 0 deletions processor/test/services/converters/helper.converter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { describe, test, expect, afterEach, jest, beforeEach } from '@jest/globals';
import {
convertPaymentMethodFromAdyenFormat,
convertPaymentMethodToAdyenFormat,
populateCartAddress,
} from '../../../src/services/converters/helper.converter';
import { Address as AdyenAddress } from '@adyen/api-library/lib/src/typings/checkout/address';
import { Address } from '@commercetools/platform-sdk';
describe('helper.converter', () => {
beforeEach(() => {
jest.setTimeout(10000);
jest.resetAllMocks();
});

afterEach(() => {
jest.restoreAllMocks();
});

test('convertPaymentMethodFromAdyenFormat', async () => {
const paymentMethod1 = 'scheme';
const paymentMethod2 = 'klarna';
const result1: string = convertPaymentMethodFromAdyenFormat(paymentMethod1);
const result2: string = convertPaymentMethodFromAdyenFormat(paymentMethod2);
expect(result1).toStrictEqual('card');
expect(result2).toStrictEqual('klarna');
});

test('convertPaymentMethodToAdyenFormat', async () => {
const paymentMethod1 = 'card';
const paymentMethod2 = 'klarna';
const result1: string = convertPaymentMethodToAdyenFormat(paymentMethod1);
const result2: string = convertPaymentMethodToAdyenFormat(paymentMethod2);
expect(result1).toStrictEqual('scheme');
expect(result2).toStrictEqual('klarna');
});

test('populateCartAddress', async () => {
const address1: Address = {
country: 'Germany',
city: 'Munich',
streetName: 'Adam-Lehmann-Straße',
streetNumber: '44',
state: 'Bavaria',
postalCode: '80797',
};

const result1: AdyenAddress = populateCartAddress(address1);

expect(result1?.country).toStrictEqual('Germany');
expect(result1?.city).toStrictEqual('Munich');
expect(result1?.street).toStrictEqual('Adam-Lehmann-Straße');
expect(result1?.stateOrProvince).toStrictEqual('Bavaria');
expect(result1?.houseNumberOrName).toStrictEqual('44');
expect(result1?.postalCode).toStrictEqual('80797');

const address2: Address = {
country: '',
};

const result2: AdyenAddress = populateCartAddress(address2);

expect(result2?.country).toStrictEqual('');
expect(result2?.city).toStrictEqual('');
expect(result2?.street).toStrictEqual('');
expect(result2?.stateOrProvince).toStrictEqual(undefined);
expect(result2?.houseNumberOrName).toStrictEqual('');
expect(result2?.postalCode).toStrictEqual('');
});
});
119 changes: 119 additions & 0 deletions processor/test/utils/mock-cart-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { LineItem, CustomLineItem, ShippingInfo } from '@commercetools/platform-sdk';
import { randomUUID } from 'crypto';
import { Cart } from '@commercetools/platform-sdk';

export const mockGetCartResult = () => {
const cartId = randomUUID();
const mockGetCartResult: Cart = {
id: cartId,
version: 1,
lineItems: [lineItem],
customLineItems: [customLineItem],
totalPrice: {
type: 'centPrecision',
currencyCode: 'USD',
centAmount: 150000,
fractionDigits: 2,
},
cartState: 'Ordered',
origin: 'Customer',
taxMode: 'ExternalAmount',
taxRoundingMode: 'HalfEven',
taxCalculationMode: 'LineItemLevel',
shipping: [],
discountCodes: [],
directDiscounts: [],
refusedGifts: [],
itemShippingAddresses: [],
inventoryMode: 'ReserveOnOrder',
shippingMode: 'Single',
shippingInfo: shippingInfo,
createdAt: '2024-01-01T00:00:00Z',
lastModifiedAt: '2024-01-01T00:00:00Z',
};
return mockGetCartResult;
};

const lineItem: LineItem = {
id: 'lineitem-id-1',
productId: 'product-id-1',
name: {
en: 'lineitem-name-1',
},
productType: {
id: 'product-type-reference-1',
typeId: 'product-type',
},
price: {
id: 'price-id-1',
value: {
type: 'centPrecision',
currencyCode: 'USD',
centAmount: 150000,
fractionDigits: 2,
},
},
quantity: 1,
totalPrice: {
type: 'centPrecision',
currencyCode: 'USD',
centAmount: 150000,
fractionDigits: 2,
},
discountedPricePerQuantity: [],
taxedPricePortions: [],
state: [],
perMethodTaxRate: [],
priceMode: 'Platform',
lineItemMode: 'Standard',
variant: {
id: 1,
sku: 'variant-sku-1',
},
};

const customLineItem: CustomLineItem = {
id: 'customLineItem-id-1',
name: {
en: 'customLineItem-name-1',
},
slug: '',
money: {
type: 'centPrecision',
currencyCode: 'USD',
centAmount: 150000,
fractionDigits: 2,
},
quantity: 1,
totalPrice: {
type: 'centPrecision',
currencyCode: 'USD',
centAmount: 150000,
fractionDigits: 2,
},
discountedPricePerQuantity: [],
taxedPricePortions: [],
state: [],
perMethodTaxRate: [],
priceMode: 'Platform',
};

const shippingInfo: ShippingInfo = {
shippingMethodName: 'shippingMethodName1',
price: {
type: 'centPrecision',
currencyCode: 'USD',
centAmount: 150000,
fractionDigits: 2,
},
shippingRate: {
price: {
type: 'centPrecision',
currencyCode: 'USD',
centAmount: 1000,
fractionDigits: 2,
},
tiers: [],
},
shippingMethodState: 'MatchesCart',
};
33 changes: 0 additions & 33 deletions processor/test/utils/mock-payment-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import { PaymentCaptureResponse } from '@adyen/api-library/lib/src/typings/check
import { PaymentRefundResponse } from '@adyen/api-library/lib/src/typings/checkout/paymentRefundResponse';
import { CreateCheckoutSessionResponse } from '@adyen/api-library/lib/src/typings/checkout/createCheckoutSessionResponse';

import { Cart } from '@commercetools/platform-sdk';
import { PaymentAmount } from '@commercetools/connect-payments-sdk/dist/commercetools/types/payment.type';
import { PaymentResponse } from '@adyen/api-library/lib/src/typings/checkout/models';
import { randomUUID } from 'crypto';

export const mockGetPaymentResult: Payment = {
id: '123456',
Expand Down Expand Up @@ -101,37 +99,6 @@ export const mockAdyenRefundPaymentResponse: PaymentRefundResponse = {
},
};

export const mockGetCartResult = () => {
const cartId = randomUUID();
const mockGetCartResult: Cart = {
id: cartId,
version: 1,
lineItems: [],
customLineItems: [],
totalPrice: {
type: 'centPrecision',
currencyCode: 'USD',
centAmount: 150000,
fractionDigits: 2,
},
cartState: 'Ordered',
origin: 'Customer',
taxMode: 'ExternalAmount',
taxRoundingMode: 'HalfEven',
taxCalculationMode: 'LineItemLevel',
shipping: [],
discountCodes: [],
directDiscounts: [],
refusedGifts: [],
itemShippingAddresses: [],
inventoryMode: 'ReserveOnOrder',
shippingMode: 'Single',
createdAt: '2024-01-01T00:00:00Z',
lastModifiedAt: '2024-01-01T00:00:00Z',
};
return mockGetCartResult;
};

export const mockGetPaymentAmount: PaymentAmount = {
centAmount: 150000,
currencyCode: 'USD',
Expand Down

0 comments on commit 2e49dcf

Please sign in to comment.