134 lines
3.4 KiB
TypeScript
134 lines
3.4 KiB
TypeScript
/**
|
|
* @jest-environment jsdom
|
|
*/
|
|
|
|
import React from 'react'
|
|
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
|
|
import '@testing-library/jest-dom'
|
|
import ChangePasswordModal from '../ChangePasswordModal'
|
|
|
|
const mockAlert = jest.fn()
|
|
const mockFetch = jest.fn()
|
|
|
|
jest.mock('@/components/ui/DialogProvider', () => ({
|
|
useDialog: () => ({
|
|
alert: mockAlert,
|
|
}),
|
|
}))
|
|
|
|
jest.mock('next-auth/react', () => ({
|
|
useSession: () => ({
|
|
update: jest.fn(),
|
|
}),
|
|
}))
|
|
|
|
jest.mock('@/lib/ui-styles', () => ({
|
|
cn: (...classes: Array<string | false | null | undefined>) => classes.filter(Boolean).join(' '),
|
|
useThemeStyles: () => ({
|
|
buttonModifiers: {
|
|
block: 'w-full',
|
|
compactXs: 'px-2 py-1',
|
|
},
|
|
buttonStyles: {
|
|
primary: 'primary',
|
|
secondary: 'secondary',
|
|
ghost: 'ghost',
|
|
danger: 'danger',
|
|
success: 'success',
|
|
},
|
|
inputModifiers: {
|
|
compactXs: 'px-2 py-1',
|
|
},
|
|
inputStyles: {
|
|
base: 'input',
|
|
error: 'input-error',
|
|
},
|
|
modalStyles: {
|
|
overlay: 'overlay',
|
|
container: 'container',
|
|
content: 'content',
|
|
header: 'header',
|
|
title: 'title',
|
|
closeButton: 'close',
|
|
body: 'body',
|
|
footer: 'footer',
|
|
sizes: {
|
|
xs: 'xs',
|
|
small: 'small',
|
|
medium: 'medium',
|
|
large: 'large',
|
|
xlarge: 'xlarge',
|
|
xxlarge: 'xxlarge',
|
|
fullscreen: 'fullscreen',
|
|
},
|
|
},
|
|
}),
|
|
}))
|
|
|
|
global.fetch = mockFetch
|
|
|
|
describe('ChangePasswordModal', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve({ success: true }),
|
|
})
|
|
})
|
|
|
|
it('强制改密时显示独立账号安全页,不显示旧密码和取消入口', () => {
|
|
render(
|
|
<ChangePasswordModal
|
|
isOpen
|
|
isRequired
|
|
onClose={jest.fn()}
|
|
/>
|
|
)
|
|
|
|
expect(screen.getByText('账号安全')).toBeInTheDocument()
|
|
expect(screen.getByRole('heading', { name: '设置新密码' })).toBeInTheDocument()
|
|
expect(screen.getByText('首次登录需要先更换初始密码,完成后才能进入素材库。')).toBeInTheDocument()
|
|
expect(screen.queryByText('旧密码')).not.toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: '取消' })).not.toBeInTheDocument()
|
|
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('强制改密提交时只发送新密码并回调成功', async () => {
|
|
const onSuccess = jest.fn()
|
|
|
|
render(
|
|
<ChangePasswordModal
|
|
isOpen
|
|
isRequired
|
|
onClose={jest.fn()}
|
|
onSuccess={onSuccess}
|
|
/>
|
|
)
|
|
|
|
fireEvent.change(screen.getByPlaceholderText('请输入新密码'), {
|
|
target: { value: 'StrongPass123!' },
|
|
})
|
|
fireEvent.change(screen.getByPlaceholderText('请再次输入新密码'), {
|
|
target: { value: 'StrongPass123!' },
|
|
})
|
|
fireEvent.click(screen.getByRole('button', { name: '确认修改' }))
|
|
|
|
await waitFor(() => {
|
|
expect(mockFetch).toHaveBeenCalledWith('/api/user/change-password', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ newPassword: 'StrongPass123!' }),
|
|
})
|
|
})
|
|
|
|
expect(onSuccess).toHaveBeenCalled()
|
|
expect(mockAlert).toHaveBeenCalledWith({
|
|
title: '成功',
|
|
message: '密码修改成功!',
|
|
variant: 'success',
|
|
})
|
|
})
|
|
})
|