import React from 'react'; import { fireEvent, render, screen, within } from '@testing-library/react'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { setLocale } from '../src/i18n'; import GeneralSettingsPanel from '../src/pages/Setting/components/GeneralSettingsPanel'; import type { GatewaySettingState } from '../src/pages/Setting/useGatewaySettingState'; import type { SettingUpdateState } from '../src/pages/Setting/useSettingUpdateState'; function createGatewayState(): GatewaySettingState { return { status: 'connected', loading: false, showLogs: false, gatewayAutoStart: true, gateway: { ok: true, status: 'connected', initialized: true, mode: null, port: 18789, pid: null, }, logContent: '', statusLoading: false, logLoading: false, restarting: false, autoStartUpdating: false, error: null, lastFetchedAt: null, refreshStatus: async () => {}, restartGateway: async () => {}, toggleLogs: async () => {}, loadLogs: async () => {}, viewLogs: async () => {}, setGatewayAutoStart: async () => {}, }; } function createUpdateState(): SettingUpdateState { return { status: 'idle', currentVersion: '1.0.0', updateInfo: null, progress: null, error: null, autoCheckUpdate: true, autoDownloadUpdate: false, checkUpdate: async () => {}, downloadUpdate: async () => {}, installUpdate: async () => {}, setAutoCheckUpdate: () => {}, setAutoDownloadUpdate: () => {}, }; } describe('GeneralSettingsPanel', () => { beforeEach(() => { setLocale('zh'); }); it('renders the launch-at-startup setting after language and before gateway', () => { render( {}} onLanguageChange={async () => {}} onLaunchAtStartupChange={async () => {}} gatewayState={createGatewayState()} updateState={createUpdateState()} />, ); const languageLabel = screen.getByText('语言'); const launchLabel = screen.getByText('开机自动启动'); const gatewayLabel = screen.getByText('网关'); expect(languageLabel.compareDocumentPosition(launchLabel) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy(); expect(launchLabel.compareDocumentPosition(gatewayLabel) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy(); }); it('calls the launch-at-startup handler when the switch is toggled', () => { const handleLaunchAtStartupChange = vi.fn(); render( {}} onLanguageChange={async () => {}} onLaunchAtStartupChange={handleLaunchAtStartupChange} gatewayState={createGatewayState()} updateState={createUpdateState()} />, ); const launchLabel = screen.getByText('开机自动启动'); const launchRow = launchLabel.parentElement?.parentElement; expect(launchRow).toBeTruthy(); const launchSwitch = within(launchRow as HTMLElement).getByRole('switch'); fireEvent.click(launchSwitch); expect(handleLaunchAtStartupChange).toHaveBeenCalledWith(true); }); });