Skip to content

Commit

Permalink
fix: move chain selection test to ibc test file
Browse files Browse the repository at this point in the history
  • Loading branch information
chalabi2 committed Jan 14, 2025
1 parent 6562154 commit 915a268
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 29 deletions.
57 changes: 28 additions & 29 deletions components/bank/components/__tests__/sendBox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,40 @@ mock.module('next/image', () => ({
},
}));

// Add this mock at the top of your test file
mock.module('../forms/ibcSendForm', () => ({
default: (props: any) => {
if (props.isBalancesLoading) {
return <div>Loading...</div>;
}
return (
<div data-testid="ibc-send-form">
<div className="dropdown">
<label tabIndex={0} aria-label="from-chain-selector" className="btn">
{props.selectedFromChain || 'Select Chain'}
</label>
</div>
<div className="dropdown">
<label tabIndex={0} aria-label="to-chain-selector" className="btn">
{props.selectedToChain || 'Select Chain'}
</label>
</div>
</div>
);
},
}));

const renderWithProps = (props = {}) => {
const defaultProps = {
address: 'test_address',
balances: mockBalances,
isBalancesLoading: false,
refetchBalances: () => {},
refetchHistory: () => {},
osmosisBalances: [],
isOsmosisBalancesLoading: false,
refetchOsmosisBalances: () => {},
resolveOsmosisRefetch: () => {},
};
return renderWithChainProvider(<SendBox {...defaultProps} {...props} />);
};
Expand Down Expand Up @@ -71,33 +99,4 @@ describe('SendBox', () => {
expect(screen.getByLabelText('to-chain-selector')).toBeInTheDocument();
});
});

test('selects chains in Cross-Chain Transfer mode', async () => {
renderWithProps();
fireEvent.click(screen.getByLabelText('cross-chain-transfer-tab'));

// Select destination chain
await waitFor(() => {
const toChainSelector = screen.getByLabelText('to-chain-selector');
expect(toChainSelector).toBeInTheDocument();
fireEvent.click(toChainSelector);
});

// Select Osmosis as destination
await waitFor(() => {
const toChainDropdown = screen.getByLabelText('to-chain-selector').closest('.dropdown');
expect(toChainDropdown).toBeInTheDocument();

// Find and click the Osmosis option within the dropdown
const osmosisOption = within(toChainDropdown!).getByText('Osmosis');
fireEvent.click(osmosisOption);
});

// Verify selection using text content instead of complex matchers
await waitFor(() => {
const selectedChain = screen.getByLabelText('to-chain-selector');
const chainText = selectedChain.textContent;
expect(chainText?.includes('Osmosis')).toBe(true);
});
});
});
54 changes: 54 additions & 0 deletions components/bank/forms/__tests__/ibcSendForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ function renderWithProps(props = {}) {
isIbcTransfer: true,
setIsIbcTransfer: jest.fn(),
ibcChains: [
{
id: 'manifest',
name: 'Manifest',
icon: 'https://osmosis.zone/assets/icons/osmo-logo-icon.svg',
prefix: 'manifest',
},
{
id: 'osmosistestnet',
name: 'Osmosis',
Expand All @@ -35,6 +41,7 @@ function renderWithProps(props = {}) {
],
selectedChain: 'osmosistestnet',
setSelectedChain: jest.fn(),
setSelectedFromChain: jest.fn(),
};

return renderWithChainProvider(<IbcSendForm {...defaultProps} {...props} />);
Expand Down Expand Up @@ -107,4 +114,51 @@ describe('IbcSendForm Component', () => {
const sendButton = screen.getByRole('button', { name: 'send-btn' });
expect(sendButton).not.toBeDisabled();
});

test('handles chain selection correctly', async () => {
renderWithProps();

// Test from-chain selection
const fromChainSelector = screen.getByLabelText('from-chain-selector');
expect(fromChainSelector).toBeInTheDocument();
fireEvent.click(fromChainSelector);

// Find and click Manifest option
const manifestOption = screen.getByText('Manifest');
fireEvent.click(manifestOption);

// Instead of checking the content directly, check for the presence of elements
const manifestIcon = screen.getAllByAltText('Manifest')[0];
expect(manifestIcon).toBeInTheDocument();

// Test to-chain selection
const toChainSelector = screen.getByLabelText('to-chain-selector');
expect(toChainSelector).toBeInTheDocument();
fireEvent.click(toChainSelector);

// Find and click Osmosis option
const osmosisOption = screen.getByText('Osmosis');
fireEvent.click(osmosisOption);

// Check for Osmosis icon instead of content
const osmosisIcon = screen.getAllByAltText('Osmosis')[0];
expect(osmosisIcon).toBeInTheDocument();
});

test('prevents selecting same chain for source and destination', async () => {
renderWithProps();

// Select Manifest as source chain
const fromChainSelector = screen.getByLabelText('from-chain-selector');
fireEvent.click(fromChainSelector);
fireEvent.click(screen.getByText('Manifest'));

// Verify Manifest is not available in destination chain options
const toChainSelector = screen.getByLabelText('to-chain-selector');
fireEvent.click(toChainSelector);

// The dropdown for destination chain should not show Manifest
const manifestOptions = screen.getAllByText('Manifest');
expect(manifestOptions.length).toBe(1); // Only the source chain should show Manifest
});
});

0 comments on commit 915a268

Please sign in to comment.