Skip to content

Commit

Permalink
added testing for the helpers using jest
Browse files Browse the repository at this point in the history
  • Loading branch information
justinelut committed Jul 17, 2023
1 parent 37c41b4 commit a354c5a
Show file tree
Hide file tree
Showing 12 changed files with 1,909 additions and 57 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-fans-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mpesapay": patch
---

added testing for the helpers using jest
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ const Account_Reference = 'YOUR_ACCOUNT_REFERENCE';
const PartyA = "YOUR_MPESA_PARTYA"
const B2C_Security_Credential = "YOUR MPESA B2C SECURITY CREDENTIAL"
const Initiator_Name = "YOUR MPESA INITIATORS NAME"
const Environment = 'sandbox'
const transaction_Type = "YOUR SHORTCODE TYPE i.e paybill or till"
const Environment = 'THE ENVIRONMENT i.e sandbox or live'
const Transaction_Type = "YOUR SHORTCODE TYPE i.e paybill or till"



Expand All @@ -73,7 +73,7 @@ const mpesapay = new MpesaPay(
B2C_Security_Credential,
Initiator_Name,
Environment,
transaction_Type
Transaction_Type
);
```

Expand Down
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
preset: 'ts-jest',
testEnvironment: 'node',
};
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
}
},
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts",
"release": "yarn build && changeset publish",
"build": "yarn test && tsup src/index.ts --format cjs,esm --dts",
"test": "jest",
"release": "yarn test && yarn build && changeset publish",
"lint": "tsc"
},
"repository": {
Expand All @@ -41,6 +42,9 @@
},
"devDependencies": {
"@changesets/cli": "^2.26.2",
"@types/jest": "^29.5.3",
"jest": "^29.6.1",
"ts-jest": "^29.1.1",
"tsup": "^7.1.0",
"typescript": ">=3.0.0"
}
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/FormatBusinessToCustomer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface ReferenceData {
ReferenceItem: ReferenceItem;
}

interface Result {
export interface Result {
ResultType: number;
ResultCode: number;
ResultDesc: string;
Expand All @@ -27,11 +27,11 @@ interface Result {
ReferenceData: ReferenceData;
}

interface BusinessToCustomerResponse {
export interface BusinessToCustomerResponse {
Result: Result;
}

interface TransactionDetails {
export interface TransactionDetails {
TransactionAmount: string | number;
TransactionReceipt: string;
ReceiverPartyPublicName: string;
Expand All @@ -43,7 +43,7 @@ interface TransactionDetails {
}


interface PaymentResult {
export interface PaymentResult {
status: 'success' | 'failed';
resultCode: number;
data?: TransactionDetails;
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/FormatPaymentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface Body {
};
}

interface Data {
export interface Data {
Body: Body;
}

Expand All @@ -23,7 +23,7 @@ interface PaymentInfo {
PhoneNumber: string;
}

interface PaymentResult {
export interface PaymentResult {
status: 'canceled' | 'failed' | 'success';
data?: PaymentInfo;
message?: string;
Expand All @@ -47,7 +47,7 @@ export default function FormatPaymentData(data: Data): PaymentResult {
const paymentinfo = extractData(data);
return {
status: 'success',
message: 'Transaction processed succesfully',
message: 'Transaction processed successfully',
data: {
Amount: paymentinfo.Amount,
MpesaReceiptNumber: paymentinfo.MpesaReceiptNumber,
Expand All @@ -66,7 +66,7 @@ export default function FormatPaymentData(data: Data): PaymentResult {
} else {
return {
status: 'canceled',
message: 'Transaction was cancelled by the user',
message: 'Transaction was canceled by the user',
resultCode: resultscode,
};
}
Expand Down
71 changes: 71 additions & 0 deletions src/tests/helpers/FormatAccountBalance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import FormatAccountBalance from "../../helpers/FormatAccountBalance";

describe('FormatAccountBalance', () => {
const accountBalanceData = {
Result: {
ResultType: 1,
ResultCode: 200,
ResultDesc: 'Success',
ResultParameters: {
ResultParameter: [
{
Key: 'AccountBalance',
Value: 'Account1|USD|100|80|10|10&Account2|EUR|200|150|20|30',
},
],
},
},
};

it('should format account balances correctly', () => {
const expectedOutput = {
resultCode: 200,
status: 'success',
accountBalances: [
{
accountName: 'Account1',
currency: 'USD',
balance: '100',
availableBalance: '80',
reservedAmount: '10',
unClearedBalance: '10',
},
{
accountName: 'Account2',
currency: 'EUR',
balance: '200',
availableBalance: '150',
reservedAmount: '20',
unClearedBalance: '30',
},
],
};

const formattedAccountBalance = FormatAccountBalance(accountBalanceData);
expect(formattedAccountBalance).toEqual(expectedOutput);
});

it('should handle missing account balance data', () => {
const accountBalanceDataWithoutResultParameter = {
Result: {
ResultType: 1,
ResultCode: 200,
ResultDesc: 'Success',
ResultParameters: {
ResultParameter: [],
},
},
};

const expectedOutput = {
resultCode: 200,
status: 'failed',
accountBalances: [],
};

const formattedAccountBalance = FormatAccountBalance(
accountBalanceDataWithoutResultParameter
);
expect(formattedAccountBalance).toEqual(expectedOutput);
});
});
83 changes: 83 additions & 0 deletions src/tests/helpers/FormatBusinessToCustomer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import FormatBusinessToCustomer, { BusinessToCustomerResponse, PaymentResult } from "../../helpers/FormatBusinessToCustomer"

describe('FormatBusinessToCustomer', () => {
const mockData: BusinessToCustomerResponse = {
Result: {
ResultType: 1,
ResultCode: 0,
ResultDesc: 'Success',
OriginatorConversationID: 'abc123',
ConversationID: 'def456',
TransactionID: 'ghi789',
ResultParameters: {
ResultParameter: [
{ Key: 'TransactionAmount', Value: 100 },
{ Key: 'TransactionReceipt', Value: 'receipt123' },
{ Key: 'ReceiverPartyPublicName', Value: 'John Doe' },
{ Key: 'TransactionCompletedDateTime', Value: '2023-07-15T10:30:00' },
{ Key: 'B2CUtilityAccountAvailableFunds', Value: 500 },
{ Key: 'B2CWorkingAccountAvailableFunds', Value: 200 },
{ Key: 'B2CRecipientIsRegisteredCustomer', Value: 'Yes' },
{ Key: 'B2CChargesPaidAccountAvailableFunds', Value: 100 },
],
},
ReferenceData: {
ReferenceItem: {
Key: 'refKey',
Value: 'refValue',
},
},
},
};

it('should format business-to-customer response correctly', () => {
const expectedOutput: PaymentResult = {
status: 'success',
resultCode: 0,
data: {
TransactionAmount: '100',
TransactionReceipt: 'receipt123',
ReceiverPartyPublicName: 'John Doe',
TransactionCompletedDateTime: '2023-07-15T10:30:00',
B2CUtilityAccountAvailableFunds: '500',
B2CWorkingAccountAvailableFunds: '200',
B2CRecipientIsRegisteredCustomer: 'Yes',
B2CChargesPaidAccountAvailableFunds: '100',
},
};

const formattedResponse: PaymentResult = FormatBusinessToCustomer(mockData);
expect(formattedResponse).toEqual(expectedOutput);
});

it('should handle failed business-to-customer response', () => {
const mockDataFailed: BusinessToCustomerResponse = {
Result: {
ResultType: 1,
ResultCode: 1,
ResultDesc: 'Failed',
OriginatorConversationID: 'abc123',
ConversationID: 'def456',
TransactionID: 'ghi789',
ResultParameters: {
ResultParameter: [],
},
ReferenceData: {
ReferenceItem: {
Key: 'refKey',
Value: 'refValue',
},
},
},
};

const expectedOutputFailed: PaymentResult = {
status: 'failed',
resultCode: 1,
};

const formattedResponseFailed: PaymentResult =
FormatBusinessToCustomer(mockDataFailed);
expect(formattedResponseFailed).toEqual(expectedOutputFailed);
});
});
83 changes: 83 additions & 0 deletions src/tests/helpers/FormatPaymentData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import FormatPaymentData, {
Data,
PaymentResult,
} from '../../helpers/FormatPaymentData';

describe('FormatPaymentData', () => {
const mockDataSuccess: Data = {
Body: {
stkCallback: {
ResultCode: 0,
CallbackMetadata: {
Item: [
{ Name: 'Amount', Value: '100' },
{ Name: 'MpesaReceiptNumber', Value: 'receipt123' },
{ Name: 'TransactionDate', Value: '2023-07-15' },
{ Name: 'PhoneNumber', Value: '1234567890' },
],
},
},
},
};

const mockDataFailure: Data = {
Body: {
stkCallback: {
ResultCode: 17,
CallbackMetadata: {
Item: [],
},
},
},
};

const mockDataCancelled: Data = {
Body: {
stkCallback: {
ResultCode: 1,
CallbackMetadata: {
Item: [],
},
},
},
};

it('should format successful payment data', () => {
const expectedOutput: PaymentResult = {
status: 'success',
message: 'Transaction processed successfully',
data: {
Amount: '100',
MpesaReceiptNumber: 'receipt123',
TransactionDate: '2023-07-15',
PhoneNumber: '1234567890',
},
resultCode: 0,
};

const formattedData: PaymentResult = FormatPaymentData(mockDataSuccess);
expect(formattedData).toEqual(expectedOutput);
});

it('should format failed payment data', () => {
const expectedOutput: PaymentResult = {
status: 'failed',
message: 'Unable to process the transaction',
resultCode: 17,
};

const formattedData: PaymentResult = FormatPaymentData(mockDataFailure);
expect(formattedData).toEqual(expectedOutput);
});

it('should format cancelled payment data', () => {
const expectedOutput: PaymentResult = {
status: 'canceled',
message: 'Transaction was canceled by the user',
resultCode: 1,
};

const formattedData: PaymentResult = FormatPaymentData(mockDataCancelled);
expect(formattedData).toEqual(expectedOutput);
});
});
2 changes: 0 additions & 2 deletions src/transactions/stkpushQuery/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export default async function StkPushQuery(
timeStamp,
CheckoutRequestID
);


try {
const response: AxiosResponse<any> = await axios.post(
`${environment}/mpesa/stkpushquery/v1/query`,
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
"noUncheckedIndexedAccess": true,
"noEmit": true
}
},
"include": ["src", "test"]
}
Loading

0 comments on commit a354c5a

Please sign in to comment.