mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-28 05:46:36 +08:00
f5c9da03f4
Result: {"status":"keep","total_issues":8,"golint_canonicalheader":0,"golint_errname":0,"golint_errorlint":1,"golint_exhaustive":0,"golint_forcetypeassert":0,"golint_gosec":0,"golint_intrange":0,"golint_modernize":3,"golint_nilnil":3,"golint_perfsprint":0,"golint_prealloc":0,"golint_recvcheck":1,"golint_usestdlibvars":0,"golint_wastedassign":0,"golint_total":8,"golint_test_testifylint":0,"golint_test_thelper":0,"golint_test_usetesting":0,"golint_test_total":0,"golint_vetx_total":0,"eslint_problems":0,"eslint_errors":0,"eslint_warnings":0,"tsc_errors":0,"measure_s":39}
110 lines
2.9 KiB
TypeScript
110 lines
2.9 KiB
TypeScript
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;
|
|
}) => <a href={href}>{children}</a>,
|
|
}));
|
|
|
|
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(
|
|
<NextIntlClientProvider locale='zh-CN' messages={zhCN} timeZone='Asia/Shanghai'>
|
|
<QueryClientProvider client={client}>{ui}</QueryClientProvider>
|
|
</NextIntlClientProvider>,
|
|
);
|
|
}
|
|
|
|
describe('ZoneDomainSelector', () => {
|
|
it('renders domains and toggles selection', async () => {
|
|
const user = userEvent.setup();
|
|
const onChange = vi.fn();
|
|
|
|
renderSelector(
|
|
<ZoneDomainSelector
|
|
value={[7]}
|
|
onChange={onChange}
|
|
domains={domains}
|
|
zones={zones}
|
|
/>,
|
|
);
|
|
|
|
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(
|
|
<ZoneDomainSelector
|
|
value={[]}
|
|
onChange={vi.fn()}
|
|
domains={domains}
|
|
zones={zones}
|
|
currentRouteId={1}
|
|
/>,
|
|
);
|
|
|
|
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(
|
|
<ZoneDomainSelector
|
|
value={[8]}
|
|
onChange={vi.fn()}
|
|
domains={domains}
|
|
zones={zones}
|
|
currentRouteId={99}
|
|
/>,
|
|
);
|
|
|
|
expect(screen.getByText('bound.example.com')).toBeVisible();
|
|
expect(screen.getByText('api.example.com')).toBeVisible();
|
|
});
|
|
});
|