Skip to content

Commit

Permalink
Merge pull request #46 from iamshabell/feat/edahab-tests
Browse files Browse the repository at this point in the history
add(marupay): edahab unit test and overall jest configuration
  • Loading branch information
iamshabell authored Dec 19, 2023
2 parents 724fef9 + 8336e39 commit 3b36401
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 229 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"build": "turbo build",
"dev": "turbo dev",
"lint": "turbo lint",
"test": "turbo test",
"test:coverage": "turbo test:coverage",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"publish-package": "turbo run build lint && changeset version && changeset publish"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/marupay/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
testPathIgnorePatterns: ['/node_modules/'],
moduleFileExtensions: ['ts', 'js', 'json'],
collectCoverage: true,
};
2 changes: 1 addition & 1 deletion packages/marupay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"dev": "yarn build -- --watch",
"lint": "eslint \"{src,test}/**/*.ts\" --fix",
"test": "jest --runInBand",
"test:cov": "jest --coverage --runInBand"
"test:coverage": "jest --coverage --runInBand"
},
"author": {
"name": "Mubarak Shabel",
Expand Down
79 changes: 79 additions & 0 deletions packages/marupay/src/handlers/edahab/edahab.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import axios from 'axios';
import { EdahabHandler, createEdahabHandler } from './edahab'; // Import your EdahabHandler
import { Currency } from '../../handlers/enums';
import { VendorErrorException } from '../../handlers/exeptions';

jest.mock('axios');

Check warning on line 6 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'jest' is not defined

Check warning on line 6 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / Release

'jest' is not defined

const mockedAxios = axios as jest.Mocked<typeof axios>;

Check warning on line 8 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'jest' is not defined

Check warning on line 8 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / Release

'jest' is not defined

describe('Edahab Handler', () => {

Check warning on line 10 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'describe' is not defined

Check warning on line 10 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / Release

'describe' is not defined
let handler: EdahabHandler;
let options: any;

beforeAll(() => {

Check warning on line 14 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'beforeAll' is not defined

Check warning on line 14 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / Release

'beforeAll' is not defined
handler = createEdahabHandler({
apiKey: 'yourApiKey',
secretKey: 'yourSecretKey',
merchantId: 'yourMerchantId',
});
options = {
accountNumber: '+252611234569',
amount: 500,
currency: Currency.SLSH,
description: 'Test purchase',
}
});

it('returns the success payment response for purchase', async () => {

Check warning on line 28 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'it' is not defined

Check warning on line 28 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / Release

'it' is not defined
const serverResponse = {
InvoiceStatus: "Paid",
TransactionId: "MP2234219.2220.A91111",
InvoiceId: 10145,
};

mockedAxios.post.mockResolvedValueOnce({ data: serverResponse });

const result = await handler.purchase(options);

expect(result.paymentStatus).toBe('Paid');

Check warning on line 39 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'expect' is not defined

Check warning on line 39 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / Release

'expect' is not defined
});

it('throws vendor errors for purchase accordingly', async () => {

Check warning on line 42 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'it' is not defined

Check warning on line 42 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / Release

'it' is not defined
const serverResponse = {
InvoiceStatus: "Unpaid",
TransactionId: "MP2234219.2220.A91111",
InvoiceId: 10145,
};

mockedAxios.post.mockResolvedValueOnce({ data: serverResponse });

await expect(handler.purchase(options)).rejects.toThrow(new VendorErrorException('1', 'Unpaid'));

Check warning on line 51 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'expect' is not defined

Check warning on line 51 in packages/marupay/src/handlers/edahab/edahab.spec.ts

View workflow job for this annotation

GitHub Actions / Release

'expect' is not defined
});

it('returns the success payment response for credit', async () => {
const serverResponse = {
TransactionId: '5678',
TransactionStatus: 'Approved',
TransactionMesage: 'Credit successful',
};

mockedAxios.post.mockResolvedValueOnce({ data: serverResponse });

const result = await handler.credit(options);

expect(result.paymentStatus).toBe('Approved');
});

it('throws vendor errors for credit accordingly', async () => {
const serverResponse = {
TransactionId: '5678',
TransactionStatus: 'Failed',
};

mockedAxios.post.mockResolvedValueOnce({ data: serverResponse });

await expect(handler.credit(options)).rejects.toThrow(new VendorErrorException('Failed', 'EDAHAB-CREDIT-ERROR'));
});

});
6 changes: 6 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
"dev": {
"cache": false,
"persistent": true
},
"test": {
"dependsOn": ["^test"]
},
"test:coverage" : {
"dependsOn": ["^test:coverage"]
}
}
}
Loading

0 comments on commit 3b36401

Please sign in to comment.