|
| 1 | +import { act } from 'react'; |
| 2 | +import { createRoot } from 'react-dom/client'; |
| 3 | +import type { Root } from 'react-dom/client'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { signOut } from 'next-auth/react'; |
| 6 | +import { DeleteAccountSettings } from '@/components/settings/delete-account-settings'; |
| 7 | +import { LanguageProvider } from '@/contexts/LanguageContext'; |
| 8 | +import { apiClient, ApiError } from '@/lib/api-client'; |
| 9 | +import { toast } from '@/lib/feedback'; |
| 10 | + |
| 11 | +vi.mock('next-auth/react', () => ({ signOut: vi.fn() })); |
| 12 | +vi.mock('@/lib/api-client', async (importOriginal) => { |
| 13 | + const actual = await importOriginal<typeof import('@/lib/api-client')>(); |
| 14 | + return { ...actual, apiClient: { delete: vi.fn() } }; |
| 15 | +}); |
| 16 | +vi.mock('@/lib/frontend-logger', () => ({ frontendLogger: { error: vi.fn() } })); |
| 17 | +vi.mock('@/lib/feedback', () => ({ toast: { success: vi.fn(), error: vi.fn() } })); |
| 18 | + |
| 19 | +let container: HTMLDivElement; |
| 20 | +let root: Root; |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + vi.clearAllMocks(); |
| 24 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true); |
| 25 | + container = document.createElement('div'); |
| 26 | + document.body.appendChild(container); |
| 27 | + root = createRoot(container); |
| 28 | +}); |
| 29 | + |
| 30 | +afterEach(async () => { |
| 31 | + await act(async () => root.unmount()); |
| 32 | + container.remove(); |
| 33 | + vi.unstubAllGlobals(); |
| 34 | +}); |
| 35 | + |
| 36 | +async function render(isAdmin: boolean) { |
| 37 | + await act(async () => root.render(<LanguageProvider><DeleteAccountSettings isAdmin={isAdmin} /></LanguageProvider>)); |
| 38 | +} |
| 39 | + |
| 40 | +/** 打开确认弹窗、输入密码并提交 */ |
| 41 | +async function submitWithPassword(password: string) { |
| 42 | + await act(async () => container.querySelector<HTMLButtonElement>('button')!.click()); |
| 43 | + const input = document.querySelector<HTMLInputElement>('#delete-account-password')!; |
| 44 | + const setValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!; |
| 45 | + await act(async () => { |
| 46 | + setValue.call(input, password); |
| 47 | + input.dispatchEvent(new Event('input', { bubbles: true })); |
| 48 | + }); |
| 49 | + await act(async () => { |
| 50 | + input.form!.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })); |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +describe('注销账号设置', () => { |
| 55 | + it('管理员的注销按钮不可用,并说明原因', async () => { |
| 56 | + await render(true); |
| 57 | + |
| 58 | + expect(container.querySelector<HTMLButtonElement>('button')!.disabled).toBe(true); |
| 59 | + expect(container.textContent).toContain('管理员账号不能自助注销'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('普通用户输入密码后注销并退出登录', async () => { |
| 63 | + vi.mocked(apiClient.delete).mockResolvedValue({ success: true }); |
| 64 | + await render(false); |
| 65 | + |
| 66 | + await submitWithPassword('secret1'); |
| 67 | + |
| 68 | + expect(apiClient.delete).toHaveBeenCalledWith('/api/user', { body: JSON.stringify({ password: 'secret1' }) }); |
| 69 | + expect(signOut).toHaveBeenCalledWith({ callbackUrl: '/login' }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('密码错误时提示并保持登录', async () => { |
| 73 | + vi.mocked(apiClient.delete).mockRejectedValue(new ApiError(400, 'Bad Request', { message: 'Incorrect password' })); |
| 74 | + await render(false); |
| 75 | + |
| 76 | + await submitWithPassword('wrong'); |
| 77 | + |
| 78 | + expect(toast.error).toHaveBeenCalledWith('密码错误'); |
| 79 | + expect(signOut).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | +}); |
0 commit comments