// @vitest-environment jsdom // // A preference save the server refuses is REPORTED (E11 S2b). The local state and the `me` cache // already show the new value, so a swallowed failure was invisible until the next load quietly put // the old value back — and the comment above the swallow claimed `req` toasted it, which it does not // for a 403/404/429 or a lost connection. import { afterEach, describe, expect, it, vi } from "vitest"; import { act, renderHook, waitFor } from "@testing-library/react"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { createElement, type ReactNode } from "react"; const { savePrefs, notifyActionError } = vi.hoisted(() => ({ savePrefs: vi.fn(), notifyActionError: vi.fn(), })); vi.mock("./api", () => ({ api: { savePrefs, me: vi.fn() } })); vi.mock("./actionErrors", () => ({ notifyActionError })); import { usePref, useSavePref } from "./usePref"; import i18n from "../i18n"; afterEach(() => { savePrefs.mockReset(); notifyActionError.mockReset(); }); const wrapper = ({ children }: { children: ReactNode }) => createElement(QueryClientProvider, { client: new QueryClient() }, children); describe("a refused preference save", () => { it("is reported from the usePref setter", async () => { const refusal = new TypeError("Failed to fetch"); savePrefs.mockRejectedValue(refusal); const { result } = renderHook(() => usePref("hintsOn", true), { wrapper }); act(() => result.current[1](false)); await waitFor(() => expect(notifyActionError).toHaveBeenCalledWith(refusal, i18n.t("errors.writeFailed.prefs")), ); expect(result.current[0]).toBe(false); // the local value stays; the toast says it did not save }); it("is reported from the raw useSavePref persister", async () => { const refusal = new Error("403"); savePrefs.mockRejectedValue(refusal); const { result } = renderHook(() => useSavePref(), { wrapper }); act(() => result.current({ stripSources: { c1: false } })); await waitFor(() => expect(notifyActionError).toHaveBeenCalledWith(refusal, i18n.t("errors.writeFailed.prefs")), ); }); });