-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathuse-ui-state.context.test.tsx
More file actions
179 lines (146 loc) · 4.35 KB
/
use-ui-state.context.test.tsx
File metadata and controls
179 lines (146 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import {
createCollection,
localOnlyCollectionOptions,
} from "@tanstack/react-db";
import { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { StudioLocalUiState } from "../studio/context";
import { useUiState } from "./use-ui-state";
const useOptionalStudioMock = vi.fn<
() =>
| {
uiLocalStateCollection: ReturnType<typeof createUiCollection>;
}
| undefined
>();
vi.mock("../studio/context", () => {
return {
useOptionalStudio: () => useOptionalStudioMock(),
};
});
(
globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;
function createUiCollection() {
return createCollection(
localOnlyCollectionOptions<StudioLocalUiState>({
id: "use-ui-state-context-test",
getKey(item) {
return item.id;
},
initialData: [],
}),
);
}
describe("useUiState with Studio context collection", () => {
let uiCollection: ReturnType<typeof createUiCollection>;
beforeEach(() => {
uiCollection = createUiCollection();
useOptionalStudioMock.mockReturnValue({
uiLocalStateCollection: uiCollection,
});
});
afterEach(() => {
vi.clearAllMocks();
document.body.innerHTML = "";
});
it("reads and writes through the injected Studio ui collection", () => {
const key = "context-backed-state";
const container = document.createElement("div");
document.body.appendChild(container);
const root = createRoot(container);
let latestState: ReturnType<typeof useUiState<string>> | undefined;
function Harness() {
latestState = useUiState<string>(key, "alpha");
return null;
}
act(() => {
root.render(<Harness />);
});
expect(latestState?.[0]).toBe("alpha");
expect(uiCollection.get(key)?.value).toBe("alpha");
act(() => {
latestState?.[1]("beta");
});
expect(latestState?.[0]).toBe("beta");
expect(uiCollection.get(key)?.value).toBe("beta");
act(() => {
root.unmount();
});
container.remove();
});
it("does not mutate the shared ui collection for cleanup-on-unmount state", () => {
const key = "context-cleanup-state";
const insertSpy = vi.spyOn(uiCollection, "insert");
const updateSpy = vi.spyOn(uiCollection, "update");
const deleteSpy = vi.spyOn(uiCollection, "delete");
const container = document.createElement("div");
document.body.appendChild(container);
const root = createRoot(container);
let latestState: ReturnType<typeof useUiState<string>> | undefined;
function Harness() {
latestState = useUiState<string>(key, "alpha", {
cleanupOnUnmount: true,
});
return null;
}
act(() => {
root.render(<Harness />);
});
act(() => {
latestState?.[1]("beta");
});
expect(latestState?.[0]).toBe("beta");
expect(insertSpy).not.toHaveBeenCalled();
expect(updateSpy).not.toHaveBeenCalled();
expect(deleteSpy).not.toHaveBeenCalled();
act(() => {
root.unmount();
});
container.remove();
expect(insertSpy).not.toHaveBeenCalled();
expect(updateSpy).not.toHaveBeenCalled();
expect(deleteSpy).not.toHaveBeenCalled();
expect(uiCollection.has(key)).toBe(false);
});
it("passes a cloneable plain value into object updaters", () => {
const key = "context-object-state";
const container = document.createElement("div");
document.body.appendChild(container);
const root = createRoot(container);
let latestState:
| ReturnType<typeof useUiState<Record<string, Array<"avg" | "p95">>>>
| undefined;
function Harness() {
latestState = useUiState<Record<string, Array<"avg" | "p95">>>(key, {});
return null;
}
act(() => {
root.render(<Harness />);
});
expect(() => {
act(() => {
latestState?.[1]((currentValue) => currentValue);
});
}).not.toThrow();
expect(() => {
act(() => {
latestState?.[1]((currentValue) => ({
...currentValue,
"series-1": ["avg"],
}));
});
}).not.toThrow();
expect(latestState?.[0]).toEqual({
"series-1": ["avg"],
});
expect(uiCollection.get(key)?.value).toEqual({
"series-1": ["avg"],
});
act(() => {
root.unmount();
});
container.remove();
});
});