This commit is contained in:
2026-08-14 18:50:11 +08:00
parent 93e0294ec0
commit 44ee2be246
9 changed files with 196 additions and 29 deletions
+47
View File
@@ -6,6 +6,7 @@ import { createMemoryHistory, createRouter } from 'vue-router'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { useAccessPreviewStore } from '@/core/access/accessPreview'
import { useAuthStore } from '@/core/auth/auth'
import { ApiResponseError } from '@/shared/api/http'
import LoginView from './LoginView.vue'
const loginSource = readFileSync(resolve(process.cwd(), 'src/modules/auth/pages/LoginView.vue'), 'utf8')
@@ -177,4 +178,50 @@ describe('LoginView authentication lifecycle', () => {
expect(initialize).not.toHaveBeenCalled()
expect(replace).not.toHaveBeenCalled()
})
it('shows the backend credential error and diagnostic identifiers', async () => {
const pinia = createPinia()
setActivePinia(pinia)
const auth = useAuthStore()
const router = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/login', component: LoginView },
{ path: '/', component: { template: '<p>home</p>' } },
],
})
await router.push('/login')
await router.isReady()
vi.spyOn(auth, 'login').mockRejectedValue(new ApiResponseError(
'用户名或密码错误',
'A0401',
undefined,
null,
'request-login',
'trace-login',
))
const wrapper = mount(LoginView, {
global: {
plugins: [pinia, router],
stubs: {
'el-form': formStub,
'el-form-item': { template: '<label><slot /></label>' },
'el-input': { props: ['modelValue'], emits: ['update:modelValue'], template: '<input :value="modelValue" @input="$emit(\'update:modelValue\', $event.target.value)" />' },
'el-button': { emits: ['click'], template: '<button type="button" @click="$emit(\'click\')"><slot /></button>' },
},
},
})
const inputs = wrapper.findAll('input')
await inputs[0]!.setValue('platform_admin')
await inputs[1]!.setValue('incorrect-password')
await wrapper.get('form').trigger('submit')
await flushPromises()
const alert = wrapper.get('[role="alert"]').text()
expect(alert).toContain('用户名或密码错误')
expect(alert).toContain('request-login')
expect(alert).toContain('trace-login')
expect(alert).not.toContain('登录状态已失效')
})
})