diff --git a/libs/test/use-paystack.test.tsx b/libs/test/use-paystack.test.tsx index b8c1dfc..272b044 100644 --- a/libs/test/use-paystack.test.tsx +++ b/libs/test/use-paystack.test.tsx @@ -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({ diff --git a/libs/types.ts b/libs/types.ts index 8ea3b3f..6199e94 100644 --- a/libs/types.ts +++ b/libs/types.ts @@ -59,11 +59,20 @@ export interface PaystackProps { onBankTransferConfirmationPending?: callback; } -export type InitializePayment = (options: { +export type InitializePaymentOptions = { onSuccess?: callback; onClose?: callback; config?: Omit; -}) => void; +}; + +export type InitializePayment = { + (options?: InitializePaymentOptions): void; + ( + onSuccess?: callback, + onClose?: callback, + config?: Omit, + ): void; +}; export type HookConfig = Omit, 'publicKey'> & Pick; diff --git a/libs/use-paystack.ts b/libs/use-paystack.ts index 2dc5a43..e2de17c 100644 --- a/libs/use-paystack.ts +++ b/libs/use-paystack.ts @@ -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[0]): void { - const args = {...hookConfig, ...config}; +export default function usePaystackPayment( + hookConfig: HookConfig, +): InitializePayment { + function initializePayment( + optionsOrOnSuccess?: InitializePaymentOptions | callback, + onClose?: callback, + config?: Omit, + ): void { + let resolvedOnSuccess: callback | undefined; + let resolvedOnClose: callback | undefined; + let resolvedConfig: Omit | 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, @@ -29,8 +55,8 @@ export default function usePaystackPayment(hookConfig: HookConfig): InitializePa onBankTransferConfirmationPending, } = args; const paystackArgs: Record = { - onSuccess: onSuccess ? onSuccess : () => null, - onCancel: onClose ? onClose : () => null, + onSuccess: resolvedOnSuccess ? resolvedOnSuccess : () => null, + onCancel: resolvedOnClose ? resolvedOnClose : () => null, key: publicKey, email, amount,