diff --git a/src/components/hooks/__tests__/useTrust.test.ts b/src/components/hooks/__tests__/useTrust.test.ts index 0280f2e..3d6f187 100644 --- a/src/components/hooks/__tests__/useTrust.test.ts +++ b/src/components/hooks/__tests__/useTrust.test.ts @@ -1,5 +1,5 @@ import { renderHook, waitFor } from "@testing-library/react"; -import { useTrust } from "../useTrust"; +import { useTrust, useTrustForAddress } from "../useTrust"; jest.mock("@/hooks/useAccount", () => ({ useAccount: jest.fn(), @@ -57,4 +57,36 @@ describe("useTrust", () => { expect(typeof result.current.suspicious).toBe("boolean"); }); }); + + it("does not leak current-user overrides into address-specific trust lookups", async () => { + localStorage.setItem( + "trustInfo", + JSON.stringify({ + reputation: 15, + isVerified: false, + }), + ); + + const { result, rerender } = renderHook( + ({ address }: { address?: string }) => useTrustForAddress(address), + { initialProps: { address: undefined } }, + ); + + await waitFor(() => { + expect(result.current.reputation).toBe(15); + expect(result.current.isVerified).toBe(false); + }); + + rerender({ address: "0xdef" }); + + await waitFor(() => { + const expectedReputation = Array.from("0xdef").reduce( + (sum, character) => sum + character.charCodeAt(0), + 0, + ) % 101; + + expect(result.current.reputation).toBe(expectedReputation); + expect(result.current.isVerified).toBe(true); + }); + }); }); diff --git a/src/components/hooks/useTrust.ts b/src/components/hooks/useTrust.ts index 8d63edd..a39e8bc 100644 --- a/src/components/hooks/useTrust.ts +++ b/src/components/hooks/useTrust.ts @@ -78,6 +78,8 @@ export function useTrustForAddress(address?: string): TrustInfo { useEffect(() => { if (!address) { setOverrideInfo(parseTrustInfoFromStorage()); + } else { + setOverrideInfo(null); } }, [address]);