Skip to content

Commit

Permalink
Add webhook test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tung-Huynh-Shopmacher committed Jul 23, 2024
1 parent be3ec94 commit e9f2c92
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 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
96 changes: 95 additions & 1 deletion 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,14 +16,20 @@ import {
} from '../../src/utils/constant.utils';
import { Payment as molliePayment } from '@mollie/api-client';
import { CTTransactionState } from '../../src/types/commercetools.types';
import { listPaymentMethods } from '../../src/mollie/payment.mollie';
import { listPaymentMethods, getPaymentById } from '../../src/mollie/payment.mollie';
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 All @@ -34,6 +41,7 @@ jest.mock('../../src/service/payment.service.ts', () => ({
jest.mock('../../src/mollie/payment.mollie', () => ({
listPaymentMethods: jest.fn(),
createMolliePayment: jest.fn(),
getPaymentById: jest.fn(),
}));

describe('Test listPaymentMethodsByPayment', () => {
Expand Down Expand Up @@ -585,3 +593,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 e9f2c92

Please sign in to comment.