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:
@@ -127,7 +127,9 @@ export const useAcctMgmtStore = defineStore('acctMgmtStore', {
|
||||
const loginStore = useLoginStore();
|
||||
const loginUserData:User = loginStore.userData;
|
||||
const foundLoginUserIndex = fetchedUserList.findIndex(user => user.username === loginUserData.username);
|
||||
fetchedUserList.unshift(fetchedUserList.splice(foundLoginUserIndex, 1)[0]);
|
||||
if (foundLoginUserIndex !== -1) {
|
||||
fetchedUserList.unshift(fetchedUserList.splice(foundLoginUserIndex, 1)[0]);
|
||||
}
|
||||
return fetchedUserList;
|
||||
},
|
||||
/**
|
||||
|
||||
@@ -21,8 +21,8 @@ vi.mock("@/api/client.js", () => ({
|
||||
}));
|
||||
|
||||
// Mock login store to avoid its side effects
|
||||
vi.mock("@/stores/login", () => {
|
||||
const { defineStore } = require("pinia");
|
||||
vi.mock("@/stores/login", async () => {
|
||||
const { defineStore } = await import("pinia");
|
||||
return {
|
||||
useLoginStore: defineStore("loginStore", {
|
||||
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", () => {
|
||||
store.isOneAccountJustCreate = true;
|
||||
store.resetJustCreateFlag();
|
||||
|
||||
Reference in New Issue
Block a user