feat(account): add forum login and sidebar account entry

This commit is contained in:
2026-05-18 22:34:14 +08:00
parent c24c88458c
commit 63dac217c2
12 changed files with 621 additions and 23 deletions
@@ -0,0 +1,48 @@
import { fireEvent, render, screen } from "@testing-library/vue";
import { describe, expect, it } from "vitest";
import AppSidebar from "@/components/AppSidebar.vue";
import type { SparkUser } from "@/global/typedefinition";
const baseProps = {
activeTab: "all",
categoryCounts: { all: 0 },
themeMode: "auto" as const,
storeFilter: "both" as const,
sparkAvailable: true,
apmAvailable: true,
sidebarEntries: [],
entryCounts: {},
};
const user: SparkUser = {
id: 1,
flarumUserId: "123",
username: "momen",
displayName: "Momen",
avatarUrl: "https://bbs.spark-app.store/avatar.png",
forumLevel: "管理员",
forumGroups: ["管理员"],
};
describe("AppSidebar account entry", () => {
it("prompts login when anonymous", async () => {
const rendered = render(AppSidebar, {
props: { ...baseProps, currentUser: null },
});
await fireEvent.click(screen.getByRole("button", { name: /登录 \/ 注册/ }));
expect(rendered.emitted("request-login")).toHaveLength(1);
});
it("opens quick menu for logged-in users", async () => {
render(AppSidebar, { props: { ...baseProps, currentUser: user } });
await fireEvent.click(screen.getByRole("button", { name: /Momen/ }));
expect(screen.getByText("用户管理")).toBeTruthy();
expect(screen.getByText("我的收藏")).toBeTruthy();
expect(screen.getByText("退出登录")).toBeTruthy();
});
});
+1
View File
@@ -16,6 +16,7 @@ const renderSidebar = (
apmAvailable: true,
sidebarEntries: [],
entryCounts: {},
currentUser: null,
...overrides,
},
});
+23
View File
@@ -0,0 +1,23 @@
import { fireEvent, render, screen } from "@testing-library/vue";
import { describe, expect, it } from "vitest";
import LoginModal from "@/components/LoginModal.vue";
describe("LoginModal", () => {
it("emits login credentials and register request", async () => {
const rendered = render(LoginModal, {
props: { show: true, loading: false, error: "" },
});
await fireEvent.update(screen.getByLabelText("论坛账号"), "momen");
await fireEvent.update(screen.getByLabelText("论坛密码"), "secret");
await fireEvent.click(screen.getByRole("button", { name: "登录" }));
await fireEvent.click(screen.getByRole("button", { name: "注册账号" }));
expect(rendered.emitted("login")?.[0]?.[0]).toEqual({
identification: "momen",
password: "secret",
});
expect(rendered.emitted("register")).toHaveLength(1);
});
});
+40
View File
@@ -0,0 +1,40 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
describe("authState", () => {
beforeEach(() => {
vi.resetModules();
localStorage.clear();
});
it("persists and clears a backend session", async () => {
const { authSession, currentUser, isLoggedIn, setAuthSession, logout } =
await import("@/global/authState");
setAuthSession({
accessToken: "jwt",
tokenType: "bearer",
user: {
id: 1,
flarumUserId: "123",
username: "momen",
displayName: "Momen",
avatarUrl: "https://bbs.spark-app.store/avatar.png",
forumLevel: "管理员",
forumGroups: ["管理员"],
},
});
expect(authSession.value?.accessToken).toBe("jwt");
expect(currentUser.value?.displayName).toBe("Momen");
expect(isLoggedIn.value).toBe(true);
expect(
JSON.parse(localStorage.getItem("spark-store-auth") || "{}").accessToken,
).toBe("jwt");
logout();
expect(authSession.value).toBeNull();
expect(isLoggedIn.value).toBe(false);
expect(localStorage.getItem("spark-store-auth")).toBeNull();
});
});