mirror of
https://github.com/jxxghp/MoviePilot-Frontend.git
synced 2026-09-09 01:36:45 +08:00
Document test helpers and component stubs
This commit is contained in:
@@ -7,6 +7,7 @@ import { server } from '@tests/support/msw/server'
|
|||||||
import { defineComponent, h } from 'vue'
|
import { defineComponent, h } from 'vue'
|
||||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
|
||||||
|
/** 扩展卡片会消费但公共下载类型尚未声明的站点字段。 */
|
||||||
interface DownloadingCardInfo extends DownloadingInfo {
|
interface DownloadingCardInfo extends DownloadingInfo {
|
||||||
site_name?: string
|
site_name?: string
|
||||||
trackers?: string[]
|
trackers?: string[]
|
||||||
|
|||||||
@@ -83,11 +83,13 @@ class IntersectionObserverMock implements IntersectionObserver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 渲染媒体卡片时可覆盖的用户权限状态。 */
|
||||||
interface RenderCardOptions {
|
interface RenderCardOptions {
|
||||||
permissions?: Record<string, boolean>
|
permissions?: Record<string, boolean>
|
||||||
superUser?: boolean
|
superUser?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 使用指定媒体信息和用户权限渲染媒体卡片。 */
|
||||||
async function renderCard(media: MediaInfo, options: RenderCardOptions = {}) {
|
async function renderCard(media: MediaInfo, options: RenderCardOptions = {}) {
|
||||||
return renderWithProviders(MediaCard, {
|
return renderWithProviders(MediaCard, {
|
||||||
props: {
|
props: {
|
||||||
@@ -108,32 +110,38 @@ async function renderCard(media: MediaInfo, options: RenderCardOptions = {}) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 获取已渲染的媒体卡片根元素。 */
|
||||||
function getCard(container: Element) {
|
function getCard(container: Element) {
|
||||||
const card = container.querySelector<HTMLElement>('.media-card')
|
const card = container.querySelector<HTMLElement>('.media-card')
|
||||||
expect(card).not.toBeNull()
|
expect(card).not.toBeNull()
|
||||||
return card as HTMLElement
|
return card as HTMLElement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 获取负责桌面悬停和触摸交互的卡片区域。 */
|
||||||
function getHoverArea(container: Element) {
|
function getHoverArea(container: Element) {
|
||||||
const area = container.querySelector<HTMLElement>('.media-card-hover-area')
|
const area = container.querySelector<HTMLElement>('.media-card-hover-area')
|
||||||
expect(area).not.toBeNull()
|
expect(area).not.toBeNull()
|
||||||
return area as HTMLElement
|
return area as HTMLElement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 获取媒体卡片当前渲染的所有操作按钮。 */
|
||||||
function getActionButtons(container: Element) {
|
function getActionButtons(container: Element) {
|
||||||
return [...container.querySelectorAll<HTMLButtonElement>('.media-card .v-card-text button')]
|
return [...container.querySelectorAll<HTMLButtonElement>('.media-card .v-card-text button')]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 获取媒体搜索操作按钮并确保其已渲染。 */
|
||||||
function getSearchButton(container: Element) {
|
function getSearchButton(container: Element) {
|
||||||
const button = getActionButtons(container)[0]
|
const button = getActionButtons(container)[0]
|
||||||
expect(button).toBeDefined()
|
expect(button).toBeDefined()
|
||||||
return button
|
return button
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 筛选用于触发媒体状态懒加载的观察器。 */
|
||||||
function getStatusObservers() {
|
function getStatusObservers() {
|
||||||
return intersectionObservers.filter(observer => observer.thresholds.includes(0.1))
|
return intersectionObservers.filter(observer => observer.thresholds.includes(0.1))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 安装站点列表及已选站点的搜索请求处理器。 */
|
||||||
function installSearchHandlers(sites: Record<string, unknown>[], selected: number[]) {
|
function installSearchHandlers(sites: Record<string, unknown>[], selected: number[]) {
|
||||||
server.use(
|
server.use(
|
||||||
http.get(siteListUrl, () => HttpResponse.json(sites)),
|
http.get(siteListUrl, () => HttpResponse.json(sites)),
|
||||||
@@ -529,6 +537,7 @@ describe('MediaCard', () => {
|
|||||||
name: 'VImg',
|
name: 'VImg',
|
||||||
emits: ['error', 'load'],
|
emits: ['error', 'load'],
|
||||||
props: { src: String },
|
props: { src: String },
|
||||||
|
/** 渲染可主动触发图片成功和失败事件的测试替身。 */
|
||||||
setup(props, { emit, slots }) {
|
setup(props, { emit, slots }) {
|
||||||
return () =>
|
return () =>
|
||||||
h('div', { 'data-src': props.src }, [
|
h('div', { 'data-src': props.src }, [
|
||||||
@@ -567,6 +576,7 @@ describe('MediaCard', () => {
|
|||||||
name: 'VImg',
|
name: 'VImg',
|
||||||
emits: ['load'],
|
emits: ['load'],
|
||||||
props: { src: String },
|
props: { src: String },
|
||||||
|
/** 渲染可主动触发海报加载完成事件的测试替身。 */
|
||||||
setup(_props, { emit, slots }) {
|
setup(_props, { emit, slots }) {
|
||||||
return () =>
|
return () =>
|
||||||
h('div', [h('button', { 'aria-label': '图片加载成功', onClick: () => emit('load') }), slots.default?.()])
|
h('div', [h('button', { 'aria-label': '图片加载成功', onClick: () => emit('load') }), slots.default?.()])
|
||||||
|
|||||||
Reference in New Issue
Block a user