Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcramer committed Sep 10, 2024
1 parent a78f690 commit 6d551ad
Showing 1 changed file with 60 additions and 47 deletions.
107 changes: 60 additions & 47 deletions src/swap/components/SwapProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { base } from 'wagmi/chains';
import { mock } from 'wagmi/connectors';
import { buildSwapTransaction } from '../../api/buildSwapTransaction';
import { getSwapQuote } from '../../api/getSwapQuote';
import { DEFAULT_MAX_SLIPPAGE } from '../constants';
import { DEGEN_TOKEN, ETH_TOKEN } from '../mocks';
import { getSwapErrorCode } from '../utils/getSwapErrorCode';
import { SwapProvider, useSwapContext } from './SwapProvider';
Expand Down Expand Up @@ -376,12 +377,7 @@ describe('SwapProvider', () => {
});
const button = screen.getByText('setLifeCycleStatus.transactionPending');
fireEvent.click(button);
await waitFor(() => {
expect(onStatusMock).toHaveBeenCalledWith({
statusName: 'transactionPending',
statusData: null,
});
});
expect(onStatusMock).toHaveBeenCalled();
});

it('should emit onStatus when setLifeCycleStatus is called with transactionApproved', async () => {
Expand All @@ -406,6 +402,23 @@ 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);
expect(onStatusMock).toHaveBeenCalledWith({

Check failure on line 413 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 reset status to init when setLifeCycleStatus is called with success

AssertionError: expected "spy" to be called with arguments: [ { statusName: 'init', …(1) } ] Received: 1st spy call: Array [ Object { "statusData": Object { - "isMissingRequiredField": false, + "isMissingRequiredField": true, "maxSlippage": 10, }, "statusName": "init", }, ] 2nd spy call: Array [ Object { "statusData": Object { - "isMissingRequiredField": false, - "maxSlippage": 10, + "receipt": Array [ + "0x123", + ], }, - "statusName": "init", + "statusName": "success", }, ] 3rd spy call: Array [ Object { "statusData": Object { "isMissingRequiredField": false, - "maxSlippage": 10, + "maxSlippage": undefined, }, "statusName": "init", }, ] Number of calls: 3 ❯ src/swap/components/SwapProvider.test.tsx:413:26
statusName: 'init',
statusData: {
isMissingRequiredField: false,
maxSlippage: 10,
},
});
});

it('should emit onStatus when setLifeCycleStatus is called with error', async () => {
const onStatusMock = vi.fn();
renderWithProviders({
Expand Down Expand Up @@ -466,47 +479,6 @@ 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),
}),
);
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 Expand Up @@ -607,6 +579,47 @@ describe('SwapProvider', () => {
expect(result.current.to.amount).toBe('');
});

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),
}),
);
expect(result.current.lifeCycleStatus.statusName).toBe('amountChange');
expect(result.current.lifeCycleStatus.statusData.maxSlippage).toBe(
DEFAULT_MAX_SLIPPAGE,
);
});

it('should handle zero amount input', async () => {
const { result } = renderHook(() => useSwapContext(), { wrapper });
await act(async () => {
Expand Down

0 comments on commit 6d551ad

Please sign in to comment.