Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alessey committed Sep 13, 2024
1 parent 5a1547f commit 5a8edd8
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 265 deletions.
38 changes: 32 additions & 6 deletions src/swap/components/SwapButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('SwapButton', () => {
address: '0x123',
to: { loading: false, amount: 1, token: 'ETH' },
from: { loading: false, amount: 1, token: 'BTC' },
loading: false,
lifeCycleStatus: { statusName: 'init' },
handleSubmit: mockHandleSubmit,
});
render(<SwapButton />);
Expand All @@ -44,7 +44,33 @@ describe('SwapButton', () => {
useSwapContextMock.mockReturnValue({
to: { loading: true, amount: 1, token: 'ETH' },
from: { loading: false, amount: 1, token: 'BTC' },
loading: false,
lifeCycleStatus: { statusName: 'init' },
handleSubmit: mockHandleSubmit,
});
render(<SwapButton />);
const button = screen.getByTestId('ockSwapButton_Button');
expect(screen.getByTestId('spinner')).toBeInTheDocument();
expect(button).toBeDisabled();
});

it('should render Spinner when transaction is pending', () => {
useSwapContextMock.mockReturnValue({
to: { loading: false, amount: 1, token: 'ETH' },
from: { loading: false, amount: 1, token: 'BTC' },
lifeCycleStatus: { statusName: 'transactionPending' },
handleSubmit: mockHandleSubmit,
});
render(<SwapButton />);
const button = screen.getByTestId('ockSwapButton_Button');
expect(screen.getByTestId('spinner')).toBeInTheDocument();
expect(button).toBeDisabled();
});

