fix lint and type error
This commit is contained in:
@@ -41,6 +41,11 @@ vi.mock('@/components/Brand.vue', () => ({
|
||||
default: { template: '<div class="brand-stub" />' },
|
||||
}))
|
||||
|
||||
function getSetupState(w: ReturnType<typeof mount>) {
|
||||
// @ts-expect-error setupState is Vue internal, not in public types
|
||||
return w.getCurrentComponent().setupState
|
||||
}
|
||||
|
||||
describe('Login Page', () => {
|
||||
let wrapper: ReturnType<typeof mount>
|
||||
|
||||
@@ -89,7 +94,7 @@ describe('Login Page', () => {
|
||||
async function submitLogin(
|
||||
fields: { username: string; password: string; remember?: boolean },
|
||||
) {
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.formState.username = fields.username
|
||||
vm.formState.password = fields.password
|
||||
if (fields.remember !== undefined) vm.formState.remember = fields.remember
|
||||
@@ -188,7 +193,7 @@ describe('Login Page', () => {
|
||||
it('shows error message on ApiError', async () => {
|
||||
const { api } = await import('@/utils/request')
|
||||
vi.mocked(api.post).mockRejectedValueOnce(new ApiError('用户名或密码错误', 401))
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as any)
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
await submitLogin({ username: 'user', password: 'wrong' })
|
||||
@@ -200,7 +205,7 @@ describe('Login Page', () => {
|
||||
it('shows network error message on generic error', async () => {
|
||||
const { api } = await import('@/utils/request')
|
||||
vi.mocked(api.post).mockRejectedValueOnce(new Error('Network failure'))
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as any)
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
await submitLogin({ username: 'user', password: 'pass' })
|
||||
@@ -211,7 +216,7 @@ describe('Login Page', () => {
|
||||
|
||||
it('navigates to register page', async () => {
|
||||
await mountPage()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.goToRegister()
|
||||
|
||||
expect(mockPush).toHaveBeenCalledWith('/register')
|
||||
|
||||
@@ -39,6 +39,11 @@ vi.mock('@/components/Brand.vue', () => ({
|
||||
default: { template: '<div class="brand-stub" />' },
|
||||
}))
|
||||
|
||||
function getSetupState(w: ReturnType<typeof mount>) {
|
||||
// @ts-expect-error setupState is Vue internal, not in public types
|
||||
return w.getCurrentComponent().setupState
|
||||
}
|
||||
|
||||
describe('Register Page', () => {
|
||||
let wrapper: ReturnType<typeof mount>
|
||||
|
||||
@@ -88,11 +93,11 @@ describe('Register Page', () => {
|
||||
it('calls /api/v1/register and redirects to /login on success without token', async () => {
|
||||
const { api } = await import('@/utils/request')
|
||||
vi.mocked(api.post).mockResolvedValueOnce({})
|
||||
const successSpy = vi.spyOn(message, 'success').mockImplementation(() => ({}) as any)
|
||||
const successSpy = vi.spyOn(message, 'success').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
const store = useUserStore()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.formState.username = 'newuser'
|
||||
vm.formState.email = 'new@test.com'
|
||||
vm.formState.password = 'password123'
|
||||
@@ -122,11 +127,11 @@ describe('Register Page', () => {
|
||||
user: { id: 2, username: 'newuser', email: 'new@test.com', role: 'user', status: 1 },
|
||||
}
|
||||
vi.mocked(api.post).mockResolvedValueOnce(mockResponse)
|
||||
vi.spyOn(message, 'success').mockImplementation(() => ({}) as any)
|
||||
vi.spyOn(message, 'success').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
const store = useUserStore()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.formState.username = 'newuser'
|
||||
vm.formState.email = 'new@test.com'
|
||||
vm.formState.password = 'password123'
|
||||
@@ -145,10 +150,10 @@ describe('Register Page', () => {
|
||||
it('shows error message on ApiError', async () => {
|
||||
const { api } = await import('@/utils/request')
|
||||
vi.mocked(api.post).mockRejectedValueOnce(new ApiError('用户名已存在', 409))
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as any)
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.formState.username = 'existing'
|
||||
vm.formState.email = 'e@test.com'
|
||||
vm.formState.password = 'password123'
|
||||
@@ -164,10 +169,10 @@ describe('Register Page', () => {
|
||||
it('shows network error message on generic error', async () => {
|
||||
const { api } = await import('@/utils/request')
|
||||
vi.mocked(api.post).mockRejectedValueOnce(new Error('Network failure'))
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as any)
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.formState.username = 'user'
|
||||
vm.formState.email = 'u@test.com'
|
||||
vm.formState.password = 'password123'
|
||||
@@ -182,7 +187,7 @@ describe('Register Page', () => {
|
||||
|
||||
it('navigates to login page', async () => {
|
||||
await mountPage()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.goToLogin()
|
||||
|
||||
expect(mockPush).toHaveBeenCalledWith('/login')
|
||||
|
||||
@@ -33,6 +33,11 @@ vi.mock('vue-router', () => ({
|
||||
useRoute: () => ({ path: '/profile' }),
|
||||
}))
|
||||
|
||||
function getSetupState(w: ReturnType<typeof mount>) {
|
||||
// @ts-expect-error setupState is Vue internal, not in public types
|
||||
return w.getCurrentComponent().setupState
|
||||
}
|
||||
|
||||
describe('Profile Page', () => {
|
||||
let wrapper: ReturnType<typeof mount>
|
||||
|
||||
@@ -74,7 +79,7 @@ describe('Profile Page', () => {
|
||||
|
||||
it('pre-fills email form field', async () => {
|
||||
await mountPage()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
|
||||
expect(vm.formState.email).toBe('test@example.com')
|
||||
})
|
||||
@@ -86,7 +91,7 @@ describe('Profile Page', () => {
|
||||
const successSpy = vi.spyOn(message, 'success').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.formState.email = 'new@example.com'
|
||||
vm.formRef = { validate: vi.fn().mockResolvedValue(true) }
|
||||
await vm.handleSubmit()
|
||||
@@ -103,7 +108,7 @@ describe('Profile Page', () => {
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.formRef = { validate: vi.fn().mockResolvedValue(true) }
|
||||
await vm.handleSubmit()
|
||||
await flushPromises()
|
||||
@@ -118,7 +123,7 @@ describe('Profile Page', () => {
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.formRef = { validate: vi.fn().mockResolvedValue(true) }
|
||||
await vm.handleSubmit()
|
||||
await flushPromises()
|
||||
|
||||
@@ -35,6 +35,11 @@ vi.mock('vue-router', () => ({
|
||||
useRoute: () => ({ path: '/profile/password' }),
|
||||
}))
|
||||
|
||||
function getSetupState(w: ReturnType<typeof mount>) {
|
||||
// @ts-expect-error setupState is Vue internal, not in public types
|
||||
return w.getCurrentComponent().setupState
|
||||
}
|
||||
|
||||
describe('Password Change Page', () => {
|
||||
let wrapper: ReturnType<typeof mount>
|
||||
|
||||
@@ -78,7 +83,7 @@ describe('Password Change Page', () => {
|
||||
const successSpy = vi.spyOn(message, 'success').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.formState.old_password = 'oldpass'
|
||||
vm.formState.new_password = 'newpass123'
|
||||
vm.formState.new_password_confirmation = 'newpass123'
|
||||
@@ -104,7 +109,7 @@ describe('Password Change Page', () => {
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.formState.old_password = 'wrong'
|
||||
vm.formState.new_password = 'newpass123'
|
||||
vm.formState.new_password_confirmation = 'newpass123'
|
||||
@@ -123,7 +128,7 @@ describe('Password Change Page', () => {
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.formRef = { validate: vi.fn().mockResolvedValue(true) }
|
||||
await vm.handleSubmit()
|
||||
await flushPromises()
|
||||
@@ -138,7 +143,7 @@ describe('Password Change Page', () => {
|
||||
vi.spyOn(message, 'error').mockImplementation(() => ({}) as never)
|
||||
|
||||
await mountPage()
|
||||
const vm = wrapper.getCurrentComponent().setupState
|
||||
const vm = getSetupState(wrapper)
|
||||
vm.formRef = { validate: vi.fn().mockResolvedValue(true) }
|
||||
await vm.handleSubmit()
|
||||
await flushPromises()
|
||||
|
||||
@@ -18,15 +18,20 @@ const rules = {
|
||||
],
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
onMounted(async () => {
|
||||
if (userStore.user) {
|
||||
formState.email = userStore.user.email || ''
|
||||
}
|
||||
userStore.fetchCurrentUser().then(() => {
|
||||
try {
|
||||
await userStore.fetchCurrentUser()
|
||||
if (userStore.user) {
|
||||
formState.email = userStore.user.email || ''
|
||||
}
|
||||
})
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof ApiError) {
|
||||
message.error(error.message)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const handleSubmit = async () => {
|
||||
|
||||
@@ -137,7 +137,7 @@ describe('useUserManageStore', () => {
|
||||
it('sets loading to false and shows error message on failure', async () => {
|
||||
const { api } = await import('@/utils/request')
|
||||
vi.mocked(api.get).mockRejectedValueOnce(new Error('Network error'))
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as any)
|
||||
const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as never)
|
||||
|
||||
const store = useUserManageStore()
|
||||
await store.fetchUsers()
|
||||
|
||||
@@ -175,7 +175,7 @@ describe('UserFormModal', () => {
|
||||
|
||||
// 验证 PUT payload 包含 status 且不包含 ext
|
||||
expect(api.put).toHaveBeenCalledOnce()
|
||||
const [url, payload] = vi.mocked(api.put).mock.calls[0]
|
||||
const [url, payload] = vi.mocked(api.put).mock.calls[0]!
|
||||
expect(url).toContain('/api/v1/users/')
|
||||
expect(payload).toHaveProperty('status')
|
||||
expect(payload).not.toHaveProperty('ext')
|
||||
|
||||
Reference in New Issue
Block a user