mirror of
https://github.com/block/buzz.git
synced 2026-11-24 02:02:07 +00:00
Co-authored-by: Brother Darryl <146fb160a3266e6165bfa385f6048c975eda9e21cf65da097a0b5ea7952532a5@buzz.block.builderlab.xyz> Signed-off-by: loganj <loganj@squareup.com>
96 lines
2.9 KiB
JavaScript
96 lines
2.9 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { after, before, test } from "node:test";
|
|
|
|
import { JSDOM } from "jsdom";
|
|
|
|
import {
|
|
DEFAULT_INBOX_VIEW_PREFERENCE,
|
|
inboxViewPreferenceStorageKey,
|
|
parseInboxViewPreference,
|
|
} from "./inboxViewPreference.ts";
|
|
|
|
const dom = new JSDOM("<!doctype html><html><body></body></html>", {
|
|
url: "http://localhost",
|
|
});
|
|
|
|
before(() => {
|
|
Object.assign(globalThis, {
|
|
document: dom.window.document,
|
|
HTMLElement: dom.window.HTMLElement,
|
|
IS_REACT_ACT_ENVIRONMENT: true,
|
|
window: dom.window,
|
|
});
|
|
});
|
|
|
|
after(() => dom.window.close());
|
|
|
|
test("parses versioned Inbox view preferences with safe field defaults", () => {
|
|
assert.deepEqual(
|
|
parseInboxViewPreference({
|
|
version: 1,
|
|
filter: "mention",
|
|
unreadOnly: true,
|
|
}),
|
|
{ version: 1, filter: "mention", unreadOnly: true },
|
|
);
|
|
assert.deepEqual(
|
|
parseInboxViewPreference({
|
|
version: 1,
|
|
filter: "unknown",
|
|
unreadOnly: "yes",
|
|
}),
|
|
DEFAULT_INBOX_VIEW_PREFERENCE,
|
|
);
|
|
assert.equal(parseInboxViewPreference({ version: 2 }), null);
|
|
});
|
|
|
|
test("filter and unread-only restore from the per-user, per-relay cache", async () => {
|
|
const { act, cleanup, renderHook } = await import("@testing-library/react");
|
|
const { relayClient } = await import("@/shared/api/relayClient");
|
|
const { useInboxViewPreference } = await import(
|
|
"./useInboxViewPreference.ts"
|
|
);
|
|
|
|
const originalFetchEvents = relayClient.fetchEvents;
|
|
const originalSubscribeLive = relayClient.subscribeLive;
|
|
const originalSubscribeToReconnects = relayClient.subscribeToReconnects;
|
|
relayClient.fetchEvents = async () => [];
|
|
relayClient.subscribeLive = async () => async () => {};
|
|
relayClient.subscribeToReconnects = () => () => {};
|
|
|
|
const pubkey = "pk-inbox-view";
|
|
const relayUrl = "wss://relay.example";
|
|
const key = inboxViewPreferenceStorageKey(pubkey, relayUrl);
|
|
window.localStorage.clear();
|
|
|
|
try {
|
|
const first = renderHook(() => useInboxViewPreference(pubkey, relayUrl));
|
|
act(() => first.result.current.setFilter("mention"));
|
|
act(() => first.result.current.setUnreadOnly(true));
|
|
|
|
assert.deepEqual(JSON.parse(window.localStorage.getItem(key) ?? "null"), {
|
|
version: 1,
|
|
filter: "mention",
|
|
unreadOnly: true,
|
|
});
|
|
first.unmount();
|
|
|
|
const restored = renderHook(() => useInboxViewPreference(pubkey, relayUrl));
|
|
assert.equal(restored.result.current.filter, "mention");
|
|
assert.equal(restored.result.current.unreadOnly, true);
|
|
restored.unmount();
|
|
} finally {
|
|
cleanup();
|
|
relayClient.fetchEvents = originalFetchEvents;
|
|
relayClient.subscribeLive = originalSubscribeLive;
|
|
relayClient.subscribeToReconnects = originalSubscribeToReconnects;
|
|
}
|
|
});
|
|
|
|
test("Inbox view cache does not bleed across relays", () => {
|
|
assert.notEqual(
|
|
inboxViewPreferenceStorageKey("pk", "wss://one.example"),
|
|
inboxViewPreferenceStorageKey("pk", "wss://two.example"),
|
|
);
|
|
});
|