|
| 1 | +import { mount, type VueWrapper } from "@vue/test-utils"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { createI18n } from "vue-i18n"; |
| 4 | +import { nextTick } from "vue"; |
| 5 | +import type { SettingCategory } from "@/types/settings-schema"; |
| 6 | +import zhCN from "@/i18n/locales/zh-CN.json"; |
| 7 | +import enUS from "@/i18n/locales/en-US.json"; |
| 8 | +import SInput from "@/components/ui/SInput.vue"; |
| 9 | +import SettingsSearch from "./SettingsSearch.vue"; |
| 10 | + |
| 11 | +const fixtures = vi.hoisted(() => ({ schema: [] as SettingCategory[] })); |
| 12 | +vi.mock("@/settings/schema", () => ({ settingsSchema: fixtures.schema })); |
| 13 | + |
| 14 | +let wrapper: VueWrapper; |
| 15 | + |
| 16 | +/** |
| 17 | + * 挂载指定语言的设置搜索 |
| 18 | + * @param locale - 界面语言 |
| 19 | + */ |
| 20 | +const mountSearch = (locale: "zh-CN" | "en-US" = "zh-CN") => { |
| 21 | + const i18n = createI18n({ |
| 22 | + legacy: false, |
| 23 | + locale, |
| 24 | + messages: { "zh-CN": zhCN, "en-US": enUS }, |
| 25 | + }); |
| 26 | + wrapper = mount(SettingsSearch, { |
| 27 | + global: { |
| 28 | + plugins: [i18n], |
| 29 | + components: { SInput }, |
| 30 | + stubs: { IconLucideSearch: true, IconLucideX: true }, |
| 31 | + }, |
| 32 | + }); |
| 33 | + return i18n; |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * 输入搜索词 |
| 38 | + * @param query - 搜索词 |
| 39 | + */ |
| 40 | +const search = async (query: string) => { |
| 41 | + await wrapper.get("input").trigger("focus"); |
| 42 | + await wrapper.get("input").setValue(query); |
| 43 | +}; |
| 44 | + |
| 45 | +beforeEach(() => { |
| 46 | + fixtures.schema.splice(0, fixtures.schema.length, { |
| 47 | + id: "other", |
| 48 | + icon: {}, |
| 49 | + sections: [ |
| 50 | + { |
| 51 | + id: "platformLogin", |
| 52 | + items: [ |
| 53 | + { |
| 54 | + key: "qmAccount", |
| 55 | + type: "custom", |
| 56 | + descriptionKey: "settings.platformLogin.desc", |
| 57 | + keywords: ["settings.platformLogin.title"], |
| 58 | + }, |
| 59 | + { |
| 60 | + key: "kgAccount", |
| 61 | + type: "custom", |
| 62 | + descriptionKey: "settings.platformLogin.desc", |
| 63 | + keywords: ["settings.platformLogin.title"], |
| 64 | + }, |
| 65 | + { key: "downloadDir", type: "custom" }, |
| 66 | + { |
| 67 | + key: "pluginImport", |
| 68 | + type: "custom", |
| 69 | + descriptionKey: "settings.plugins.hint", |
| 70 | + }, |
| 71 | + ], |
| 72 | + }, |
| 73 | + ], |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +afterEach(() => wrapper.unmount()); |
| 78 | + |
| 79 | +describe("设置搜索文案", () => { |
| 80 | + it.each(["zh-CN", "en-US"] as const)("%s 显示账号标题和组件共用的描述", async (locale) => { |
| 81 | + mountSearch(locale); |
| 82 | + await search(locale === "zh-CN" ? "登录" : "account"); |
| 83 | + const messages = locale === "zh-CN" ? zhCN : enUS; |
| 84 | + expect(wrapper.text()).toContain(messages.settings.qmAccount.label); |
| 85 | + expect(wrapper.text()).toContain(messages.settings.kgAccount.label); |
| 86 | + expect(wrapper.text()).toContain(messages.settings.platformLogin.desc); |
| 87 | + expect(wrapper.text()).not.toContain("settings."); |
| 88 | + }); |
| 89 | + |
| 90 | + it.each(["zh-CN", "en-US"] as const)("%s 没有描述的下载目录仍可搜索", async (locale) => { |
| 91 | + mountSearch(locale); |
| 92 | + const messages = locale === "zh-CN" ? zhCN : enUS; |
| 93 | + await search(messages.settings.downloadDir.label); |
| 94 | + expect(wrapper.findAll(".font-medium").map((item) => item.text())).toEqual([ |
| 95 | + messages.settings.downloadDir.label, |
| 96 | + ]); |
| 97 | + expect(wrapper.find(".truncate").exists()).toBe(false); |
| 98 | + }); |
| 99 | + |
| 100 | + it("缺失的标题、描述和关键词不会显示或参与匹配", async () => { |
| 101 | + fixtures.schema[0].sections![0].items.push( |
| 102 | + { key: "missingTitle", type: "custom", keywords: ["settings.platformLogin.title"] }, |
| 103 | + { key: "pluginList", type: "custom", keywords: ["settings.missingKeyword"] }, |
| 104 | + ); |
| 105 | + mountSearch(); |
| 106 | + await search("登录"); |
| 107 | + expect(wrapper.findAll(".font-medium")).toHaveLength(2); |
| 108 | + await search("settings"); |
| 109 | + expect(wrapper.findAll(".font-medium")).toHaveLength(0); |
| 110 | + expect(wrapper.text()).toContain(zhCN.common.noData); |
| 111 | + }); |
| 112 | + |
| 113 | + it("切换语言后更新已有搜索结果", async () => { |
| 114 | + const i18n = mountSearch(); |
| 115 | + await search("QQ"); |
| 116 | + expect(wrapper.text()).toContain(zhCN.settings.qmAccount.label); |
| 117 | + i18n.global.locale.value = "en-US"; |
| 118 | + await nextTick(); |
| 119 | + expect(wrapper.text()).toContain(enUS.settings.qmAccount.label); |
| 120 | + expect(wrapper.text()).not.toContain(zhCN.settings.qmAccount.label); |
| 121 | + }); |
| 122 | + |
| 123 | + it("递归搜索子项并携带父项供收起时定位", async () => { |
| 124 | + fixtures.schema[0].sections![0].items.push({ |
| 125 | + key: "appearanceStyle", |
| 126 | + type: "select", |
| 127 | + searchable: false, |
| 128 | + hideChildren: true, |
| 129 | + children: [{ key: "backgroundBlur", type: "slider" }], |
| 130 | + }); |
| 131 | + mountSearch(); |
| 132 | + await search(zhCN.settings.backgroundBlur.label); |
| 133 | + await wrapper.get(".top-full .cursor-pointer").trigger("mousedown"); |
| 134 | + expect(wrapper.emitted("select")).toEqual([["other", "backgroundBlur", ["appearanceStyle"]]]); |
| 135 | + expect((wrapper.get("input").element as HTMLInputElement).value).toBe(""); |
| 136 | + }); |
| 137 | + |
| 138 | + it("隐藏的分区和设置项连同子项不参与搜索", async () => { |
| 139 | + fixtures.schema[0].sections![0].items[0].visible = () => false; |
| 140 | + fixtures.schema[0].sections!.push({ |
| 141 | + id: "appearanceStyle", |
| 142 | + visible: () => false, |
| 143 | + items: [{ key: "qmAccount", type: "custom" }], |
| 144 | + }); |
| 145 | + fixtures.schema[0].sections![0].items.push({ |
| 146 | + key: "appearanceStyle", |
| 147 | + type: "select", |
| 148 | + visible: () => false, |
| 149 | + children: [{ key: "qmAccount", type: "custom" }], |
| 150 | + }); |
| 151 | + mountSearch(); |
| 152 | + await search("QQ"); |
| 153 | + expect(wrapper.findAll(".font-medium")).toHaveLength(0); |
| 154 | + }); |
| 155 | + |
| 156 | + it("只有空格时不遮挡设置菜单", async () => { |
| 157 | + mountSearch(); |
| 158 | + await search(" "); |
| 159 | + expect(wrapper.emitted("active-change")).toBeUndefined(); |
| 160 | + expect(wrapper.find(".top-full").exists()).toBe(false); |
| 161 | + }); |
| 162 | +}); |
0 commit comments