Fix splice(-1,1) removing last user when logged-in user not found

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-09 13:52:08 +08:00
parent 3db725e52c
commit 9acd722929
2 changed files with 35 additions and 3 deletions

View File

@@ -127,7 +127,9 @@ export const useAcctMgmtStore = defineStore('acctMgmtStore', {
const loginStore = useLoginStore(); const loginStore = useLoginStore();
const loginUserData:User = loginStore.userData; const loginUserData:User = loginStore.userData;
const foundLoginUserIndex = fetchedUserList.findIndex(user => user.username === loginUserData.username); const foundLoginUserIndex = fetchedUserList.findIndex(user => user.username === loginUserData.username);
if (foundLoginUserIndex !== -1) {
fetchedUserList.unshift(fetchedUserList.splice(foundLoginUserIndex, 1)[0]); fetchedUserList.unshift(fetchedUserList.splice(foundLoginUserIndex, 1)[0]);
}
return fetchedUserList; return fetchedUserList;
}, },
/** /**

View File

@@ -21,8 +21,8 @@ vi.mock("@/api/client.js", () => ({
})); }));
// Mock login store to avoid its side effects // Mock login store to avoid its side effects
vi.mock("@/stores/login", () => { vi.mock("@/stores/login", async () => {
const { defineStore } = require("pinia"); const { defineStore } = await import("pinia");
return { return {
useLoginStore: defineStore("loginStore", { useLoginStore: defineStore("loginStore", {
state: () => ({ state: () => ({
@@ -243,6 +243,36 @@ describe("acctMgmtStore", () => {
}); });
}); });
describe("moveCurrentLoginUserToFirstRow", () => {
it("moves logged-in user to first position", async () => {
const list = [
{ username: "alice", name: "Alice" },
{ username: "currentUser", name: "Current" },
{ username: "bob", name: "Bob" },
];
const result = await store.moveCurrentLoginUserToFirstRow(list);
expect(result.map((u) => u.username)).toEqual([
"currentUser",
"alice",
"bob",
]);
});
it("preserves list when logged-in user is not found", async () => {
const list = [
{ username: "alice", name: "Alice" },
{ username: "bob", name: "Bob" },
{ username: "charlie", name: "Charlie" },
];
const result = await store.moveCurrentLoginUserToFirstRow(list);
expect(result.map((u) => u.username)).toEqual([
"alice",
"bob",
"charlie",
]);
});
});
it("resetJustCreateFlag resets flag", () => { it("resetJustCreateFlag resets flag", () => {
store.isOneAccountJustCreate = true; store.isOneAccountJustCreate = true;
store.resetJustCreateFlag(); store.resetJustCreateFlag();