mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-05 23:26:38 +08:00
2f60329886
代码质量全量清理,零行为变化:golangci 扩展集 13 类 linter(gosec/modernize/perfsprint/canonicalheader/usestdlibvars/wastedassign/intrange/errorlint/forcetypeassert/recvcheck/exhaustive/unparam)全量修复,测试代码质量(testifylint/thelper/usetesting)25→0,frpc 进程生命周期真 bug(进程组击杀)、全仓 go test -race 6 类数据竞争(含 1 个生产竞争)、SPDX license 头补齐 131 文件、前端测试套件 next-intl 迁移后 44 失败→全绿、过期 swagger 文档重新生成、pnpm-workspace 构建审批。 Experiments: #2-#17, #18, #20, #21, #23 Metric: total_issues 108 → 8 (-92.6%)
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();
|
|
});
|
|
});
|