Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadFindStore.test.ts

94 lines3.6 KB

1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
3import { describe, expect, it, vi } from "vite-plus/test";
4​
5import {
6 attachThreadFindTimeline,
7 closeThreadFind,
8 findForMessageMatch,
9 openThreadFind,
10 openThreadFindOnQuery,
11 retainThreadFindForRoute,
12 setThreadFindQuery,
13 useThreadFindStore,
14} from "./threadFindStore";
15​
16const threadRef = scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-1"));
17const otherRef = scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-2"));
18const threadKey = "env-1:thread-1";
19​
20function setup() {
21 closeThreadFind();
22 const readPosition = vi.fn(() => ({ entryId: "entry-9", occurrence: 1 }));
23 return { readPosition, timeline: { threadKey, readPosition } };
25​
26describe("thread find store", () => {
27 it("restarts an open query from the viewport of the timeline showing the thread", () => {
28 const { readPosition, timeline } = setup();
29 const detach = attachThreadFindTimeline(timeline);
30 openThreadFind(threadKey);
31 expect(useThreadFindStore.getState()).toMatchObject({ threadKey, focusRequestId: 1 });
32​
33 setThreadFindQuery(threadKey, " needle ");
34 expect(readPosition).toHaveBeenLastCalledWith("needle");
35 // Opening again refocuses and restarts the search from where the viewport is.
36 openThreadFind(threadKey);
37 expect(useThreadFindStore.getState()).toMatchObject({
38 query: " needle ",
39 focusRequestId: 2,
40 start: { entryId: "entry-9", occurrence: 1 },
41 navigationId: 2,
42 requestId: 2,
43 });
44 detach();
45 });
46​
47 it("opens a search result's find for a thread before its timeline shows it", () => {
48 const { readPosition, timeline } = setup();
49 const detach = attachThreadFindTimeline({ ...timeline, threadKey: "env-1:thread-2" });
50 openThreadFindOnQuery(threadKey, { query: "Café", start: { entryId: "m", occurrence: 0 } });
51 expect(useThreadFindStore.getState()).toMatchObject({
52 threadKey,
53 query: "Café",
54 start: { entryId: "m", occurrence: 0 },
55 });
56 // Another thread's timeline does not answer for this one.
57 setThreadFindQuery(threadKey, "Caf");
58 expect(readPosition).not.toHaveBeenCalled();
59 detach();
60 });
61​
62 it("keeps find over Settings and Usage, and closes it on any other route", () => {
63 setup();
64 openThreadFindOnQuery(threadKey, { query: "x" });
65 retainThreadFindForRoute({ modalPage: false, threadRef });
66 retainThreadFindForRoute({ modalPage: true, threadRef: null });
67 expect(useThreadFindStore.getState()).toMatchObject({ threadKey, query: "x" });
68​
69 retainThreadFindForRoute({ modalPage: false, threadRef: otherRef });
70 expect(useThreadFindStore.getState()).toMatchObject({ threadKey: null, query: "" });
71​
72 openThreadFindOnQuery(threadKey, { query: "x" });
73 retainThreadFindForRoute({ modalPage: false, threadRef: null });
74 expect(useThreadFindStore.getState().threadKey).toBeNull();
75 });
76});
77​
78describe("findForMessageMatch", () => {
79 it("opens find on the longest word as the snippet writes it, at the matched message", () => {
80 expect(
81 findForMessageMatch(
82 { snippet: "The cafeteria, then the Café", messageId: MessageId.make("message-1") },
83 "cafe",
84 ),
85 ).toEqual({ query: "Café", start: { entryId: "message-1", occurrence: 0 } });
86 expect(findForMessageMatch({ snippet: "fix the sidebar lag" }, "fix sidebar")).toEqual({
87 query: "sidebar",
88 });
89 // The whole query, when the snippet holds it as typed.
90 expect(findForMessageMatch({ snippet: "then Vp run dev again" }, "vp run dev")).toEqual({
91 query: "Vp run dev",
92 });
93 });
94});