Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlec committed Sep 30, 2024
1 parent 2c9283e commit c168ed1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
56 changes: 45 additions & 11 deletions src/api/buildPayTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { Mock } from 'vitest';
import { CDP_HYDRATE_CHARGE } from '../network/definitions/pay';
import {
CDP_CREATE_PRODUCT_CHARGE,
CDP_HYDRATE_CHARGE,
} from '../network/definitions/pay';
import { sendRequest } from '../network/request';
import {
PAY_INVALID_CHARGE_ERROR_MESSAGE,
Expand All @@ -11,25 +14,34 @@ import {
*/
import { buildPayTransaction } from './buildPayTransaction';
import {
MOCK_CREATE_PRODUCT_CHARGE_SUCCESS_RESPONSE,
MOCK_HYDRATE_CHARGE_INVALID_CHARGE_ERROR_RESPONSE,
MOCK_HYDRATE_CHARGE_SUCCESS_RESPONSE,
MOCK_INVALID_CHARGE_ID,
MOCK_VALID_CHARGE_ID,
MOCK_VALID_PAYER_ADDRESS,
MOCK_VALID_PRODUCT_ID,
} from './mocks';
import type {
BuildPayTransactionParams,
CreateProductChargeParams,
HydrateChargeAPIParams,
} from './types';

vi.mock('../network/request');
vi.mock('./utils/getPayErrorMessage', () => ({
getPayErrorMessage: vi.fn((code?: string) => {
if (code === 'INVALID_CHARGE') return PAY_INVALID_CHARGE_ERROR_MESSAGE;
return UNCAUGHT_PAY_ERROR_MESSAGE;
}),
}));

describe('buildPayTransaction', () => {
afterEach(() => {
vi.clearAllMocks();
});

it('should return a Pay Transaction', async () => {
it('should return a Pay Transaction with chargeId', async () => {
const mockParams: BuildPayTransactionParams = {
address: MOCK_VALID_PAYER_ADDRESS,
chargeId: MOCK_VALID_CHARGE_ID,
Expand All @@ -49,24 +61,46 @@ describe('buildPayTransaction', () => {
]);
});

it('should return an error if sendRequest fails', async () => {
it('should return a Pay Transaction with productId', async () => {
const mockParams: BuildPayTransactionParams = {
address: MOCK_VALID_PAYER_ADDRESS,
chargeId: MOCK_VALID_CHARGE_ID,
productId: MOCK_VALID_PRODUCT_ID,
};
const mockAPIParams: HydrateChargeAPIParams = {
const mockAPIParams: CreateProductChargeParams = {
sender: MOCK_VALID_PAYER_ADDRESS,
chargeId: MOCK_VALID_CHARGE_ID,
productId: MOCK_VALID_PRODUCT_ID,
};
(sendRequest as Mock).mockResolvedValue(
MOCK_HYDRATE_CHARGE_SUCCESS_RESPONSE,
MOCK_CREATE_PRODUCT_CHARGE_SUCCESS_RESPONSE,
);
const payTransaction = await buildPayTransaction(mockParams);
expect(payTransaction).toEqual(
MOCK_CREATE_PRODUCT_CHARGE_SUCCESS_RESPONSE.result,
);
const hydratedCharge = await buildPayTransaction(mockParams);
expect(hydratedCharge).toEqual(MOCK_HYDRATE_CHARGE_SUCCESS_RESPONSE.result);
expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_HYDRATE_CHARGE, [
expect(sendRequest).toHaveBeenCalledWith(CDP_CREATE_PRODUCT_CHARGE, [
mockAPIParams,
]);
});

it('should return an error if neither chargeId nor productId is provided', async () => {
const mockParams: BuildPayTransactionParams = {
address: MOCK_VALID_PAYER_ADDRESS,
};
const error = await buildPayTransaction(mockParams);
expect(error).toEqual({
code: 'AmBPTa01',
error: 'No chargeId or productId provided',
message: UNCAUGHT_PAY_ERROR_MESSAGE,
});
expect(sendRequest).not.toHaveBeenCalled();
});

it('should return an error if sendRequest fails', async () => {
const mockParams: BuildPayTransactionParams = {
address: MOCK_VALID_PAYER_ADDRESS,
chargeId: MOCK_VALID_CHARGE_ID,
};
const mockError = new Error(
'buildPayTransaction: Error: Failed to send request',
);
Expand All @@ -79,7 +113,7 @@ describe('buildPayTransaction', () => {
});
});

it('should return an error object from buildPayTransaction', async () => {
it('should return an error object when hydrating an invalid charge', async () => {
const mockParams: BuildPayTransactionParams = {
address: MOCK_VALID_PAYER_ADDRESS,
chargeId: MOCK_INVALID_CHARGE_ID,
Expand Down
28 changes: 28 additions & 0 deletions src/api/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ export const MOCK_HYDRATE_CHARGE_SUCCESS_RESPONSE = {
},
},
};
export const MOCK_VALID_PRODUCT_ID = '1b03e80d-4e87-46fd-9772-422a1b693fb7';
export const MOCK_CREATE_PRODUCT_CHARGE_SUCCESS_RESPONSE = {
id: 1,
jsonrpc: '2.0',
result: {
id: MOCK_VALID_CHARGE_ID,
callData: {
deadline: '2024-08-29T23:00:38Z',
feeAmount: '10000',
id: '0xd2e57fb373f246768a193cadd4a5ce1e',
operator: '0xd1db362f9d23a029834375afa2b37d91d2e67a95',
prefix: '0x4b3220496e666f726d6174696f6e616c204d6573736167653a20333220',
recipient: '0xb724dcF5f1156dd8E2AB217921b5Bd46a9e5cAa5',
recipientAmount: '990000',
recipientCurrency: '0xF175520C52418dfE19C8098071a252da48Cd1C19',
refundDestination: MOCK_VALID_PAYER_ADDRESS,
signature:
'0xb49a08026bdfdc55e3b1b797a9481fbdb7a9246c73f5f77fece76d5f24e979561f2168862aed0dd72980a4f9930cf23836084f3326b98b5546b280c4f0d57aae1b',
},
metaData: {
chainId: 8453,
contractAddress: '0x131642c019AF815Ae5F9926272A70C84AE5C37ab',
sender: MOCK_VALID_PAYER_ADDRESS,
settlementCurrencyAddress: '0xF175520C52418dfE19C8098071a252da48Cd1C19',
},
},
};

export const MOCK_HYDRATE_CHARGE_INVALID_CHARGE_ERROR_RESPONSE = {
id: 1,
jsonrpc: '2.0',
Expand Down

0 comments on commit c168ed1

Please sign in to comment.