-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added testing for the helpers using jest
- Loading branch information
1 parent
37c41b4
commit a354c5a
Showing
12 changed files
with
1,909 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"mpesapay": patch | ||
--- | ||
|
||
added testing for the helpers using jest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.