From 2e49dcff8fe078c94a180a147bd4506f1d4bd2ec Mon Sep 17 00:00:00 2001 From: King-Hin Leung Date: Tue, 19 Mar 2024 10:38:19 +0100 Subject: [PATCH] Add test case for helper.converter --- .../src/services/adyen-payment.service.ts | 1 - .../services/adyen-payment.service.spec.ts | 14 +-- .../converters/helper.converter.spec.ts | 69 ++++++++++ processor/test/utils/mock-cart-data.ts | 119 ++++++++++++++++++ processor/test/utils/mock-payment-data.ts | 33 ----- 5 files changed, 195 insertions(+), 41 deletions(-) create mode 100644 processor/test/services/converters/helper.converter.spec.ts create mode 100644 processor/test/utils/mock-cart-data.ts diff --git a/processor/src/services/adyen-payment.service.ts b/processor/src/services/adyen-payment.service.ts index b8af3f3..a1717c2 100644 --- a/processor/src/services/adyen-payment.service.ts +++ b/processor/src/services/adyen-payment.service.ts @@ -240,7 +240,6 @@ export class AdyenPaymentService extends AbstractPaymentService { paymentId: ctPayment.id, }); } - const data = this.createPaymentConverter.convertRequest({ data: opts.data, cart: ctCart, diff --git a/processor/test/services/adyen-payment.service.spec.ts b/processor/test/services/adyen-payment.service.spec.ts index 5bab259..63ee7ca 100644 --- a/processor/test/services/adyen-payment.service.spec.ts +++ b/processor/test/services/adyen-payment.service.spec.ts @@ -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, @@ -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, @@ -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 @@ -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, }, }; diff --git a/processor/test/services/converters/helper.converter.spec.ts b/processor/test/services/converters/helper.converter.spec.ts new file mode 100644 index 0000000..218cb90 --- /dev/null +++ b/processor/test/services/converters/helper.converter.spec.ts @@ -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(''); + }); +}); diff --git a/processor/test/utils/mock-cart-data.ts b/processor/test/utils/mock-cart-data.ts new file mode 100644 index 0000000..48e4f92 --- /dev/null +++ b/processor/test/utils/mock-cart-data.ts @@ -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', +}; diff --git a/processor/test/utils/mock-payment-data.ts b/processor/test/utils/mock-payment-data.ts index 42ca45f..5267e88 100644 --- a/processor/test/utils/mock-payment-data.ts +++ b/processor/test/utils/mock-payment-data.ts @@ -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', @@ -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',