it('should render Spinner when transaction is approved', () => {
useSwapContextMock.mockReturnValue({
to: { loading: false, amount: 1, token: 'ETH' },
from: { loading: false, amount: 1, token: 'BTC' },
lifeCycleStatus: { statusName: 'transactionApproved' },
handleSubmit: mockHandleSubmit,
});
render(<SwapButton />);
Expand All @@ -57,7 +83,7 @@ describe('SwapButton', () => {
useSwapContextMock.mockReturnValue({
to: { loading: false, amount: 1, token: 'ETH' },
from: { loading: false, amount: null, token: 'BTC' },
loading: false,
lifeCycleStatus: { statusName: 'init' },
handleSubmit: mockHandleSubmit,
});
render(<SwapButton />);
Expand All @@ -70,7 +96,7 @@ describe('SwapButton', () => {
address: '0x123',
to: { loading: false, amount: 1, token: 'ETH' },
from: { loading: false, amount: 1, token: 'BTC' },
loading: false,
lifeCycleStatus: { statusName: 'init' },
handleSubmit: mockHandleSubmit,
});
render(<SwapButton />);
Expand All @@ -84,7 +110,7 @@ describe('SwapButton', () => {
address: '0x123',
to: { loading: false, amount: 1, token: 'ETH' },
from: { loading: false, amount: 1, token: 'BTC' },
loading: false,
lifeCycleStatus: { statusName: 'init' },
handleSubmit: mockHandleSubmit,
});
const customClass = 'custom-class';
Expand All @@ -97,7 +123,7 @@ describe('SwapButton', () => {
useSwapContextMock.mockReturnValue({
to: { loading: false, amount: 1, token: 'ETH' },
from: { loading: false, amount: 1, token: 'BTC' },
loading: false,
lifeCycleStatus: { statusName: 'init' },
handleSubmit: mockHandleSubmit,
});
vi.mocked(useAccount).mockReturnValue({
Expand Down
85 changes: 16 additions & 69 deletions src/swap/components/SwapProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const TestSwapComponent = () => {
context.to.setToken(DEGEN_TOKEN);
}, [context]);
const handleStatusError = async () => {
context.setLifeCycleStatus({
context.updateLifeCycleStatus({
statusName: 'error',
statusData: {
code: 'code',
Expand All @@ -126,7 +126,7 @@ const TestSwapComponent = () => {
});
};
const handleStatusAmountChange = async () => {
context.setLifeCycleStatus({
context.updateLifeCycleStatus({
statusName: 'amountChange',
statusData: {
amountFrom: '',
Expand All @@ -138,7 +138,7 @@ const TestSwapComponent = () => {
});
};
const handleStatusTransactionPending = async () => {
context.setLifeCycleStatus({
context.updateLifeCycleStatus({
statusName: 'transactionPending',
statusData: {
// LifecycleStatus shared data
Expand All @@ -148,7 +148,7 @@ const TestSwapComponent = () => {
});
};
const handleStatusTransactionApproved = async () => {
context.setLifeCycleStatus({
context.updateLifeCycleStatus({
statusName: 'transactionApproved',
statusData: {
transactionHash: '0x123',
Expand All @@ -160,10 +160,10 @@ const TestSwapComponent = () => {
});
};
const handleStatusSuccess = async () => {
context.setLifeCycleStatus({
context.updateLifeCycleStatus({
statusName: 'success',
statusData: {
receipt: ['0x123'],
transactionReceipt: { transactionHash: '0x123' },
// LifecycleStatus shared data
isMissingRequiredField: false,
maxSlippage: 3,
Expand Down Expand Up @@ -258,48 +258,13 @@ describe('SwapProvider', () => {
});
});

it('should call setError when setLifeCycleStatus is called with error', async () => {
const { result } = renderHook(() => useSwapContext(), { wrapper });
const errorStatusData = {
code: 'code',
error: 'error_long_messages',
message: 'test',
// LifecycleStatus shared data
isMissingRequiredField: false,
maxSlippage: 3,
};
await act(async () => {
result.current.setLifeCycleStatus({
statusName: 'error',
statusData: errorStatusData,
});
});
expect(result.current.error).toBe(errorStatusData);
});

it('should call setError with undefined when setLifeCycleStatus is called with success', async () => {
const { result } = renderHook(() => useSwapContext(), { wrapper });
await act(async () => {
result.current.setLifeCycleStatus({
statusName: 'success',
statusData: {
receipt: ['0x123'],
// LifecycleStatus shared data
isMissingRequiredField: false,
maxSlippage: 5,
},
});
});
expect(result.current.error).toBeUndefined();
});

it('should reset inputs when setLifeCycleStatus is called with success', async () => {
const { result } = renderHook(() => useSwapContext(), { wrapper });
await act(async () => {
result.current.setLifeCycleStatus({
result.current.updateLifeCycleStatus({
statusName: 'success',
statusData: {
transactionReceipt: '0x123',
transactionReceipt: { transactionHash: '0x123' },
// LifecycleStatus shared data
isMissingRequiredField: false,
maxSlippage: 5,
Expand Down Expand Up @@ -456,10 +421,10 @@ describe('SwapProvider', () => {
expect(onStatusMock).toHaveBeenCalledWith(
expect.objectContaining({
statusName: 'init',
statusData: expect.objectContaining({
isMissingRequiredField: false,
maxSlippage: 3,
}),
statusData: {
isMissingRequiredField: true,
maxSlippage: 10,
},
}),
);
});
Expand Down Expand Up @@ -594,15 +559,6 @@ describe('SwapProvider', () => {
expect(result.current.to.amount).toBe('10');
});

it('should handle submit with missing data', async () => {
const { result } = renderHook(() => useSwapContext(), { wrapper });
await act(async () => {
result.current.handleSubmit();
});
expect(result.current.error).toBeUndefined();
expect(result.current.loading).toBe(false);
});

it('should update amount and trigger quote', async () => {
const { result } = renderHook(() => useSwapContext(), { wrapper });
await act(async () => {
Expand Down Expand Up @@ -647,14 +603,11 @@ describe('SwapProvider', () => {
});
expect(result.current.lifeCycleStatus).toEqual({
statusName: 'error',
statusData: {
statusData: expect.objectContaining({
code: 'TmSPc01',
error: JSON.stringify(mockError),
message: '',
// LifecycleStatus shared data
isMissingRequiredField: true,
maxSlippage: 5,
},
}),
});
});

Expand All @@ -670,14 +623,11 @@ describe('SwapProvider', () => {
});
expect(result.current.lifeCycleStatus).toEqual({
statusName: 'error',
statusData: {
statusData: expect.objectContaining({
code: 'UNCAUGHT_SWAP_QUOTE_ERROR',
error: 'Something went wrong',
message: '',
// LifecycleStatus shared data
isMissingRequiredField: true,
maxSlippage: 5,
},
}),
});
});

Expand Down Expand Up @@ -730,9 +680,6 @@ describe('SwapProvider', () => {
code: getSwapErrorCode('uncaught-swap'),
error: 'Something went wrong',
message: '',
// LifecycleStatus shared data
isMissingRequiredField: false,
maxSlippage: 3,
});
renderWithProviders({ Component: TestSwapComponent });
fireEvent.click(screen.getByText('Swap'));
Expand Down
6 changes: 1 addition & 5 deletions src/swap/components/SwapSettingsSlippageInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let mockLifeCycleStatus = {

vi.mock('./SwapProvider', () => ({
useSwapContext: () => ({
setLifeCycleStatus: mockSetLifeCycleStatus,
updateLifeCycleStatus: mockSetLifeCycleStatus,
lifeCycleStatus: mockLifeCycleStatus,
}),
}));
Expand Down Expand Up @@ -63,7 +63,6 @@ describe('SwapSettingsSlippageInput', () => {
expect(mockSetLifeCycleStatus).toHaveBeenCalledWith({
statusName: 'slippageChange',
statusData: {
isMissingRequiredField: false,
maxSlippage: 2.5,
},
});
Expand All @@ -88,7 +87,6 @@ describe('SwapSettingsSlippageInput', () => {
expect(mockSetLifeCycleStatus).toHaveBeenCalledWith({
statusName: 'slippageChange',
statusData: {
isMissingRequiredField: false,
maxSlippage: 1.5,
},
});
Expand All @@ -112,7 +110,6 @@ describe('SwapSettingsSlippageInput', () => {
expect(mockSetLifeCycleStatus).toHaveBeenCalledWith({
statusName: 'slippageChange',
statusData: {
isMissingRequiredField: false,
maxSlippage: 2.75,
},
});
Expand Down Expand Up @@ -195,7 +192,6 @@ describe('SwapSettingsSlippageInput', () => {
expect(mockSetLifeCycleStatus).toHaveBeenLastCalledWith({
statusName: 'slippageChange',
statusData: {
isMissingRequiredField: false,
maxSlippage: 3,
},
});
Expand Down
Loading

0 comments on commit 5a8edd8

Please sign in to comment.