Files
memos/web/tests/device-preferences.test.tsx
T
johnnyjoygh ccc1f82393 refactor: keep link preview and double-click edit as device preferences
Link preview was a Timeline view option, so it reset to on everywhere else.
Double-click to edit was an instance setting an admin chose for everyone.
Both are presentation choices for one browser, so they now live in a single
localStorage-backed store shared by every memo view and are toggled from
Settings > Preferences under "This device".

MemoRelatedSetting.enable_double_click_edit is removed from the API and
storage protos and reactions becomes field 1; stored rows that still carry
the old key load unchanged. The admin Memo page keeps only reactions.
2026-10-21 07:34:33 +08:00

28 lines
1.4 KiB
TypeScript

import { act, renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it } from "vitest";
import { DEVICE_PREFERENCES_KEY, updateDevicePreferences, useDevicePreferences } from "@/hooks/useDevicePreferences";
describe("device preferences", () => {
beforeEach(() => localStorage.clear());
it("defaults to link previews on and double-click editing off", () => {
const { result } = renderHook(() => useDevicePreferences());
expect(result.current).toEqual({ linkPreview: true, doubleClickEdit: false });
});
it("reads saved values and ignores malformed ones", () => {
localStorage.setItem(DEVICE_PREFERENCES_KEY, JSON.stringify({ linkPreview: false, doubleClickEdit: "yes" }));
const { result } = renderHook(() => useDevicePreferences());
expect(result.current).toEqual({ linkPreview: false, doubleClickEdit: false });
});
it("persists updates and shares them with every reader", () => {
const first = renderHook(() => useDevicePreferences());
const second = renderHook(() => useDevicePreferences());
act(() => updateDevicePreferences({ doubleClickEdit: true }));
expect(first.result.current).toEqual({ linkPreview: true, doubleClickEdit: true });
expect(second.result.current).toEqual({ linkPreview: true, doubleClickEdit: true });
expect(JSON.parse(localStorage.getItem(DEVICE_PREFERENCES_KEY) ?? "null")).toEqual({ linkPreview: true, doubleClickEdit: true });
});
});