Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcramer committed Sep 10, 2024
1 parent 77e2192 commit a78f690
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
71 changes: 41 additions & 30 deletions src/swap/components/SwapProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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 }) => (
<WagmiProvider config={config}>
<QueryClientProvider client={queryClient}>
<SwapProvider experimental={{ useAggregator: true }}>
{children}
</SwapProvider>
</QueryClientProvider>
</WagmiProvider>
),
});
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),

Check failure on line 501 in src/swap/components/SwapProvider.test.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

src/swap/components/SwapProvider.test.tsx > SwapProvider > should use DEFAULT_MAX_SLIPPAGE when lifeCycleStatus.statusData is falsy and experimental.maxSlippage is not provided

ReferenceError: DEFAULT_MAX_SLIPPAGE is not defined ❯ src/swap/components/SwapProvider.test.tsx:501:29
}),
);
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();
Expand Down
7 changes: 4 additions & 3 deletions src/swap/components/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down

0 comments on commit a78f690

Please sign in to comment.