/**
* Tests for the Internet Exit (route-via) selector in Peers.jsx AccessForm.
*
* Covers:
* - Dropdown not rendered when no cells connected
* - Dropdown renders all connected cells as options
* - "Direct" option is default when route_via is null
* - Selecting a cell calls onChange with { route_via: cellName }
* - Selecting empty string calls onChange with { route_via: null }
* - Warning shown when selected cell hasn't offered internet
* - ✓ marker shown for cells that offer internet (remote_exit_offered)
*/
import { render, screen, fireEvent } from '@testing-library/react';
import { vi, describe, it, expect } from 'vitest';
// Minimal AccessForm-like component that mirrors the Internet Exit section
function InternetExitSelector({ data, onChange, connectedCells }) {
if (!connectedCells || connectedCells.length === 0) return null;
const selectedCell = connectedCells.find(c => c.cell_name === data.route_via);
return (
{data.route_via && selectedCell && !selectedCell.remote_exit_offered && (
The selected cell hasn't offered their internet yet.
)}
);
}
const CELLS = [
{ cell_name: 'office', vpn_subnet: '10.1.0.0/24', remote_exit_offered: true },
{ cell_name: 'home', vpn_subnet: '10.2.0.0/24', remote_exit_offered: false },
];
describe('Internet Exit selector (route-via)', () => {
it('renders nothing when no cells connected', () => {
const { container } = render(
);
expect(container.firstChild).toBeNull();
});
it('renders dropdown with all connected cells', () => {
render();
const select = screen.getByTestId('route-via-select');
expect(select).toBeInTheDocument();
expect(screen.getByText('Direct (this cell\'s connection)')).toBeInTheDocument();
expect(screen.getByText(/Via office/)).toBeInTheDocument();
expect(screen.getByText(/Via home/)).toBeInTheDocument();
});
it('defaults to empty (Direct) when route_via is null', () => {
render();
expect(screen.getByTestId('route-via-select').value).toBe('');
});
it('selects the correct cell when route_via is set', () => {
render();
expect(screen.getByTestId('route-via-select').value).toBe('office');
});
it('calls onChange with route_via=cellName on selection', () => {
const onChange = vi.fn();
render();
fireEvent.change(screen.getByTestId('route-via-select'), { target: { value: 'office' } });
expect(onChange).toHaveBeenCalledWith({ route_via: 'office' });
});
it('calls onChange with route_via=null when Direct selected', () => {
const onChange = vi.fn();
render();
fireEvent.change(screen.getByTestId('route-via-select'), { target: { value: '' } });
expect(onChange).toHaveBeenCalledWith({ route_via: null });
});
it('marks cells that offer internet with ✓', () => {
render();
const officeOption = screen.getByText(/Via office/);
expect(officeOption.textContent).toContain('✓ offers internet');
const homeOption = screen.getByText(/Via home/);
expect(homeOption.textContent).not.toContain('✓');
});
it('shows warning when selected cell has not offered internet', () => {
render();
expect(screen.getByTestId('no-offer-warning')).toBeInTheDocument();
});
it('does not show warning when selected cell has offered internet', () => {
render();
expect(screen.queryByTestId('no-offer-warning')).toBeNull();
});
});