Files
outline/app/models/User.test.ts
T
Tom MoorandClaude 9ea360995f feat: Optimistic model saves with rollback on failure (#14593)
* feat: Make model.save() optimistic with rollback on failure

Apply saved params to the model immediately so MobX observers update
without waiting for the server, and roll the model back to its previous
state when the request fails. New models with a client-generated id are
optimistically added to the store and removed again on failure.

* chore: Apply formatter and test naming convention

* fix: Scope optimistic saves to passed fields and confirmed values

Only fields passed to save() are applied optimistically, and a failure
reverts a field only while it still holds the optimistic value. Change
hooks compare against the last confirmed values so they run when the
server responds. The optimistic store add is removed.

* refactor: Use optimistic save for preferences and field toggles

Add savePreference to User, Team and Document, and pass changed fields to
save() so they revert on failure.

* fix: Only compare incoming fields against confirmed values in change hooks

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-15 09:26:55 -05:00

325 lines
7.8 KiB
TypeScript

import { vi } from "vitest";
import { UserPreference } from "@shared/types";
import User from "./User";
import stores from "~/stores";
import { client } from "~/utils/ApiClient";
const post = vi.mocked(client.post);
describe("User model", () => {
const users = stores.users;
describe("initial", () => {
it("should return first character of name uppercased", () => {
const user = new User(
{
id: "123",
name: "alice smith",
},
users
);
expect(user.initial).toBe("A");
});
it("should return first character when name is already uppercase", () => {
const user = new User(
{
id: "124",
name: "Bob Johnson",
},
users
);
expect(user.initial).toBe("B");
});
it("should return ? when name is empty", () => {
const user = new User(
{
id: "125",
name: "",
},
users
);
expect(user.initial).toBe("?");
});
it("should return ? when name is null", () => {
const user = new User(
{
id: "126",
name: null,
},
users
);
expect(user.initial).toBe("?");
});
it("should return ? when name is undefined", () => {
const user = new User(
{
id: "127",
name: undefined,
},
users
);
expect(user.initial).toBe("?");
});
});
describe("initials", () => {
it("should return empty string when name is empty", () => {
const user = new User(
{
id: "201",
name: "",
},
users
);
expect(user.initials).toBe("");
});
it("should return empty string when name is null", () => {
const user = new User(
{
id: "202",
name: null,
},
users
);
expect(user.initials).toBe("");
});
it("should return single character uppercased for single word name", () => {
const user = new User(
{
id: "203",
name: "alice",
},
users
);
expect(user.initials).toBe("A");
});
it("should return single character uppercased for single word name already uppercase", () => {
const user = new User(
{
id: "204",
name: "BOB",
},
users
);
expect(user.initials).toBe("B");
});
it("should return first and last initials for two word name", () => {
const user = new User(
{
id: "205",
name: "alice smith",
},
users
);
expect(user.initials).toBe("AS");
});
it("should return first and last initials for three word name", () => {
const user = new User(
{
id: "206",
name: "alice marie smith",
},
users
);
expect(user.initials).toBe("AS");
});
it("should return first and last initials for many word name", () => {
const user = new User(
{
id: "207",
name: "alice marie jane doe smith",
},
users
);
expect(user.initials).toBe("AS");
});
it("should handle names with extra spaces", () => {
const user = new User(
{
id: "208",
name: " alice smith ",
},
users
);
expect(user.initials).toBe("AS");
});
it("should handle names with mixed case", () => {
const user = new User(
{
id: "209",
name: "aLiCe sMiTh",
},
users
);
expect(user.initials).toBe("AS");
});
it("should handle names with special characters", () => {
const user = new User(
{
id: "210",
name: "Jean-Pierre O'Connor",
},
users
);
expect(user.initials).toBe("JO");
});
it("should handle single letter names", () => {
const user = new User(
{
id: "211",
name: "X",
},
users
);
expect(user.initials).toBe("X");
});
it("should handle names with unicode characters", () => {
const user = new User(
{
id: "212",
name: "José García",
},
users
);
expect(user.initials).toBe("JG");
});
});
describe("documentMemberships", () => {
const user = new User({ id: "300", name: "Member" }, users);
beforeEach(() => {
stores.documents.clear();
stores.userMemberships.clear();
});
const addDocument = (id: string, parentDocumentId?: string) =>
stores.documents.add({
id,
title: id,
parentDocumentId,
collectionId: "collection",
});
const addMembership = (id: string, documentId: string) =>
stores.userMemberships.add({
id,
userId: user.id,
documentId,
sourceId: null,
index: id,
});
it("should only include the highest shared document of a tree", () => {
addDocument("parent");
addDocument("child", "parent");
addDocument("grandchild", "child");
addMembership("m1", "grandchild");
addMembership("m2", "child");
addMembership("m3", "parent");
expect(user.documentMemberships.map((m) => m.documentId)).toEqual([
"parent",
]);
});
it("should include a moved document that is still in a cached tree", () => {
addDocument("parent");
addDocument("child");
addMembership("m1", "child");
const parent = addMembership("m2", "parent");
parent.node = {
id: "parent",
title: "parent",
url: "/doc/parent",
children: [
{ id: "child", title: "child", url: "/doc/child", children: [] },
],
};
expect(user.documentMemberships.map((m) => m.documentId)).toEqual([
"child",
"parent",
]);
});
it("should include a document whose parent is not shared", () => {
addDocument("parent");
addDocument("child", "parent");
addDocument("other");
addMembership("m1", "child");
addMembership("m2", "other");
expect(user.documentMemberships.map((m) => m.documentId)).toEqual([
"child",
"other",
]);
});
});
describe("savePreference", () => {
beforeEach(() => {
post.mockReset();
});
it("should send only preferences and keep other values", async () => {
const user = users.add({
id: "pref-success",
name: "Alice",
preferences: { [UserPreference.RememberLastPath]: true },
});
post.mockResolvedValueOnce({ data: { id: "pref-success" } });
const promise = user.savePreference(
UserPreference.UseCursorPointer,
false
);
expect(user.getPreference(UserPreference.UseCursorPointer)).toBe(false);
await promise;
expect(post).toHaveBeenCalledWith("/users.update", {
id: "pref-success",
preferences: {
[UserPreference.RememberLastPath]: true,
[UserPreference.UseCursorPointer]: false,
},
});
expect(user.getPreference(UserPreference.UseCursorPointer)).toBe(false);
expect(user.getPreference(UserPreference.RememberLastPath)).toBe(true);
});
it("should revert the preference on failure", async () => {
const user = users.add({
id: "pref-failure",
name: "Alice",
preferences: { [UserPreference.UseCursorPointer]: true },
});
post.mockRejectedValueOnce(new Error("server error"));
const promise = user.savePreference(
UserPreference.UseCursorPointer,
false
);
expect(user.getPreference(UserPreference.UseCursorPointer)).toBe(false);
await expect(promise).rejects.toThrow("server error");
expect(user.getPreference(UserPreference.UseCursorPointer)).toBe(true);
});
});
});