Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions libs/test/use-paystack.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,39 @@ describe('usePaystackPayment()', () => {
expect(callPaystackPop).toHaveBeenCalledTimes(1);
});

it('should pass if initializePayment is called without arguments', () => {
const {result, rerender} = renderHook(() => usePaystackPayment(config));
rerender();

act(() => {
result.current();
});

expect(callPaystackPop).toHaveBeenCalledTimes(1);
});

it('should support positional callbacks initializePayment(onSuccess, onClose)', () => {
const mockSuccess = jest.fn();
const mockClose = jest.fn();
const {result, rerender} = renderHook(() => usePaystackPayment(config));
rerender();

act(() => {
result.current(mockSuccess, mockClose);
});

expect(callPaystackPop).toHaveBeenCalledTimes(1);
const passedArgs = (callPaystackPop as jest.Mock).mock.calls[0][0];
expect(passedArgs.onSuccess).toBe(mockSuccess);
expect(passedArgs.onCancel).toBe(mockClose);

passedArgs.onSuccess('ref_test');
expect(mockSuccess).toHaveBeenCalledWith('ref_test');

passedArgs.onCancel();
expect(mockClose).toHaveBeenCalledTimes(1);
});

it('should usePaystackPayment accept all parameters', () => {
const {result, rerender} = renderHook(() =>
usePaystackPayment({
Expand Down
13 changes: 11 additions & 2 deletions libs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,20 @@ export interface PaystackProps {
onBankTransferConfirmationPending?: callback;
}

export type InitializePayment = (options: {
export type InitializePaymentOptions = {
onSuccess?: callback;
onClose?: callback;
config?: Omit<PaystackProps, 'publicKey'>;
}) => void;
};

export type InitializePayment = {
(options?: InitializePaymentOptions): void;
(
onSuccess?: callback,
onClose?: callback,
config?: Omit<PaystackProps, 'publicKey'>,
): void;
};

export type HookConfig = Omit<Partial<PaystackProps>, 'publicKey'> &
Pick<PaystackProps, 'publicKey'>;
38 changes: 32 additions & 6 deletions libs/use-paystack.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import {HookConfig, InitializePayment} from './types';
import {
HookConfig,
InitializePayment,
InitializePaymentOptions,
PaystackProps,
callback,
} from './types';
import {callPaystackPop} from './paystack-actions';

export default function usePaystackPayment(hookConfig: HookConfig): InitializePayment {
function initializePayment({config, onSuccess, onClose}: Parameters<InitializePayment>[0]): void {
const args = {...hookConfig, ...config};
export default function usePaystackPayment(
hookConfig: HookConfig,
): InitializePayment {
function initializePayment(
optionsOrOnSuccess?: InitializePaymentOptions | callback,
onClose?: callback,
config?: Omit<PaystackProps, 'publicKey'>,
): void {
let resolvedOnSuccess: callback | undefined;
let resolvedOnClose: callback | undefined;
let resolvedConfig: Omit<PaystackProps, 'publicKey'> | undefined;

if (typeof optionsOrOnSuccess === 'function') {
resolvedOnSuccess = optionsOrOnSuccess;
resolvedOnClose = onClose;
resolvedConfig = config;
} else if (optionsOrOnSuccess && typeof optionsOrOnSuccess === 'object') {
resolvedOnSuccess = optionsOrOnSuccess.onSuccess;
resolvedOnClose = optionsOrOnSuccess.onClose;
resolvedConfig = optionsOrOnSuccess.config;
}

const args = {...hookConfig, ...resolvedConfig};

const {
publicKey,
Expand All @@ -29,8 +55,8 @@ export default function usePaystackPayment(hookConfig: HookConfig): InitializePa
onBankTransferConfirmationPending,
} = args;
const paystackArgs: Record<string, any> = {
onSuccess: onSuccess ? onSuccess : () => null,
onCancel: onClose ? onClose : () => null,
onSuccess: resolvedOnSuccess ? resolvedOnSuccess : () => null,
onCancel: resolvedOnClose ? resolvedOnClose : () => null,
key: publicKey,
email,
amount,
Expand Down