// @vitest-environment jsdom // // jsdom because the shared module reaches lib/api, which pulls in i18n's localStorage probe. import { describe, it, expect, vi } from "vitest"; import { QueryClient } from "@tanstack/react-query"; import { invalidateUserAccess } from "./adminUsers"; import { qk } from "./queryKeys"; // The set matters, not the call: letting someone in changes three things a user can see at once — // the row, the pending queue, and the /api/me badge — and the account panel shipped invalidating // only the first two, so the badge went on claiming the person was stuck until a reload. Pinning the // keys here is what stops the two call sites drifting apart again, since neither component test // asserts on invalidation. describe("invalidateUserAccess", () => { it("refreshes the row, the pending queue and the badge", () => { const qc = new QueryClient(); const spy = vi.spyOn(qc, "invalidateQueries").mockImplementation(() => Promise.resolve()); invalidateUserAccess(qc); const keys = spy.mock.calls.map(([a]) => JSON.stringify(a?.queryKey)); expect(keys).toEqual( [qk.adminUsers(), qk.adminPendingAccess(), qk.me()].map((k) => JSON.stringify(k)), ); }); it("also refreshes one account's gate readout when given an id", () => { const qc = new QueryClient(); const spy = vi.spyOn(qc, "invalidateQueries").mockImplementation(() => Promise.resolve()); invalidateUserAccess(qc, 7); expect(spy.mock.calls.map(([a]) => JSON.stringify(a?.queryKey))).toContain( JSON.stringify(qk.adminUserAccess(7)), ); }); });