import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, expect, it, vi } from 'vitest';
import { ZoneDomainSelector } from '@/app/(main)/proxy-routes/components/zone-domain-selector';
import type { ZoneDomainItem, ZoneItem } from '@/lib/services/openflare';
import { NextIntlClientProvider } from 'next-intl';
import zhCN from '@/messages/zh-CN.json';
vi.mock('next/link', () => ({
default: ({
children,
href,
}: {
children: React.ReactNode;
href: string;
}) => {children},
}));
const zones: ZoneItem[] = [
{ id: 1, domain: 'example.com', created_at: '', updated_at: '' },
];
const domains: ZoneDomainItem[] = [
{
id: 7,
zone_id: 1,
proxy_route_id: null,
domain: 'api.example.com',
cert_id: 9,
created_at: '',
updated_at: '',
},
{
id: 8,
zone_id: 1,
proxy_route_id: 99,
domain: 'bound.example.com',
cert_id: null,
created_at: '',
updated_at: '',
},
];
function renderSelector(ui: React.ReactElement) {
const client = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
return render(
{ui}
,
);
}
describe('ZoneDomainSelector', () => {
it('renders domains and toggles selection', async () => {
const user = userEvent.setup();
const onChange = vi.fn();
renderSelector(
,
);
expect(screen.getByText('api.example.com')).toBeVisible();
// Bound to another route — hidden by default
expect(screen.queryByText('bound.example.com')).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: /快捷新增域名/ })).toBeVisible();
await user.click(screen.getByText('api.example.com'));
expect(onChange).toHaveBeenCalledWith([]);
});
it('hides domains bound to another route', () => {
renderSelector(
,
);
expect(screen.getByText('api.example.com')).toBeVisible();
expect(screen.queryByText('bound.example.com')).not.toBeInTheDocument();
});
it('still shows domains already bound to the current route', () => {
renderSelector(
,
);
expect(screen.getByText('bound.example.com')).toBeVisible();
expect(screen.getByText('api.example.com')).toBeVisible();
});
});