80 lines
2.6 KiB
TypeScript
80 lines
2.6 KiB
TypeScript
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, nextTick } from "vue";
|
|
import MessagesView from "./MessagesView.vue";
|
|
import { mockApi } from "@/api/mock";
|
|
import * as admin from "@/api/admin";
|
|
|
|
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("MessagesView", () => {
|
|
beforeEach(() => {
|
|
setActivePinia(createPinia());
|
|
mockApi._setSession();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it("查询时传递时间范围,统计含拒绝,详情有原因列并可翻页", async () => {
|
|
const listSpy = vi.spyOn(admin, "listMessages");
|
|
const getSpy = vi.spyOn(admin, "getMessage").mockImplementation((seq, cursor) => mockApi.getMessage(seq, cursor, 1));
|
|
const w = mount(wrap(MessagesView), {
|
|
global: { plugins: [createPinia()] },
|
|
attachTo: document.body,
|
|
});
|
|
await flushPromises();
|
|
|
|
expect(w.text()).toMatch(/已拒绝/);
|
|
const picker = w.findComponent({ name: "DatePicker" });
|
|
expect(picker.exists()).toBe(true);
|
|
picker.vm.$emit("update:value", [1_700_000_000_000, 1_800_000_000_000]);
|
|
await nextTick();
|
|
const queryBtn = w.findAll("button").find((b) => b.text().trim() === "查询");
|
|
await queryBtn!.trigger("click");
|
|
await flushPromises();
|
|
|
|
const last = listSpy.mock.calls.at(-1)?.[0] as { from_ms?: number; to_ms?: number };
|
|
expect(last.from_ms).toBe(1_700_000_000_000);
|
|
expect(last.to_ms).toBe(1_800_000_000_000);
|
|
|
|
const detailBtn = w.findAll("button").find((b) => b.text().trim() === "详情");
|
|
await detailBtn!.trigger("click");
|
|
await flushPromises();
|
|
expect(w.text()).toContain("原因");
|
|
expect(w.find('[data-testid="delivery-more"]').exists()).toBe(true);
|
|
|
|
getSpy.mockClear();
|
|
await w.find('[data-testid="delivery-more"]').trigger("click");
|
|
await flushPromises();
|
|
expect(getSpy).toHaveBeenCalled();
|
|
expect(getSpy.mock.calls[0][1]).toBeTruthy();
|
|
w.unmount();
|
|
});
|
|
});
|