Files
crawler-plugin/frontend-vue/tests/mount/notification-bell-failure.mount.test.ts
T
huangzd1997 be41331922 feat(用户端护栏): 首批组件挂载测试 + 铃铛标已读失败改可见(§2「0 挂载测试」那一半句闭掉)
- vitest.config.ts 与 vite.config.ts 分家:只服务 tests/mount/**,不进 tsconfig include,npm run build 产物不受影响(vue-tsc --noEmit rc=0 实测)
- tests/mount/notification-bell-failure.mount.test.ts 三支:标已读失败必须给带原因的错误提示且不许把行冒充成已读;成功时不产生提示且行真转已读;列表失败文案进面板本身而不弹全局消息
- NotificationBell.vue 的 onItemClick 改前只 console.warn(点了通知没反应、行也不转已读),现与 readAll 同走 ElMessage.error;反证:摘掉这行出口 ⇒ 挂载例判红(实测 1 failed 2 passed)
- 依赖只加 devDependencies(vitest/@vue/test-utils/happy-dom),运行时四项 lock 版本与 HEAD 逐一对等 ⇒ 生产构建输入未变
- 面板是 Teleport 到 body:wrapper.find 看不见里面的行,改从 document 取节点用原生 click 触发(实测踩过)
- npm test 那支不递归的 glob 仍 762 例 0 红,两套 runner 互不抓取
2026-10-09 11:21:47 +08:00

133 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 用户端首批组件挂载用例(四面9分口径 §2「错误静默 …;0 挂载测试」那一半句的执行证据)。
*
* 为什么是这三个靶子:本面 src 下的 vue 组件里"只 console 不给用户看"的 catch 实测 5 处,
* 其中铃铛占 3 处(未读数轮询、列表加载、点通知标已读)。轮询与列表那两处是**有意**静默
* (源码注释声明"不打扰用户",列表失败改在面板内给文案),所以这里钉的是"声明的意图真兑现";
* 标已读那一处改前是真静默——点了通知没反应、行也没转已读,用户只能反复点,本轮改成可见。
*
* 跑真组件:真 api 桩、真 ElMessage 出口、真 DOM,判的是运行时行为,
* 不是读源码的形状钉子(那类钉子在本仓已占 25% 用例,见 §2 后管同源问题)。
*
* ⚠ 面板是 `<Teleport to="body">`:每支用例都必须 unmount 并清 body,
* 否则上一支留下的 `.bell-item--unread` 会被这一支的 document 查询捡到。
*
* 跑法:`npx vitest run --root frontend-vue`
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import NotificationBell from '@/shared/components/NotificationBell.vue'
const message = vi.hoisted(() => ({
error: vi.fn(),
warning: vi.fn(),
success: vi.fn(),
info: vi.fn(),
closeAll: vi.fn(),
}))
const api = vi.hoisted(() => ({
fetchNotificationSummary: vi.fn(),
fetchNotificationList: vi.fn(),
markNotificationRead: vi.fn(),
markAllNotificationsRead: vi.fn(),
}))
vi.mock('element-plus', () => ({ ElMessage: message }))
vi.mock('@/shared/api/types/modules/notification.ts', () => ({
NOTIFICATION_CATEGORY_OPTIONS: [],
fetchNotificationSummary: api.fetchNotificationSummary,
fetchNotificationList: api.fetchNotificationList,
markAllNotificationsRead: api.markAllNotificationsRead,
markNotificationRead: api.markNotificationRead,
}))
const notification = (over: Record<string, unknown> = {}) => ({
id: 7,
title: '品牌检测服务挂了',
content: '上游 503',
level: 'error',
read: false,
createdAt: new Date().toISOString(),
...over,
})
let wrapper: VueWrapper | null = null
/** 打开面板:触发 togglePanel ⇒ 内部 void loadPage(1),靠 flushPromises 等它落地。 */
async function openPanel() {
wrapper = mount(NotificationBell)
await flushPromises()
await wrapper.find('.bell-trigger').trigger('click')
await flushPromises()
}
/**
* 点第一条通知。⚠ 面板是 Teleport 到 body 的,`wrapper.find()` 看不见里面的节点
* (实测 wrapper 查 `.bell-item` 得到空 wrapper 而 document 里那一行确实在)
* ⇒ 一律从 document 取节点、用原生 click 触发,Vue 的 @click 监听挂在元素上,照样生效。
*/
function clickFirstRow() {
const row = document.querySelector<HTMLElement>('.bell-item')
expect(row, '面板里没有可点的通知行').not.toBeNull()
row!.click()
}
describe('NotificationBell:三处失败的用户可见性', () => {
beforeEach(() => {
document.body.innerHTML = ''
localStorage.clear()
api.fetchNotificationSummary.mockResolvedValue({ unreadCount: 1, latestId: 7 })
api.fetchNotificationList.mockResolvedValue({ items: [notification()], total: 1, unreadCount: 1 })
api.markNotificationRead.mockResolvedValue(undefined)
vi.spyOn(console, 'warn').mockImplementation(() => {})
vi.spyOn(console, 'log').mockImplementation(() => {})
})
afterEach(() => {
wrapper?.unmount()
wrapper = null
document.body.innerHTML = ''
vi.restoreAllMocks()
vi.clearAllMocks()
})
it('点通知标已读失败:必须给一条带原因的错误提示,且不许把这一行冒充成已读', async () => {
api.markNotificationRead.mockRejectedValue(new Error('403 Forbidden'))
await openPanel()
expect(document.querySelectorAll('.bell-item')).toHaveLength(1)
clickFirstRow()
await flushPromises()
expect(message.error).toHaveBeenCalledTimes(1)
expect(String(message.error.mock.calls[0][0])).toContain('403 Forbidden')
// 失败就是失败:这一行还得看着是未读(否则用户以为点到了,实际服务端没落)
expect(document.querySelector('.bell-item--unread')).not.toBeNull()
})
it('反向钉:标已读成功时不产生任何提示,而这一行要真的转为已读', async () => {
await openPanel()
// 挂载那一轮 refreshSummary 发现"有新通知"本来就会弹一条 warning(既有设计,不是缺陷),
// 所以先把账清掉——本用例判的是"点成功这个动作"不许再产生提示。
message.error.mockClear()
message.warning.mockClear()
clickFirstRow()
await flushPromises()
expect(message.error).not.toHaveBeenCalled()
expect(message.warning).not.toHaveBeenCalled()
expect(document.querySelector('.bell-item--unread')).toBeNull()
})
it('列表加载失败:文案进面板本身,而不是弹一条全局消息打扰用户', async () => {
api.fetchNotificationList.mockRejectedValue(new Error('502 Bad Gateway'))
await openPanel()
expect(document.body.textContent).toContain('502 Bad Gateway')
expect(message.error).not.toHaveBeenCalled()
expect(document.querySelectorAll('.bell-item')).toHaveLength(0)
})
})