-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathuse-table-ui-state.test.tsx
More file actions
197 lines (170 loc) · 4.75 KB
/
use-table-ui-state.test.tsx
File metadata and controls
197 lines (170 loc) · 4.75 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import {
createCollection,
localOnlyCollectionOptions,
} from "@tanstack/react-db";
import { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { Table } from "../../data/adapter";
import type { TableUiState } from "../studio/context";
import { useTableUiState } from "./use-table-ui-state";
const useStudioMock = vi.fn();
const useNavigationMock = vi.fn();
vi.mock("../studio/context", () => ({
useStudio: () => useStudioMock(),
}));
vi.mock("./use-navigation", () => ({
useNavigation: () => useNavigationMock(),
}));
(
globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;
function createTable(): Table {
return {
columns: {},
name: "users",
schema: "public",
};
}
function createTableUiStateCollection() {
return createCollection(
localOnlyCollectionOptions<TableUiState>({
id: "test-table-ui-state",
getKey(item) {
return item.id;
},
initialData: [],
}),
);
}
function renderHarness(defaults?: Parameters<typeof useTableUiState>[0]) {
const container = document.createElement("div");
document.body.appendChild(container);
const root = createRoot(container);
let latestState: ReturnType<typeof useTableUiState> | undefined;
function Harness() {
latestState = useTableUiState(defaults);
return null;
}
act(() => {
root.render(<Harness />);
});
function cleanup() {
act(() => {
root.unmount();
});
container.remove();
}
return {
cleanup,
getLatestState() {
return latestState;
},
};
}
async function flush(): Promise<void> {
await act(async () => {
await Promise.resolve();
await new Promise((resolve) => setTimeout(resolve, 0));
});
}
async function waitFor(assertion: () => boolean): Promise<void> {
const timeoutMs = 2000;
const start = Date.now();
while (Date.now() - start < timeoutMs) {
if (assertion()) {
return;
}
await flush();
}
throw new Error("Timed out waiting for hook state");
}
afterEach(() => {
vi.clearAllMocks();
document.body.innerHTML = "";
});
describe("useTableUiState", () => {
it("initializes table UI state for active table and applies updates", async () => {
const tableUiStateCollection = createTableUiStateCollection();
useStudioMock.mockReturnValue({
tableUiStateCollection,
});
useNavigationMock.mockReturnValue({
metadata: {
activeTable: createTable(),
},
});
const { cleanup, getLatestState } = renderHarness({
editingFilter: {
after: "and",
filters: [],
id: "default-filter",
kind: "FilterGroup",
},
stagedRows: [{ id: "staged-1" }],
stagedUpdates: [
{
changes: { name: "Alice Updated" },
row: { __ps_rowid: "row-1", id: "u1", name: "Alice" },
rowId: "row-1",
},
],
});
const initial = getLatestState();
expect(initial?.scopeKey).toBe("public.users");
expect(tableUiStateCollection.get("public.users")).toEqual(
expect.objectContaining({
id: "public.users",
stagedRows: [{ id: "staged-1" }],
stagedUpdates: [
{
changes: { name: "Alice Updated" },
row: { __ps_rowid: "row-1", id: "u1", name: "Alice" },
rowId: "row-1",
},
],
}),
);
act(() => {
initial?.updateTableUiState((draft) => {
draft.stagedRows = [{ id: "staged-2" }];
draft.stagedUpdates = [
{
changes: { name: "Alice Draft" },
row: { __ps_rowid: "row-2", id: "u2", name: "Alice" },
rowId: "row-2",
},
];
});
});
expect(tableUiStateCollection.get("public.users")?.stagedRows).toEqual([
{ id: "staged-2" },
]);
expect(tableUiStateCollection.get("public.users")?.stagedUpdates).toEqual([
{
changes: { name: "Alice Draft" },
row: { __ps_rowid: "row-2", id: "u2", name: "Alice" },
rowId: "row-2",
},
]);
await waitFor(() => {
const state = getLatestState()?.tableUiState;
return (
JSON.stringify(state?.stagedRows) === '[{"id":"staged-2"}]' &&
JSON.stringify(state?.stagedUpdates) ===
'[{"changes":{"name":"Alice Draft"},"row":{"__ps_rowid":"row-2","id":"u2","name":"Alice"},"rowId":"row-2"}]'
);
});
expect(getLatestState()?.tableUiState?.stagedRows).toEqual([
{ id: "staged-2" },
]);
expect(getLatestState()?.tableUiState?.stagedUpdates).toEqual([
{
changes: { name: "Alice Draft" },
row: { __ps_rowid: "row-2", id: "u2", name: "Alice" },
rowId: "row-2",
},
]);
cleanup();
});
});