From a78f6907e69f44808e8180177e3eb19c552dab17 Mon Sep 17 00:00:00 2001 From: Paul Cramer Date: Tue, 10 Sep 2024 16:34:07 -0700 Subject: [PATCH] refactor --- src/swap/components/SwapProvider.test.tsx | 71 +++++++++++++---------- src/swap/components/SwapProvider.tsx | 7 ++- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/src/swap/components/SwapProvider.test.tsx b/src/swap/components/SwapProvider.test.tsx index 8152a7a41a..28d40ebe9b 100644 --- a/src/swap/components/SwapProvider.test.tsx +++ b/src/swap/components/SwapProvider.test.tsx @@ -406,36 +406,6 @@ describe('SwapProvider', () => { expect(onSuccessMock).toHaveBeenCalled(); }); - it('should reset status to init when setLifeCycleStatus is called with success', async () => { - const onStatusMock = vi.fn(); - renderWithProviders({ - Component: TestSwapComponent, - onStatus: onStatusMock, - }); - const button = screen.getByText('setLifeCycleStatus.success'); - fireEvent.click(button); - await waitFor(() => { - expect(onStatusMock).toHaveBeenCalledWith({ - statusName: 'success', - statusData: { - receipt: ['0x123'], - }, - }); - }); - await waitFor( - () => { - expect(onStatusMock).toHaveBeenCalledWith({ - statusName: 'init', - statusData: { - isMissingRequiredField: false, - maxSlippage: expect.any(Number), - }, - }); - }, - { timeout: 3000 }, - ); // Increase timeout if needed - }); - it('should emit onStatus when setLifeCycleStatus is called with error', async () => { const onStatusMock = vi.fn(); renderWithProviders({ @@ -496,6 +466,47 @@ describe('SwapProvider', () => { ); }); + it('should use DEFAULT_MAX_SLIPPAGE when lifeCycleStatus.statusData is falsy and experimental.maxSlippage is not provided', async () => { + const { result } = renderHook(() => useSwapContext(), { + wrapper: ({ children }) => ( + + + + {children} + + + + ), + }); + act(() => { + result.current.setLifeCycleStatus({ + statusName: 'error', + statusData: null, + }); + }); + vi.mocked(getSwapQuote).mockResolvedValueOnce({ + toAmount: '100', + to: { decimals: 18 }, + }); + await act(async () => { + await result.current.handleAmountChange( + 'from', + '10', + ETH_TOKEN, + DEGEN_TOKEN, + ); + }); + expect(getSwapQuote).toHaveBeenCalledWith( + expect.objectContaining({ + maxSlippage: String(DEFAULT_MAX_SLIPPAGE), + }), + ); + expect(result.current.lifeCycleStatus.statusName).toBe('amountChange'); + expect(result.current.lifeCycleStatus.statusData.maxSlippage).toBe( + DEFAULT_MAX_SLIPPAGE, + ); + }); + it('should pass the correct amountReference to getSwapQuote', async () => { const TestComponent = () => { const { handleAmountChange } = useSwapContext(); diff --git a/src/swap/components/SwapProvider.tsx b/src/swap/components/SwapProvider.tsx index 20ff860847..b7d7b1a63d 100644 --- a/src/swap/components/SwapProvider.tsx +++ b/src/swap/components/SwapProvider.tsx @@ -70,9 +70,10 @@ export function SwapProvider({ const resetInputs = useResetInputs({ from, to }); const getMaxSlippage = useCallback(() => { - return lifeCycleStatus.statusName !== 'error' - ? lifeCycleStatus.statusData.maxSlippage - : experimental.maxSlippage || DEFAULT_MAX_SLIPPAGE; + if (lifeCycleStatus.statusData && lifeCycleStatus.statusName !== 'error') { + return lifeCycleStatus.statusData.maxSlippage; + } + return experimental.maxSlippage || DEFAULT_MAX_SLIPPAGE; }, [lifeCycleStatus, experimental.maxSlippage]); // Component lifecycle emitters useEffect(() => {