feat: 项目结构重构优化
This commit is contained in:
62
tests/unit/composables/useDictSync.test.ts
Normal file
62
tests/unit/composables/useDictSync.test.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { useDictSync } from "@/composables/websocket/useDictSync";
|
||||
import { useDictStore } from "@/store/modules/dict";
|
||||
import { setActivePinia, createPinia } from "pinia";
|
||||
|
||||
// Mock useStomp
|
||||
vi.mock("@/composables/websocket/useStomp", () => ({
|
||||
useStomp: vi.fn(() => ({
|
||||
isConnected: ref(false),
|
||||
connect: vi.fn(),
|
||||
disconnect: vi.fn(),
|
||||
subscribe: vi.fn(() => "sub-id"),
|
||||
unsubscribe: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
describe("useDictSync", () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia());
|
||||
localStorage.clear();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("初始化", () => {
|
||||
it("应该创建字典同步实例", () => {
|
||||
const dictSync = useDictSync();
|
||||
expect(dictSync).toBeDefined();
|
||||
expect(dictSync.initialize).toBeDefined();
|
||||
expect(dictSync.cleanup).toBeDefined();
|
||||
});
|
||||
|
||||
it("应该初始化 WebSocket 连接", () => {
|
||||
const dictSync = useDictSync();
|
||||
dictSync.initialize();
|
||||
// 验证初始化逻辑(具体实现取决于 useDictSync 的内部逻辑)
|
||||
expect(dictSync).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("清理", () => {
|
||||
it("应该清理 WebSocket 连接", () => {
|
||||
const dictSync = useDictSync();
|
||||
dictSync.initialize();
|
||||
dictSync.cleanup();
|
||||
// 验证清理逻辑
|
||||
expect(dictSync).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("字典同步", () => {
|
||||
it("应该处理字典更新消息", () => {
|
||||
const dictSync = useDictSync();
|
||||
const dictStore = useDictStore();
|
||||
|
||||
dictSync.initialize();
|
||||
|
||||
// 模拟接收字典更新消息
|
||||
// 注意:这里需要根据实际的消息格式来测试
|
||||
expect(dictStore).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
88
tests/unit/composables/useTableSelection.test.ts
Normal file
88
tests/unit/composables/useTableSelection.test.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import { useTableSelection } from "@/composables/table/useTableSelection";
|
||||
|
||||
describe("useTableSelection", () => {
|
||||
let selection: ReturnType<typeof useTableSelection>;
|
||||
|
||||
beforeEach(() => {
|
||||
selection = useTableSelection();
|
||||
});
|
||||
|
||||
it("应该初始化为空数组", () => {
|
||||
expect(selection.selectedIds.value).toEqual([]);
|
||||
expect(selection.selectedRows.value).toEqual([]);
|
||||
});
|
||||
|
||||
describe("handleSelectionChange()", () => {
|
||||
it("应该更新选中的行", () => {
|
||||
const rows = [
|
||||
{ id: 1, name: "张三" },
|
||||
{ id: 2, name: "李四" },
|
||||
];
|
||||
|
||||
selection.handleSelectionChange(rows);
|
||||
|
||||
expect(selection.selectedRows.value).toEqual(rows);
|
||||
expect(selection.selectedIds.value).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
it("应该处理空数组", () => {
|
||||
selection.handleSelectionChange([]);
|
||||
|
||||
expect(selection.selectedRows.value).toEqual([]);
|
||||
expect(selection.selectedIds.value).toEqual([]);
|
||||
});
|
||||
|
||||
it("应该支持自定义 ID 字段", () => {
|
||||
const customSelection = useTableSelection("userId");
|
||||
const rows = [
|
||||
{ userId: "u1", name: "张三" },
|
||||
{ userId: "u2", name: "李四" },
|
||||
];
|
||||
|
||||
customSelection.handleSelectionChange(rows);
|
||||
|
||||
expect(customSelection.selectedIds.value).toEqual(["u1", "u2"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("clearSelection()", () => {
|
||||
it("应该清空选中项", () => {
|
||||
const rows = [{ id: 1, name: "张三" }];
|
||||
selection.handleSelectionChange(rows);
|
||||
|
||||
selection.clearSelection();
|
||||
|
||||
expect(selection.selectedIds.value).toEqual([]);
|
||||
expect(selection.selectedRows.value).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("hasSelection", () => {
|
||||
it("有选中项时应该返回 true", () => {
|
||||
const rows = [{ id: 1, name: "张三" }];
|
||||
selection.handleSelectionChange(rows);
|
||||
|
||||
expect(selection.hasSelection.value).toBe(true);
|
||||
});
|
||||
|
||||
it("无选中项时应该返回 false", () => {
|
||||
expect(selection.hasSelection.value).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("selectionCount", () => {
|
||||
it("应该返回选中项数量", () => {
|
||||
expect(selection.selectionCount.value).toBe(0);
|
||||
|
||||
const rows = [
|
||||
{ id: 1, name: "张三" },
|
||||
{ id: 2, name: "李四" },
|
||||
{ id: 3, name: "王五" },
|
||||
];
|
||||
selection.handleSelectionChange(rows);
|
||||
|
||||
expect(selection.selectionCount.value).toBe(3);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user