Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Tung-Huynh-Shopmacher committed Jul 23, 2024
2 parents adefab0 + e9f2c92 commit 78d325c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 7 deletions.
2 changes: 1 addition & 1 deletion processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"build": "rimraf ./dist && tsc",
"watch": "nodemon -q --ignore '**/*.spec.ts' src/index.ts",
"lint": "eslint . --ext .ts",
"prettier": "prettier --write '**/*.{js,ts}'",
"prettier:check": "prettier --check '**/*.{js,ts}'",
"fix": "eslint . --ext .ts --fix && prettier --write '**/*.{js,ts}'",
"test": "jest --detectOpenHandles --clearMocks --colors --config jest.config.cjs --ci --ci --reporters=default --reporters=jest-junit --coverage",
"test:watch": "jest --watch --clearMocks --detectOpenHandles --colors --config jest.config.cjs",
"preinstall": "npx npm-force-resolutions",
Expand Down
100 changes: 94 additions & 6 deletions processor/tests/service/payment.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, test, expect, jest, beforeEach, afterEach, it } from '@jest/globals';
import { CustomFields, Payment } from '@commercetools/platform-sdk';
import {
handlePaymentWebhook,
getCreatePaymentUpdateAction,
getPaymentCancelRefundActions,
handleCreatePayment,
Expand All @@ -15,19 +16,20 @@ import {
} from '../../src/utils/constant.utils';
import { PaymentStatus, Payment as molliePayment } from '@mollie/api-client';
import { CTTransactionState } from '../../src/types/commercetools.types';
import {
cancelPaymentRefund,
createMolliePayment,
getPaymentById,
listPaymentMethods,
} from '../../src/mollie/payment.mollie';
import { listPaymentMethods, getPaymentById, createMolliePayment, cancelPaymentRefund } from '../../src/mollie/payment.mollie';

Check failure on line 19 in processor/tests/service/payment.service.spec.ts

View workflow job for this annotation

GitHub Actions / build-processor

Replace `·listPaymentMethods,·getPaymentById,·createMolliePayment,·cancelPaymentRefund·` with `⏎··listPaymentMethods,⏎··getPaymentById,⏎··createMolliePayment,⏎··cancelPaymentRefund,⏎`

Check failure on line 19 in processor/tests/service/payment.service.spec.ts

View workflow job for this annotation

GitHub Actions / build-processor

Replace `·listPaymentMethods,·getPaymentById,·createMolliePayment,·cancelPaymentRefund·` with `⏎··listPaymentMethods,⏎··getPaymentById,⏎··createMolliePayment,⏎··cancelPaymentRefund,⏎`
import CustomError from '../../src/errors/custom.error';
import { logger } from '../../src/utils/logger.utils';
import { getPaymentByMolliePaymentId, updatePayment } from '../../src/commercetools/payment.commercetools';
const uuid = '5c8b0375-305a-4f19-ae8e-07806b101999';
jest.mock('uuid', () => ({
v4: () => uuid,
}));

jest.mock('../../src/commercetools/payment.commercetools', () => ({
getPaymentByMolliePaymentId: jest.fn(),
updatePayment: jest.fn(),
}));

jest.mock('../../src/service/payment.service.ts', () => ({
...(jest.requireActual('../../src/service/payment.service.ts') as object),
getCreatePaymentUpdateAction: jest.fn(),
Expand Down Expand Up @@ -734,3 +736,89 @@ describe('Test handlePaymentCancelRefund', () => {
});
});
});

describe('Test handlePaymentWebhook', () => {
it('should handle with no action', async () => {
const fakePaymentId = 'tr_XXXX';
(getPaymentById as jest.Mock).mockReturnValue({
id: fakePaymentId,
status: 'open',
amount: {
currency: 'EUR',
value: '10.00',
},
});
(getPaymentByMolliePaymentId as jest.Mock).mockReturnValue({
transactions: [
{
interactionId: fakePaymentId,
state: 'Initial',
},
],
});
await handlePaymentWebhook(fakePaymentId);
expect(logger.debug).toBeCalledTimes(2);
expect(logger.debug).toBeCalledWith(`SCTM - handlePaymentWebhook - paymentId:${fakePaymentId}`);
expect(logger.debug).toBeCalledWith(`handlePaymentWebhook - No actions needed`);
});

it('should handle with add action', async () => {
const fakePaymentId = 'tr_XXXX';
(getPaymentById as jest.Mock).mockReturnValue({
id: fakePaymentId,
status: 'open',
amount: {
currency: 'EUR',
value: '10.00',
},
});
const ctPayment = {
transactions: [],
};
(getPaymentByMolliePaymentId as jest.Mock).mockReturnValue(ctPayment);
await handlePaymentWebhook(fakePaymentId);
expect(updatePayment as jest.Mock).toBeCalledTimes(1);
expect(updatePayment as jest.Mock).toBeCalledWith(ctPayment, [
{
action: 'addTransaction',
transaction: {
amount: { centAmount: 1000, currencyCode: 'EUR', fractionDigits: 2, type: 'centPrecision' },
interactionId: 'tr_XXXX',
state: 'Initial',
type: 'Charge',
},
},
]);
});

it('should handle with update action', async () => {
const fakePaymentId = 'tr_XXXX';
(getPaymentById as jest.Mock).mockReturnValue({
id: fakePaymentId,
status: 'paid',
amount: {
currency: 'EUR',
value: '10.00',
},
});
const ctPayment = {
transactions: [
{
id: 'test',
interactionId: fakePaymentId,
state: 'Initial',
},
],
};
(getPaymentByMolliePaymentId as jest.Mock).mockReturnValue(ctPayment);
await handlePaymentWebhook(fakePaymentId);
expect(updatePayment as jest.Mock).toBeCalledTimes(1);
expect(updatePayment as jest.Mock).toBeCalledWith(ctPayment, [
{
action: 'changeTransactionState',
transactionId: 'test',
state: 'Success',
},
]);
});
});

0 comments on commit 78d325c

Please sign in to comment.