优化
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils'
|
||||
import { ElButton, ElTooltip } from 'element-plus'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import DashboardView from './DashboardView.vue'
|
||||
import { getTodayNewOrderCount, getActiveCustomerCount, getDashboardOperationalMetrics } from '../api/dashboard'
|
||||
@@ -20,6 +21,10 @@ vi.mock('../api/dashboard', () => ({
|
||||
|
||||
enableAutoUnmount(afterEach)
|
||||
|
||||
function mountDashboard() {
|
||||
return mount(DashboardView, { global: { components: { ElButton, ElTooltip } } })
|
||||
}
|
||||
|
||||
describe('DashboardView', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
@@ -28,7 +33,7 @@ describe('DashboardView', () => {
|
||||
|
||||
it('makes no metric requests for a minimal pool sales account and gives an authorized-menu hint', async () => {
|
||||
access.permissions = new Set(['platform:fleet:capacity-offer:list', 'platform:fleet:capacity-offer:inquiry-create', 'platform:fleet:capacity-offer:inquiry-edit', 'platform:fleet:capacity-offer:inquiry-cancel'])
|
||||
const wrapper = mount(DashboardView); await flushPromises()
|
||||
const wrapper = mountDashboard(); await flushPromises()
|
||||
expect(getTodayNewOrderCount).not.toHaveBeenCalled(); expect(getActiveCustomerCount).not.toHaveBeenCalled(); expect(getDashboardOperationalMetrics).not.toHaveBeenCalled()
|
||||
expect(wrapper.findAll('.dashboard-metric')).toHaveLength(0)
|
||||
expect(wrapper.text()).toContain('请从左侧菜单进入已授权业务')
|
||||
@@ -37,28 +42,30 @@ describe('DashboardView', () => {
|
||||
|
||||
it('requests only granted metric sources and requires both container and customs permissions', async () => {
|
||||
access.permissions = new Set(['platform:customer:list', 'platform:firstleg:container:list'])
|
||||
const wrapper = mount(DashboardView); await flushPromises()
|
||||
const wrapper = mountDashboard(); await flushPromises()
|
||||
expect(getActiveCustomerCount).toHaveBeenCalledOnce(); expect(getTodayNewOrderCount).not.toHaveBeenCalled(); expect(getDashboardOperationalMetrics).not.toHaveBeenCalled()
|
||||
expect(wrapper.findAll('.dashboard-metric p').map(node => node.text())).toEqual(['活跃客户'])
|
||||
expect(wrapper.findAll('.workbench-metric-label').map(node => node.text())).toEqual(['活跃客户'])
|
||||
await wrapper.findAll('button').find(button => button.text().trim() === '刷新数据')!.trigger('click'); await flushPromises()
|
||||
expect(getActiveCustomerCount).toHaveBeenCalledTimes(2); expect(getDashboardOperationalMetrics).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('renders only the four live metrics', async () => {
|
||||
const wrapper = mount(DashboardView)
|
||||
const wrapper = mountDashboard()
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.find('.dashboard-context > p').exists()).toBe(false)
|
||||
expect(wrapper.classes()).toContain('admin-workbench')
|
||||
expect(wrapper.text()).toContain('数据更新时间:2026-09-11 12:00')
|
||||
expect(wrapper.findAll('.dashboard-metric')).toHaveLength(4)
|
||||
expect(wrapper.findAll('.dashboard-metric p').map(label => label.text())).toEqual(['今日新运单', '在途柜量', '活跃客户', '待清关票数'])
|
||||
expect(wrapper.findAll('.workbench-metric-label').map(label => label.text())).toEqual(['今日新运单', '在途柜量', '活跃客户', '待清关票数'])
|
||||
expect(wrapper.find('.density-fields').exists()).toBe(false)
|
||||
expect(wrapper.findAll('.dashboard-metric-mark')).toHaveLength(4)
|
||||
expect(wrapper.findAll('.dashboard-metric.workbench-metric .workbench-icon')).toHaveLength(4)
|
||||
expect(wrapper.findAll('button[aria-label^="查看"]')).toHaveLength(4)
|
||||
expect(wrapper.text()).toContain('12 票')
|
||||
expect(wrapper.text()).toContain('4.25 TEU')
|
||||
expect(wrapper.text()).toContain('8 家')
|
||||
expect(wrapper.text()).toContain('7 票')
|
||||
expect(wrapper.text()).toContain('1 柜箱型未知')
|
||||
expect(wrapper.get('.dashboard-metric .workbench-badge[data-tone="amber"]').text()).toBe('1 柜箱型未知')
|
||||
expect(wrapper.text()).not.toContain('近 30 天运单量趋势')
|
||||
expect(wrapper.text()).not.toContain('运单状态分布')
|
||||
expect(wrapper.text()).not.toContain('TOP10 航线货量')
|
||||
@@ -69,7 +76,7 @@ describe('DashboardView', () => {
|
||||
|
||||
it('keeps successful metrics when one request fails and refreshes through the visible control', async () => {
|
||||
vi.mocked(getTodayNewOrderCount).mockRejectedValueOnce(new Error('指标加载失败'))
|
||||
const wrapper = mount(DashboardView)
|
||||
const wrapper = mountDashboard()
|
||||
await flushPromises()
|
||||
expect(wrapper.text()).toContain('部分核心指标加载失败')
|
||||
expect(wrapper.get('.dashboard-metric strong').text()).toContain('—')
|
||||
@@ -82,7 +89,7 @@ describe('DashboardView', () => {
|
||||
})
|
||||
|
||||
it('uses the same visualization root when entering and exiting fullscreen', async () => {
|
||||
const wrapper = mount(DashboardView)
|
||||
const wrapper = mountDashboard()
|
||||
await flushPromises()
|
||||
let fullscreen: Element | null = null
|
||||
const previousElement = Object.getOwnPropertyDescriptor(document, 'fullscreenElement')
|
||||
|
||||
Reference in New Issue
Block a user