fix: 统一管理后台 401 与加载失败处理
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import { config, mount, flushPromises } from "@vue/test-utils";
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
NConfigProvider,
|
||||
NDialogProvider,
|
||||
NMessageProvider,
|
||||
dateZhCN,
|
||||
zhCN,
|
||||
} from "naive-ui";
|
||||
import { defineComponent, h } from "vue";
|
||||
import OverviewView from "./OverviewView.vue";
|
||||
import * as admin from "@/api/admin";
|
||||
import { mockApi } from "@/api/mock";
|
||||
|
||||
vi.mock("@/api/admin", async () => import("@/api/admin-mock"));
|
||||
|
||||
config.global.stubs = { teleport: true };
|
||||
|
||||
function wrap(Comp: object) {
|
||||
return defineComponent({
|
||||
setup() {
|
||||
return () =>
|
||||
h(NConfigProvider, { locale: zhCN, dateLocale: dateZhCN, size: "small" }, {
|
||||
default: () =>
|
||||
h(NMessageProvider, null, {
|
||||
default: () =>
|
||||
h(NDialogProvider, null, {
|
||||
default: () => h(Comp),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
describe("OverviewView 加载失败", () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia());
|
||||
mockApi._setSession();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("失败时显示重试,点后重新加载", async () => {
|
||||
const spy = vi.spyOn(admin, "fetchOverview");
|
||||
spy.mockRejectedValueOnce(new Error("boom")).mockResolvedValueOnce({
|
||||
version: "0.1.0",
|
||||
endpoints_total: 3,
|
||||
endpoints_online: 1,
|
||||
endpoints_disabled: 1,
|
||||
endpoints_self: 1,
|
||||
groups_total: 1,
|
||||
messages_pending: 0,
|
||||
messages_scheduled: 0,
|
||||
uptime_ms: 1000,
|
||||
});
|
||||
|
||||
const w = mount(wrap(OverviewView), {
|
||||
global: { plugins: [createPinia()] },
|
||||
attachTo: document.body,
|
||||
});
|
||||
await flushPromises();
|
||||
|
||||
expect(w.text()).toContain("加载失败");
|
||||
expect(w.find('[data-testid="load-retry"]').exists()).toBe(true);
|
||||
|
||||
await w.find('[data-testid="load-retry"]').trigger("click");
|
||||
await flushPromises();
|
||||
|
||||
expect(w.text()).toContain("0.1.0");
|
||||
expect(w.text()).not.toContain("加载失败");
|
||||
w.unmount();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user