优化
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { flushPromises, mount } from '@vue/test-utils'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import DashboardView from './DashboardView.vue'
|
||||
import { getTodayNewOrderCount } from '../api/dashboard'
|
||||
|
||||
vi.mock('../api/dashboard', () => ({
|
||||
getTodayNewOrderCount: vi.fn().mockResolvedValue(12),
|
||||
@@ -14,6 +15,8 @@ vi.mock('../api/dashboard', () => ({
|
||||
}),
|
||||
}))
|
||||
|
||||
enableAutoUnmount(afterEach)
|
||||
|
||||
describe('DashboardView', () => {
|
||||
beforeEach(() => vi.clearAllMocks())
|
||||
|
||||
@@ -24,6 +27,7 @@ describe('DashboardView', () => {
|
||||
expect(wrapper.text()).toContain('核心指标来自实时业务数据')
|
||||
expect(wrapper.text()).toContain('数据更新时间:2026-09-11 12:00')
|
||||
expect(wrapper.findAll('.dashboard-metric')).toHaveLength(4)
|
||||
expect(wrapper.findAll('.density-fields dt').map(label => label.text())).toEqual(['今日新运单', '在途柜量', '活跃客户', '待清关票数'])
|
||||
expect(wrapper.text()).toContain('12 票')
|
||||
expect(wrapper.text()).toContain('4.25 TEU')
|
||||
expect(wrapper.text()).toContain('8 家')
|
||||
@@ -36,4 +40,50 @@ describe('DashboardView', () => {
|
||||
expect(wrapper.text()).not.toContain('待办事项')
|
||||
expect(wrapper.text()).not.toContain('静态演示数据')
|
||||
})
|
||||
|
||||
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)
|
||||
await flushPromises()
|
||||
expect(wrapper.text()).toContain('部分核心指标加载失败')
|
||||
expect(wrapper.get('.dashboard-metric dd').text()).toContain('—')
|
||||
expect(wrapper.text()).toContain('4.25 TEU')
|
||||
await wrapper.findAll('button').find(button => button.text().trim() === '刷新数据')!.trigger('click')
|
||||
await flushPromises()
|
||||
expect(getTodayNewOrderCount).toHaveBeenCalledTimes(2)
|
||||
expect(wrapper.text()).toContain('核心指标来自实时业务数据')
|
||||
expect(wrapper.text()).toContain('12 票')
|
||||
})
|
||||
|
||||
it('uses the same density-enabled root when entering and exiting fullscreen', async () => {
|
||||
const wrapper = mount(DashboardView)
|
||||
await flushPromises()
|
||||
let fullscreen: Element | null = null
|
||||
const previousElement = Object.getOwnPropertyDescriptor(document, 'fullscreenElement')
|
||||
const previousExit = Object.getOwnPropertyDescriptor(document, 'exitFullscreen')
|
||||
const enter = vi.fn(async () => { fullscreen = wrapper.element; document.dispatchEvent(new Event('fullscreenchange')) })
|
||||
const exit = vi.fn(async () => { fullscreen = null; document.dispatchEvent(new Event('fullscreenchange')) })
|
||||
Object.defineProperty(wrapper.element, 'requestFullscreen', { configurable: true, value: enter })
|
||||
Object.defineProperty(document, 'fullscreenElement', { configurable: true, get: () => fullscreen })
|
||||
Object.defineProperty(document, 'exitFullscreen', { configurable: true, value: exit })
|
||||
try {
|
||||
expect(wrapper.classes()).toContain('admin-density')
|
||||
const button = wrapper.get('button[aria-pressed]')
|
||||
await button.trigger('click')
|
||||
await flushPromises()
|
||||
expect(enter).toHaveBeenCalledOnce()
|
||||
expect(button.text()).toBe('退出全屏')
|
||||
expect(button.attributes('aria-pressed')).toBe('true')
|
||||
await button.trigger('click')
|
||||
await flushPromises()
|
||||
expect(exit).toHaveBeenCalledOnce()
|
||||
expect(button.text()).toBe('全屏展示')
|
||||
expect(button.attributes('aria-pressed')).toBe('false')
|
||||
} finally {
|
||||
if (previousElement) Object.defineProperty(document, 'fullscreenElement', previousElement)
|
||||
else Reflect.deleteProperty(document, 'fullscreenElement')
|
||||
if (previousExit) Object.defineProperty(document, 'exitFullscreen', previousExit)
|
||||
else Reflect.deleteProperty(document, 'exitFullscreen')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user