macos-web/tests/cases/44-podcasts.test.ts

31 lines
2.4 KiB
TypeScript

/**
* 44-podcasts: Podcasts Vue 组件化测试
* 覆盖: 6 集播客列表、选中/播放、▶ 指示器、时长格式化、下一集、audio 错误容错、卸载
* ⚠️ jsdom 不支持 audio.play(),验证 DOM 状态变化不抛错
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { h, render, nextTick } from 'vue'
import PodcastsComponent from '../../src/apps/podcasts/Podcasts.vue'
import { setupDOM } from '../helpers'
describe('44-podcasts', () => {
let body: HTMLElement, win: any
beforeEach(() => {
setupDOM(); const el = document.createElement('div'); el.className = 'window'; document.body.appendChild(el)
body = document.createElement('div'); body.className = 'win-body'; el.appendChild(body)
win = { el, body, timers: [], data: {} };
(window as any).Sys = () => ({ unregisterMedia: () => {} })
})
afterEach(() => { render(null, body); win.el.remove() })
function m() { render(h(PodcastsComponent, { win }), body) }
it('6 集播客', () => { m(); expect(body.querySelectorAll('.music-row').length).toBe(6) })
it('每集有序号和标题', () => { m(); const first = body.querySelector('.music-row') as HTMLElement; expect(first.querySelector('.music-num')?.textContent).toBe('1'); expect(first.querySelector('b')?.textContent).toContain('Monkeys') })
it('每集有时长', () => { m(); expect(body.querySelector('.music-dur')?.textContent).toMatch(/^\d+:\d{2}$/) })
it('点击选中显示 ▶', async () => { m(); const row = body.querySelector('.music-row') as HTMLElement; row.dispatchEvent(new MouseEvent('click', { bubbles: true })); await nextTick(); expect(body.querySelector('.music-row.sel')).toBeTruthy() })
it('底部栏显示当前集名', async () => { m(); const row = body.querySelector('.music-row') as HTMLElement; row.dispatchEvent(new MouseEvent('click', { bubbles: true })); await nextTick(); expect(body.querySelector('.pod-bar')?.textContent).toContain('Monkeys') })
it('再次点击暂停', async () => { m(); const row = body.querySelector('.music-row') as HTMLElement; row.dispatchEvent(new MouseEvent('click', { bubbles: true })); await nextTick(); row.dispatchEvent(new MouseEvent('click', { bubbles: true })); await nextTick(); /* 不抛错 */ })
it('时长格式化 22→0:22', () => { m(); expect(body.querySelector('.music-dur')?.textContent).toBe('0:22') })
it('卸载', () => { m(); render(null, body); expect(body.children.length).toBe(0) })
})