diff --git a/src/components/modals/MusicCommentsDialog.spec.ts b/src/components/modals/MusicCommentsDialog.spec.ts new file mode 100644 index 000000000..b3574aa56 --- /dev/null +++ b/src/components/modals/MusicCommentsDialog.spec.ts @@ -0,0 +1,301 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { flushPromises, mount, type VueWrapper } from "@vue/test-utils"; +import { createPinia, setActivePinia } from "pinia"; +import { createI18n } from "vue-i18n"; +import { nextTick } from "vue"; +import { useStatusStore } from "@/stores/status"; +import MusicCommentsDialog from "./MusicCommentsDialog.vue"; + +const mockTrack = { + id: "track-1", + title: "测试曲目", + source: "netease", + duration: 200, + artist: "测试歌手", + artists: [{ id: "artist-1", name: "测试歌手" }], +}; + +const mockCommentData = { + list: [ + { + id: "comment-1", + userName: "张三", + text: "这是一条精彩的歌曲评论", + time: 1700000000000, + likedCount: 88, + reply: [ + { + id: "reply-1", + userName: "李四", + text: "赞同,前奏很抓耳", + time: 1700001000000, + }, + ], + }, + ], + total: 1, + page: 1, + limit: 20, +}; + +describe("MusicCommentsDialog 评论复制功能", () => { + let wrapper: VueWrapper; + let writeTextMock: ReturnType; + + beforeEach(() => { + setActivePinia(createPinia()); + writeTextMock = vi.fn().mockResolvedValue(undefined); + Object.defineProperty(navigator, "clipboard", { + value: { + writeText: writeTextMock, + }, + configurable: true, + writable: true, + }); + + window.api = { + ...window.api, + comments: { + sources: vi.fn().mockResolvedValue([ + { + id: "netease", + name: "网易云音乐", + kind: "builtin", + platform: "netease", + tabs: ["hot", "new"], + }, + ]), + get: vi.fn().mockResolvedValue({ + ok: true, + data: mockCommentData, + }), + }, + } as any; + }); + + afterEach(() => { + wrapper?.unmount(); + vi.clearAllMocks(); + }); + + /** + * 挂载评论弹窗并等待首屏数据加载完成 + * @returns 包装后的测试组件实例 + */ + const mountDialog = async (): Promise => { + const status = useStatusStore(); + status.commentsTrack = mockTrack as any; + status.commentsOpen = false; + + const i18n = createI18n({ + legacy: false, + locale: "zh-CN", + missingWarn: false, + fallbackWarn: false, + messages: { + "zh-CN": { + common: { + copied: "已复制", + copyFailed: "复制失败", + prev: "上一页", + next: "下一页", + }, + comments: { + name: "歌曲评论", + title: "「{name}」的评论", + hot: "热门评论", + new: "最新评论", + copy: "复制", + copyComment: "复制评论", + copyReply: "复制回复", + location: "来自{location}", + page: "第 {page} / {total} 页", + }, + }, + }, + }); + + wrapper = mount(MusicCommentsDialog, { + global: { + plugins: [i18n], + stubs: { + SDialog: { + props: ["open", "title"], + template: '
', + }, + STabs: { + props: ["modelValue", "tabs"], + template: '
', + }, + SSelect: { + props: ["modelValue", "options"], + template: '