@marktiderman/genesis-ui-native 2.5.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-IDHW2NOP.js → chunk-3S5XVER4.js} +231 -6
- package/dist/chunk-3S5XVER4.js.map +1 -0
- package/dist/chunk-56QLEHHV.js +1231 -0
- package/dist/chunk-56QLEHHV.js.map +1 -0
- package/dist/chunk-6JBNSW4D.js +479 -0
- package/dist/chunk-6JBNSW4D.js.map +1 -0
- package/dist/{chunk-YJV5MMZX.js → chunk-KZEIF4YI.js} +7 -741
- package/dist/chunk-KZEIF4YI.js.map +1 -0
- package/dist/{chunk-YI7XVY2D.js → chunk-VMSIKSU2.js} +13 -479
- package/dist/chunk-VMSIKSU2.js.map +1 -0
- package/dist/components/index.d.ts +3 -1
- package/dist/components/index.js +3 -4
- package/dist/data/index.d.ts +382 -4
- package/dist/data/index.js +1067 -19
- package/dist/data/index.js.map +1 -1
- package/dist/inbox-page-DZUR-PJ1.d.ts +199 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +5 -5
- package/dist/layouts/index.d.ts +4 -195
- package/dist/layouts/index.js +3 -3
- package/package.json +2 -2
- package/src/components/image.tsx +5 -0
- package/src/data/NativeDetailPanel.tsx +8 -1
- package/src/data/index.ts +1 -0
- package/src/data/resource-view/NativeResourceFormSheet.tsx +305 -0
- package/src/data/resource-view/NativeResourceView.tsx +528 -0
- package/src/data/resource-view/__tests__/native-resource-view.test.tsx +870 -0
- package/src/data/resource-view/display.tsx +256 -0
- package/src/data/resource-view/index.ts +50 -0
- package/src/data/resource-view/mapping.ts +326 -0
- package/src/data/resource-view/messages.ts +121 -0
- package/src/data/resource-view/use-native-resource-view.ts +307 -0
- package/dist/chunk-EFXCJ5AN.js +0 -234
- package/dist/chunk-EFXCJ5AN.js.map +0 -1
- package/dist/chunk-FIITITXO.js +0 -492
- package/dist/chunk-FIITITXO.js.map +0 -1
- package/dist/chunk-IDHW2NOP.js.map +0 -1
- package/dist/chunk-YI7XVY2D.js.map +0 -1
- package/dist/chunk-YJV5MMZX.js.map +0 -1
|
@@ -0,0 +1,870 @@
|
|
|
1
|
+
// @feat:native-components
|
|
2
|
+
/**
|
|
3
|
+
* NativeResourceView: a genesis-core view on native list pieces.
|
|
4
|
+
* - summary → NativeListItem (its subtitle only, never a raw column);
|
|
5
|
+
* - the peek draws view.columns() like the cells: `when`-false and sensitive values never show;
|
|
6
|
+
* - groupBy → sections;
|
|
7
|
+
* - guards → swipe actions, each refusal with its reason; delete wording follows the bin;
|
|
8
|
+
* - form(mode) + writable(mode) → the form sheet; read-only columns are never inputs or sent;
|
|
9
|
+
* - url and image values go through isSafeUrl (`javascript:` is text);
|
|
10
|
+
* - a calendar day is the same day west of UTC;
|
|
11
|
+
* - a refused delete comes back and says why, inline;
|
|
12
|
+
* - cursor paging and the 300 ms search.
|
|
13
|
+
*/
|
|
14
|
+
// West of UTC, where `new Date("2026-01-02")` is the 1st. Before any import builds a formatter.
|
|
15
|
+
vi.hoisted(() => {
|
|
16
|
+
(globalThis as unknown as { process: { env: Record<string, string> } }).process.env.TZ = "America/Los_Angeles";
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// Watch what the list asks useInfiniteResource for (maxPages, the sort), with the real hook underneath.
|
|
20
|
+
const hooks = vi.hoisted(() => ({ infiniteOptions: [] as Record<string, unknown>[] }));
|
|
21
|
+
vi.mock("@marktiderman/genesis-core/hooks", async (importOriginal) => {
|
|
22
|
+
const actual = await importOriginal<typeof import("@marktiderman/genesis-core/hooks")>();
|
|
23
|
+
return {
|
|
24
|
+
...actual,
|
|
25
|
+
useInfiniteResource: (resource: string, options: Record<string, unknown>) => {
|
|
26
|
+
hooks.infiniteOptions.push(options);
|
|
27
|
+
return actual.useInfiniteResource(resource, options);
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
import { act, fireEvent, render, renderHook, screen, waitFor, within } from "@testing-library/react";
|
|
33
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
34
|
+
import type { ReactNode } from "react";
|
|
35
|
+
import { Alert, Linking } from "react-native";
|
|
36
|
+
import { describe, expect, it, vi, beforeEach } from "vitest";
|
|
37
|
+
import {
|
|
38
|
+
DataWriteError,
|
|
39
|
+
defineResource,
|
|
40
|
+
GenesisProvider,
|
|
41
|
+
type DataProvider,
|
|
42
|
+
type ListParams,
|
|
43
|
+
} from "@marktiderman/genesis-core";
|
|
44
|
+
import { defineView } from "@marktiderman/genesis-core/resource-view";
|
|
45
|
+
import {
|
|
46
|
+
dateToDay,
|
|
47
|
+
dayToDate,
|
|
48
|
+
defaultNativeResourceViewMessages as messages,
|
|
49
|
+
formFieldsFor,
|
|
50
|
+
formatDay,
|
|
51
|
+
NativeResourceView,
|
|
52
|
+
openableUrl,
|
|
53
|
+
rowItem,
|
|
54
|
+
screenValues,
|
|
55
|
+
sectionsFor,
|
|
56
|
+
serverSort,
|
|
57
|
+
formatInstant,
|
|
58
|
+
resolveZone,
|
|
59
|
+
swipeActionsFor,
|
|
60
|
+
useNativeResourceView,
|
|
61
|
+
validateValues,
|
|
62
|
+
writableValues,
|
|
63
|
+
} from "..";
|
|
64
|
+
|
|
65
|
+
type Brand = {
|
|
66
|
+
id: string;
|
|
67
|
+
practice_id: string;
|
|
68
|
+
name: string;
|
|
69
|
+
status: "active" | "archived" | "draft" | null;
|
|
70
|
+
is_default: boolean;
|
|
71
|
+
website: string | null;
|
|
72
|
+
launched_on: string | null;
|
|
73
|
+
secret: string | null;
|
|
74
|
+
secret_visible: boolean;
|
|
75
|
+
updated_at: string;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const brands = defineResource<Brand>()({
|
|
79
|
+
name: "brands",
|
|
80
|
+
key: "id",
|
|
81
|
+
labelField: "name",
|
|
82
|
+
fields: [
|
|
83
|
+
{ name: "id", type: "string", hasDefault: true },
|
|
84
|
+
{ name: "practice_id", type: "string" },
|
|
85
|
+
{ name: "name", type: "string" },
|
|
86
|
+
{ name: "status", type: "string", enumValues: ["active", "archived", "draft"], nullable: true },
|
|
87
|
+
{ name: "is_default", type: "boolean", hasDefault: true },
|
|
88
|
+
{ name: "website", type: "string", widget: "url", nullable: true },
|
|
89
|
+
{ name: "launched_on", type: "datetime", dbType: "date", nullable: true },
|
|
90
|
+
{ name: "secret", type: "string", nullable: true },
|
|
91
|
+
{ name: "secret_visible", type: "boolean", hasDefault: true },
|
|
92
|
+
{ name: "updated_at", type: "datetime", hasDefault: true, readOnly: true },
|
|
93
|
+
],
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// The same rows with a sensitive note, a link and a JSON blob: derived forms and the peek leave them out.
|
|
97
|
+
const notedBrands = defineResource<Brand & { notes: string | null; owner_id: string | null; meta: unknown }>()({
|
|
98
|
+
name: "brands",
|
|
99
|
+
key: "id",
|
|
100
|
+
labelField: "name",
|
|
101
|
+
fields: [
|
|
102
|
+
...brands.fields.filter((f) => f.name !== "secret" && f.name !== "secret_visible" && f.name !== "updated_at"),
|
|
103
|
+
{ name: "secret", type: "string", nullable: true },
|
|
104
|
+
{ name: "secret_visible", type: "boolean", hasDefault: true, readOnly: true },
|
|
105
|
+
{ name: "updated_at", type: "datetime", hasDefault: true, readOnly: true },
|
|
106
|
+
{ name: "notes", type: "string", nullable: true, sensitive: true },
|
|
107
|
+
{ name: "owner_id", type: "string", nullable: true, references: { resource: "people", field: "id" } },
|
|
108
|
+
{ name: "meta", type: "json", nullable: true },
|
|
109
|
+
],
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// A plain view: no summary, so the row falls back to labelField plus two columns.
|
|
113
|
+
const plainView = defineView(brands, {
|
|
114
|
+
scope: ["practice_id"],
|
|
115
|
+
fields: {
|
|
116
|
+
status: { tones: { active: "success" } },
|
|
117
|
+
secret: { label: "Secret", when: (r) => r.secret_visible },
|
|
118
|
+
name: { max: 10 },
|
|
119
|
+
},
|
|
120
|
+
columns: { default: ["name", "secret", "launched_on", "status", "website"], compact: ["name"] },
|
|
121
|
+
form: ["name", { section: "details", title: "Details", fields: ["status", "website", "launched_on", "updated_at"] }],
|
|
122
|
+
groupBy: "status",
|
|
123
|
+
orderBy: { field: "name" },
|
|
124
|
+
can: { delete: (r) => (r.is_default ? "The default brand stays." : true) },
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// A view with its own summary.
|
|
128
|
+
const summaryView = defineView(brands, {
|
|
129
|
+
summary: (r) => ({ title: r.name, subtitle: `Since ${r.launched_on ?? "never"}`, chips: [{ label: "Live", tone: "success" }] }),
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const ROWS: Brand[] = [
|
|
133
|
+
{
|
|
134
|
+
id: "b1",
|
|
135
|
+
practice_id: "p1",
|
|
136
|
+
name: "Northwind",
|
|
137
|
+
status: "active",
|
|
138
|
+
is_default: true,
|
|
139
|
+
website: "javascript:alert(1)",
|
|
140
|
+
launched_on: "2026-01-02",
|
|
141
|
+
secret: "hidden",
|
|
142
|
+
secret_visible: false,
|
|
143
|
+
updated_at: "2026-01-02T10:00:00Z",
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
id: "b2",
|
|
147
|
+
practice_id: "p1",
|
|
148
|
+
name: "Contoso",
|
|
149
|
+
status: "archived",
|
|
150
|
+
is_default: false,
|
|
151
|
+
website: "https://contoso.example",
|
|
152
|
+
launched_on: null,
|
|
153
|
+
secret: "shown",
|
|
154
|
+
secret_visible: true,
|
|
155
|
+
updated_at: "2026-01-02T10:00:00Z",
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
id: "b3",
|
|
159
|
+
practice_id: "p1",
|
|
160
|
+
name: "Fabrikam",
|
|
161
|
+
status: "active",
|
|
162
|
+
is_default: false,
|
|
163
|
+
website: null,
|
|
164
|
+
launched_on: null,
|
|
165
|
+
secret: null,
|
|
166
|
+
secret_visible: false,
|
|
167
|
+
updated_at: "2026-01-02T10:00:00Z",
|
|
168
|
+
},
|
|
169
|
+
];
|
|
170
|
+
|
|
171
|
+
function provider(overrides: Partial<DataProvider> = {}): DataProvider {
|
|
172
|
+
return {
|
|
173
|
+
getList: vi.fn(async () => ({ data: ROWS, total: ROWS.length })) as DataProvider["getList"],
|
|
174
|
+
getOne: vi.fn(async (_r, { id }) => ({ data: ROWS.find((r) => r.id === id) })) as DataProvider["getOne"],
|
|
175
|
+
getMany: vi.fn(async () => ({ data: [] })),
|
|
176
|
+
create: vi.fn(async (_r, { data }) => ({ data: { id: "new", ...(data as object) } })) as DataProvider["create"],
|
|
177
|
+
update: vi.fn(async (_r, { id, data }) => ({ data: { id, ...(data as object) } })) as DataProvider["update"],
|
|
178
|
+
deleteOne: vi.fn(async (_r, { id }) => ({ data: { id } })),
|
|
179
|
+
...overrides,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function wrap(p: DataProvider, queryClient = new QueryClient({ defaultOptions: { queries: { retry: false, staleTime: Infinity } } })) {
|
|
184
|
+
return ({ children }: { children: ReactNode }) => (
|
|
185
|
+
<GenesisProvider provider={p} queryClient={queryClient}>
|
|
186
|
+
{children}
|
|
187
|
+
</GenesisProvider>
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const alert = Alert.alert as unknown as ReturnType<typeof vi.fn>;
|
|
192
|
+
beforeEach(() => alert.mockClear());
|
|
193
|
+
|
|
194
|
+
/** Press a button of the last Alert.alert. */
|
|
195
|
+
function pressAlert(text: string) {
|
|
196
|
+
const buttons = alert.mock.calls.at(-1)?.[2] as { text: string; onPress?: () => void }[];
|
|
197
|
+
act(() => buttons.find((b) => b.text === text)!.onPress!());
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
describe("rows", () => {
|
|
201
|
+
it("uses summary(row) for the title, subtitle and chips", () => {
|
|
202
|
+
const item = rowItem(summaryView, ROWS[0]!);
|
|
203
|
+
expect(item).toEqual({ id: "b1", title: "Northwind", subtitle: "Since 2026-01-02", chips: [{ label: "Live", tone: "success" }], picture: null });
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("shows no subtitle when the summary has none, never a raw column", () => {
|
|
207
|
+
const item = rowItem(plainView, ROWS[1]!);
|
|
208
|
+
expect(item).toEqual({ id: "b2", title: "Contoso", chips: [], picture: null });
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("draws rows as list items with the summary's chip", async () => {
|
|
212
|
+
render(<NativeResourceView view={summaryView} />, { wrapper: wrap(provider()) });
|
|
213
|
+
const row = await screen.findByTestId("native-resource-view-row-b1");
|
|
214
|
+
expect(row.getAttribute("aria-label")).toBe("Northwind, Since 2026-01-02, Live");
|
|
215
|
+
expect(screen.getByTestId("native-resource-view-row-b1-chip").textContent).toBe("Live");
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe("sections", () => {
|
|
220
|
+
it("groups by groupBy in the order groups first appear, with a section for no value", () => {
|
|
221
|
+
const rows = [...ROWS, { ...ROWS[2]!, id: "b4", name: "Tailspin", status: null }];
|
|
222
|
+
const sections = sectionsFor(plainView, rows, messages);
|
|
223
|
+
expect(sections.map((s) => [s.title, s.data.map((r) => r.id)])).toEqual([
|
|
224
|
+
["Active", ["b1", "b3"]],
|
|
225
|
+
["Archived", ["b2"]],
|
|
226
|
+
["No status", ["b4"]],
|
|
227
|
+
]);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("is one untitled section without groupBy, and none for no rows", () => {
|
|
231
|
+
expect(sectionsFor(summaryView, ROWS, messages).map((s) => s.title)).toEqual([""]);
|
|
232
|
+
expect(sectionsFor(summaryView, [], messages)).toEqual([]);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("draws a header per section", async () => {
|
|
236
|
+
render(<NativeResourceView view={plainView} />, { wrapper: wrap(provider()) });
|
|
237
|
+
expect((await screen.findByTestId("native-resource-view-section-active")).textContent).toContain("Active");
|
|
238
|
+
// Only loaded rows could be counted, so the header shows the title alone.
|
|
239
|
+
expect(screen.getByTestId("native-resource-view-section-archived").textContent).toBe("Archived");
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
describe("guards to swipe actions", () => {
|
|
244
|
+
const handlers = () => ({ onEdit: vi.fn(), onDelete: vi.fn(), onRefused: vi.fn() });
|
|
245
|
+
|
|
246
|
+
it("keeps a refused action with its reason, and pressing it explains instead of acting", () => {
|
|
247
|
+
const h = handlers();
|
|
248
|
+
const actions = swipeActionsFor(plainView, ROWS[0]!, { messages, softDelete: false, editable: true, ...h });
|
|
249
|
+
expect(actions.map((a) => [a.id, a.label, a.reason])).toEqual([
|
|
250
|
+
["edit", "Edit", undefined],
|
|
251
|
+
["delete", "Delete", "The default brand stays."],
|
|
252
|
+
]);
|
|
253
|
+
actions[1]!.onPress();
|
|
254
|
+
expect(h.onDelete).not.toHaveBeenCalled();
|
|
255
|
+
expect(h.onRefused).toHaveBeenCalledWith("Delete", "The default brand stays.");
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("says Move to Bin when the provider bins deletes", () => {
|
|
259
|
+
const actions = swipeActionsFor(plainView, ROWS[1]!, { messages, softDelete: true, editable: true, ...handlers() });
|
|
260
|
+
expect(actions.find((a) => a.id === "delete")).toMatchObject({ label: "Move to Bin", destructive: true, icon: "trash" });
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("lets the screen only narrow: false hides, a sentence refuses, true never overrides the view", () => {
|
|
264
|
+
const actions = swipeActionsFor(plainView, ROWS[0]!, {
|
|
265
|
+
messages,
|
|
266
|
+
softDelete: false,
|
|
267
|
+
editable: true,
|
|
268
|
+
screen: (action) => (action === "update" ? false : true),
|
|
269
|
+
...handlers(),
|
|
270
|
+
});
|
|
271
|
+
expect(actions.map((a) => a.id)).toEqual(["delete"]);
|
|
272
|
+
expect(actions[0]!.reason).toBe("The default brand stays.");
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("reads the reason to screen readers and shows it when pressed", async () => {
|
|
276
|
+
render(<NativeResourceView view={plainView} />, { wrapper: wrap(provider()) });
|
|
277
|
+
const row = await screen.findByTestId("native-resource-view-row-b1");
|
|
278
|
+
expect(row.getAttribute("data-a11y-actions")).toBe("edit,delete");
|
|
279
|
+
act(() => {
|
|
280
|
+
row.dispatchEvent(new CustomEvent("accessibilityaction", { detail: "delete" }));
|
|
281
|
+
});
|
|
282
|
+
expect(alert).toHaveBeenLastCalledWith("Delete", "The default brand stays.");
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
describe("form sheet", () => {
|
|
287
|
+
it("builds fields from form(mode); read-only columns are text in an edit and absent in a create", () => {
|
|
288
|
+
const edit = formFieldsFor(plainView, "edit");
|
|
289
|
+
expect(edit.map((f) => [f.key, f.kind, f.readOnly, f.sectionTitle])).toEqual([
|
|
290
|
+
["name", "text", false, null],
|
|
291
|
+
["status", "segmented", false, "Details"],
|
|
292
|
+
["website", "url", false, "Details"],
|
|
293
|
+
["launched_on", "date", false, "Details"],
|
|
294
|
+
["updated_at", "datetime", true, "Details"],
|
|
295
|
+
]);
|
|
296
|
+
expect(formFieldsFor(plainView, "create").map((f) => f.key)).toEqual(["name", "status", "website", "launched_on"]);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("relies on form(): a derived form has no links, JSON, scope, sensitive or server-filled fields", () => {
|
|
300
|
+
const derived = defineView(notedBrands, { scope: ["practice_id"] });
|
|
301
|
+
expect(formFieldsFor(derived, "create").map((f) => f.key)).toEqual(["name", "status", "is_default", "website", "launched_on", "secret"]);
|
|
302
|
+
expect(formFieldsFor(derived, "edit").map((f) => f.key)).toEqual(["name", "status", "is_default", "website", "launched_on", "secret"]);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("copies form() rather than changing it (core caches and freezes it)", () => {
|
|
306
|
+
const before = plainView.form("edit");
|
|
307
|
+
formFieldsFor(plainView, "edit").forEach((f) => (f.label = "changed"));
|
|
308
|
+
expect(plainView.form("edit")).toBe(before);
|
|
309
|
+
expect(before[0]!.label).toBe("Name");
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("sends only writable keys: never read-only, key, scope or joined fields", () => {
|
|
313
|
+
const fields = formFieldsFor(plainView, "edit");
|
|
314
|
+
const values = { ...ROWS[1]!, joined_owner: "x", name: "Contoso 2", website: "" };
|
|
315
|
+
expect(writableValues(plainView, "edit", fields, values)).toEqual({
|
|
316
|
+
name: "Contoso 2",
|
|
317
|
+
status: "archived",
|
|
318
|
+
website: null,
|
|
319
|
+
launched_on: null,
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("checks required and max before saving", () => {
|
|
324
|
+
const fields = formFieldsFor(plainView, "create");
|
|
325
|
+
expect(validateValues(fields, { name: "" }, messages)).toEqual({ name: "Name is required." });
|
|
326
|
+
expect(validateValues(fields, { name: "A name that is too long" }, messages)).toEqual({
|
|
327
|
+
name: "Name can be at most 10 characters.",
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("opens over the row fetched by id, shows read-only fields as text, and sends only what changed", async () => {
|
|
332
|
+
// The list's select is narrower than the record: the website only comes with getOne.
|
|
333
|
+
const listRows = ROWS.map((r) => ({ ...r, website: null }));
|
|
334
|
+
const p = provider({ getList: vi.fn(async () => ({ data: listRows, total: listRows.length })) as unknown as DataProvider["getList"] });
|
|
335
|
+
render(<NativeResourceView view={plainView} />, { wrapper: wrap(p) });
|
|
336
|
+
const row = await screen.findByTestId("native-resource-view-row-b2");
|
|
337
|
+
act(() => {
|
|
338
|
+
row.dispatchEvent(new CustomEvent("accessibilityaction", { detail: "edit" }));
|
|
339
|
+
});
|
|
340
|
+
const form = await screen.findByTestId("native-resource-view-form");
|
|
341
|
+
expect(within(form).getByTestId("native-resource-view-form-cancel")).toBeTruthy();
|
|
342
|
+
const updated = await within(form).findByTestId("native-resource-view-form-field-updated_at");
|
|
343
|
+
expect(p.getOne).toHaveBeenCalledWith("brands", expect.objectContaining({ id: "b2" }));
|
|
344
|
+
expect(updated.querySelector("input")).toBeNull();
|
|
345
|
+
expect(within(form).getByTestId("native-resource-view-form-section-details").textContent).toContain("Details");
|
|
346
|
+
// The fetched website is in the form, though the list row had none.
|
|
347
|
+
const website = within(form).getByTestId("native-resource-view-form-field-website").querySelector("input")!;
|
|
348
|
+
expect(website.value).toBe("https://contoso.example");
|
|
349
|
+
fireEvent.change(within(form).getByTestId("native-resource-view-form-field-name").querySelector("input")!, { target: { value: "Contoso 2" } });
|
|
350
|
+
fireEvent.click(within(form).getByTestId("native-resource-view-form-save"));
|
|
351
|
+
await waitFor(() => expect(p.update).toHaveBeenCalled());
|
|
352
|
+
expect((p.update as ReturnType<typeof vi.fn>).mock.calls[0]![1]).toEqual({ id: "b2", data: { name: "Contoso 2" } });
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it("sends nothing when nothing changed", async () => {
|
|
356
|
+
const p = provider();
|
|
357
|
+
const { result } = renderHook(() => useNativeResourceView(plainView), { wrapper: wrap(p) });
|
|
358
|
+
await waitFor(() => expect(result.current.rows).toHaveLength(3));
|
|
359
|
+
await act(async () => {
|
|
360
|
+
expect(await result.current.save("edit", ROWS[1]!, ROWS[1]!, new Set())).toEqual({ ok: true });
|
|
361
|
+
});
|
|
362
|
+
expect(p.update).not.toHaveBeenCalled();
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("never puts a JSON or list field in the sheet, even when the form names it", () => {
|
|
366
|
+
type Tagged = { id: string; name: string; tags: string[] | null; meta: unknown };
|
|
367
|
+
const tagged = defineResource<Tagged>()({
|
|
368
|
+
name: "tagged",
|
|
369
|
+
key: "id",
|
|
370
|
+
labelField: "name",
|
|
371
|
+
fields: [
|
|
372
|
+
{ name: "id", type: "string", hasDefault: true },
|
|
373
|
+
{ name: "name", type: "string" },
|
|
374
|
+
{ name: "tags", type: "string", array: true, nullable: true },
|
|
375
|
+
{ name: "meta", type: "json", nullable: true },
|
|
376
|
+
],
|
|
377
|
+
});
|
|
378
|
+
const taggedView = defineView(tagged, { form: ["name", "tags", "meta"] });
|
|
379
|
+
for (const mode of ["create", "edit"] as const) {
|
|
380
|
+
expect(formFieldsFor(taggedView, mode).map((f) => f.key)).toEqual(["name"]);
|
|
381
|
+
expect(writableValues(taggedView, mode, formFieldsFor(taggedView, mode), { name: "a", tags: "x,y", meta: "[object Object]" })).toEqual({
|
|
382
|
+
name: "a",
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("sends null for a cleared optional number or date", () => {
|
|
388
|
+
type Scored = { id: string; name: string; score: number | null; due_on: string | null };
|
|
389
|
+
const scored = defineResource<Scored>()({
|
|
390
|
+
name: "scored",
|
|
391
|
+
key: "id",
|
|
392
|
+
labelField: "name",
|
|
393
|
+
fields: [
|
|
394
|
+
{ name: "id", type: "string", hasDefault: true },
|
|
395
|
+
{ name: "name", type: "string" },
|
|
396
|
+
{ name: "score", type: "number", nullable: true },
|
|
397
|
+
{ name: "due_on", type: "datetime", dbType: "date", nullable: true },
|
|
398
|
+
],
|
|
399
|
+
});
|
|
400
|
+
const scoredView = defineView(scored, {});
|
|
401
|
+
const fields = formFieldsFor(scoredView, "edit");
|
|
402
|
+
expect(fields.find((f) => f.key === "score")?.kind).toBe("number");
|
|
403
|
+
expect(writableValues(scoredView, "edit", fields, { name: "a", score: undefined, due_on: null }, new Set(["score", "due_on"]))).toEqual({
|
|
404
|
+
score: null,
|
|
405
|
+
due_on: null,
|
|
406
|
+
});
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it("shows an empty date as a button to add one, and a set date can be cleared", async () => {
|
|
410
|
+
render(<NativeResourceView view={plainView} />, { wrapper: wrap(provider()) });
|
|
411
|
+
fireEvent.click(await screen.findByTestId("native-resource-view-create"));
|
|
412
|
+
const add = await screen.findByTestId("native-resource-view-form-field-launched_on-set");
|
|
413
|
+
expect(add.textContent).toBe("Add a date");
|
|
414
|
+
fireEvent.click(add);
|
|
415
|
+
const clear = await screen.findByTestId("native-resource-view-form-field-launched_on-clear");
|
|
416
|
+
expect(clear.textContent).toBe("Clear launched on");
|
|
417
|
+
fireEvent.click(clear);
|
|
418
|
+
expect((await screen.findByTestId("native-resource-view-form-field-launched_on-set")).textContent).toBe("Add a date");
|
|
419
|
+
});
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
describe("links and the screen's filter", () => {
|
|
423
|
+
type Question = { id: string; form_id: string; prompt: string };
|
|
424
|
+
// A required link, read-only as core marks references (writable() leaves it out).
|
|
425
|
+
const questions = defineResource<Question>()({
|
|
426
|
+
name: "questions",
|
|
427
|
+
key: "id",
|
|
428
|
+
labelField: "prompt",
|
|
429
|
+
fields: [
|
|
430
|
+
{ name: "id", type: "string", hasDefault: true },
|
|
431
|
+
{ name: "form_id", type: "string", references: { resource: "forms", field: "id" }, readOnly: true },
|
|
432
|
+
{ name: "prompt", type: "string" },
|
|
433
|
+
],
|
|
434
|
+
});
|
|
435
|
+
const questionsView = defineView(questions, { form: ["form_id", "prompt"] });
|
|
436
|
+
const QUESTION: Question = { id: "q1", form_id: "f1", prompt: "How was it?" };
|
|
437
|
+
const questionProvider = () =>
|
|
438
|
+
provider({ getList: vi.fn(async () => ({ data: [QUESTION], total: 1 })) as unknown as DataProvider["getList"] });
|
|
439
|
+
|
|
440
|
+
it("shows a read-only link as its label and raw id, never an input", async () => {
|
|
441
|
+
render(<NativeResourceView view={questionsView} filter={{ form_id: "f1" }} />, { wrapper: wrap(questionProvider()) });
|
|
442
|
+
const row = await screen.findByTestId("native-resource-view-row-q1");
|
|
443
|
+
act(() => {
|
|
444
|
+
row.dispatchEvent(new CustomEvent("accessibilityaction", { detail: "edit" }));
|
|
445
|
+
});
|
|
446
|
+
const field = await screen.findByTestId("native-resource-view-form-field-form_id");
|
|
447
|
+
expect(field.querySelector("input")).toBeNull();
|
|
448
|
+
expect(field.textContent).toContain("Form");
|
|
449
|
+
expect(screen.getByTestId("native-resource-view-form-field-form_id-value").textContent).toBe("f1");
|
|
450
|
+
expect(formFieldsFor(questionsView, "create").map((f) => f.key)).toEqual(["prompt"]);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
it("sends the filter's link on create although it's read-only, and never on edit", async () => {
|
|
454
|
+
const p = questionProvider();
|
|
455
|
+
const { result } = renderHook(() => useNativeResourceView(questionsView, { filter: { form_id: "f1" } }), { wrapper: wrap(p) });
|
|
456
|
+
await waitFor(() => expect(result.current.rows).toHaveLength(1));
|
|
457
|
+
await act(async () => {
|
|
458
|
+
expect(await result.current.save("create", { prompt: "New one", form_id: "typed" })).toEqual({ ok: true });
|
|
459
|
+
});
|
|
460
|
+
expect((p.create as ReturnType<typeof vi.fn>).mock.calls[0]![1].data).toEqual({ prompt: "New one", form_id: "f1" });
|
|
461
|
+
await act(async () => {
|
|
462
|
+
expect(await result.current.save("edit", { ...QUESTION, prompt: "Changed", form_id: "f2" }, QUESTION)).toEqual({ ok: true });
|
|
463
|
+
});
|
|
464
|
+
expect((p.update as ReturnType<typeof vi.fn>).mock.calls[0]![1]).toEqual({ id: "q1", data: { prompt: "Changed" } });
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
it("takes create values from non-null filters only, the screen's over the tab's", () => {
|
|
468
|
+
expect(screenValues({ status: "draft", form_id: "a" }, { form_id: "b", owner_id: null })).toEqual({ status: "draft", form_id: "b" });
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
describe("urls and images", () => {
|
|
473
|
+
it("never opens javascript: or other unsafe schemes", () => {
|
|
474
|
+
expect(openableUrl("javascript:alert(1)")).toBeNull();
|
|
475
|
+
expect(openableUrl("java\tscript:alert(1)")).toBeNull();
|
|
476
|
+
expect(openableUrl("data:text/html,x")).toBeNull();
|
|
477
|
+
expect(openableUrl("/relative")).toBeNull();
|
|
478
|
+
// These start like a link but isSafeUrl refuses them: whitespace, control characters, length.
|
|
479
|
+
expect(openableUrl("https://exa mple.com")).toBeNull();
|
|
480
|
+
expect(openableUrl("https://example.com/\u0000")).toBeNull();
|
|
481
|
+
expect(openableUrl(`https://example.com/${"a".repeat(2100)}`)).toBeNull();
|
|
482
|
+
expect(openableUrl("https://contoso.example")).toBe("https://contoso.example");
|
|
483
|
+
expect(openableUrl("mailto:a@b.example")).toBe("mailto:a@b.example");
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
it("draws an unsafe url as text and a safe one as a link in the peek", async () => {
|
|
487
|
+
render(<NativeResourceView view={plainView} />, { wrapper: wrap(provider()) });
|
|
488
|
+
fireEvent.click(await screen.findByTestId("native-resource-view-row-b1"));
|
|
489
|
+
const peek = await screen.findByTestId("native-resource-view-peek");
|
|
490
|
+
expect(within(peek).getByText("javascript:alert(1)").getAttribute("role")).not.toBe("link");
|
|
491
|
+
expect(within(peek).queryAllByRole("link")).toHaveLength(0);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it("draws a safe url as a link that opens it", async () => {
|
|
495
|
+
render(<NativeResourceView view={plainView} />, { wrapper: wrap(provider()) });
|
|
496
|
+
fireEvent.click(await screen.findByTestId("native-resource-view-row-b2"));
|
|
497
|
+
const link = within(await screen.findByTestId("native-resource-view-peek")).getByRole("link");
|
|
498
|
+
expect(link.textContent).toBe("contoso.example");
|
|
499
|
+
fireEvent.click(link);
|
|
500
|
+
expect(Linking.openURL).toHaveBeenCalledWith("https://contoso.example");
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
describe("peek", () => {
|
|
505
|
+
it("shows only the view's columns, never every key of the row", async () => {
|
|
506
|
+
render(<NativeResourceView view={plainView} />, { wrapper: wrap(provider()) });
|
|
507
|
+
fireEvent.click(await screen.findByTestId("native-resource-view-row-b2"));
|
|
508
|
+
const peek = await screen.findByTestId("native-resource-view-peek");
|
|
509
|
+
for (const label of ["Name", "Secret", "Launched On", "Status", "Website"]) expect(peek.textContent).toContain(label);
|
|
510
|
+
for (const hidden of ["Practice Id", "Is Default", "Secret Visible", "Updated At"]) expect(peek.textContent).not.toContain(hidden);
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
it("hides a when-false value in the peek, as in a cell", async () => {
|
|
514
|
+
render(<NativeResourceView view={plainView} />, { wrapper: wrap(provider()) });
|
|
515
|
+
fireEvent.click(await screen.findByTestId("native-resource-view-row-b1"));
|
|
516
|
+
const peek = await screen.findByTestId("native-resource-view-peek");
|
|
517
|
+
expect(peek.textContent).toContain("Secret");
|
|
518
|
+
expect(peek.textContent).not.toContain("hidden");
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
it("titles the peek with summary(row).title, not the raw labelField", async () => {
|
|
522
|
+
const titled = defineView(brands, { summary: (r) => ({ title: `${r.name} (brand)` }) });
|
|
523
|
+
render(<NativeResourceView view={titled} />, { wrapper: wrap(provider()) });
|
|
524
|
+
fireEvent.click(await screen.findByTestId("native-resource-view-row-b2"));
|
|
525
|
+
expect((await screen.findByTestId("native-resource-view-peek")).textContent).toContain("Contoso (brand)");
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
it("shows a refused action with its reason, and the bin's wording", async () => {
|
|
529
|
+
const p = provider({ capabilities: vi.fn(async () => ({ softDelete: true })) as unknown as DataProvider["capabilities"] });
|
|
530
|
+
render(<NativeResourceView view={plainView} />, { wrapper: wrap(p) });
|
|
531
|
+
await waitFor(() => expect(p.capabilities).toHaveBeenCalled());
|
|
532
|
+
fireEvent.click(await screen.findByTestId("native-resource-view-row-b1"));
|
|
533
|
+
const button = await screen.findByTestId("native-resource-view-peek-delete");
|
|
534
|
+
await waitFor(() => expect(button.textContent).toBe("Move to Bin"));
|
|
535
|
+
expect(screen.getByTestId("native-resource-view-peek-delete-reason").textContent).toBe("The default brand stays.");
|
|
536
|
+
fireEvent.click(button);
|
|
537
|
+
expect(alert).toHaveBeenLastCalledWith("Move to Bin", "The default brand stays.");
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
it("reads a practice-clock column on the practice's zone", async () => {
|
|
541
|
+
const clocked = defineView(brands, {
|
|
542
|
+
fields: { updated_at: { timeZone: "practice" } },
|
|
543
|
+
columns: { default: ["name", "updated_at"] },
|
|
544
|
+
});
|
|
545
|
+
render(<NativeResourceView view={clocked} practiceTimeZone="Asia/Tokyo" />, { wrapper: wrap(provider()) });
|
|
546
|
+
fireEvent.click(await screen.findByTestId("native-resource-view-row-b2"));
|
|
547
|
+
const peek = await screen.findByTestId("native-resource-view-peek");
|
|
548
|
+
expect(peek.textContent).toContain(formatInstant("2026-01-02T10:00:00Z", "Asia/Tokyo"));
|
|
549
|
+
expect(formatInstant("2026-01-02T10:00:00Z", "Asia/Tokyo")).not.toBe(formatInstant("2026-01-02T10:00:00Z", "America/Los_Angeles"));
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
it("shows view.columns() drawn like the cells: a when-false value and a sensitive field never appear", async () => {
|
|
553
|
+
const peekView = defineView(notedBrands, {
|
|
554
|
+
scope: ["practice_id"],
|
|
555
|
+
fields: { secret: { label: "Secret", when: (r) => r.secret_visible } },
|
|
556
|
+
});
|
|
557
|
+
const rows = [{ ...ROWS[0]!, secret: "hidden-value", notes: "private-note", owner_id: "o1", meta: { a: 1 } }];
|
|
558
|
+
const p = provider({ getList: vi.fn(async () => ({ data: rows, total: 1 })) as unknown as DataProvider["getList"] });
|
|
559
|
+
render(<NativeResourceView view={peekView} />, { wrapper: wrap(p) });
|
|
560
|
+
fireEvent.click(await screen.findByTestId("native-resource-view-row-b1"));
|
|
561
|
+
const peek = await screen.findByTestId("native-resource-view-peek");
|
|
562
|
+
expect(peek.textContent).toContain("Secret");
|
|
563
|
+
expect(peek.textContent).not.toContain("hidden-value");
|
|
564
|
+
expect(peek.textContent).not.toContain("private-note");
|
|
565
|
+
expect(peek.textContent).not.toContain("Notes");
|
|
566
|
+
});
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
describe("calendar days", () => {
|
|
570
|
+
it("keeps a day the same day west of UTC", () => {
|
|
571
|
+
const date = dayToDate("2026-01-02")!;
|
|
572
|
+
expect([date.getFullYear(), date.getMonth(), date.getDate()]).toEqual([2026, 0, 2]);
|
|
573
|
+
expect(dateToDay(date)).toBe("2026-01-02");
|
|
574
|
+
expect(formatDay("2026-01-02")).toBe(new Intl.DateTimeFormat(undefined, { dateStyle: "medium" }).format(new Date(2026, 0, 2)));
|
|
575
|
+
// Only a whole day: a timestamp or trailing text isn't one.
|
|
576
|
+
expect(dayToDate("2026-01-02T00:00:00Z")).toBeNull();
|
|
577
|
+
expect(formatDay("2026-01-02junk")).toBe("2026-01-02junk");
|
|
578
|
+
// What `new Date(day)` would have shown here: the day before.
|
|
579
|
+
expect(new Date("2026-01-02").getDate()).toBe(1);
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
describe("clocks", () => {
|
|
584
|
+
it("resolves viewer and practice to the given zones, and an unknown name to the viewer's", () => {
|
|
585
|
+
const zones = { viewer: "America/Los_Angeles", practice: "Europe/Berlin" };
|
|
586
|
+
expect(resolveZone(undefined, zones)).toBe("America/Los_Angeles");
|
|
587
|
+
expect(resolveZone("viewer", zones)).toBe("America/Los_Angeles");
|
|
588
|
+
expect(resolveZone("practice", zones)).toBe("Europe/Berlin");
|
|
589
|
+
expect(resolveZone("Asia/Tokyo", zones)).toBe("Asia/Tokyo");
|
|
590
|
+
expect(resolveZone("Not/AZone", zones)).toBe("America/Los_Angeles");
|
|
591
|
+
});
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
describe("refusals", () => {
|
|
595
|
+
it("puts a refused delete back and says why, inline", async () => {
|
|
596
|
+
const p = provider({
|
|
597
|
+
deleteOne: vi.fn(async () => {
|
|
598
|
+
throw new DataWriteError("forbidden", "You can't delete this brand.");
|
|
599
|
+
}),
|
|
600
|
+
});
|
|
601
|
+
render(<NativeResourceView view={plainView} />, { wrapper: wrap(p) });
|
|
602
|
+
const row = await screen.findByTestId("native-resource-view-row-b2");
|
|
603
|
+
act(() => {
|
|
604
|
+
row.dispatchEvent(new CustomEvent("accessibilityaction", { detail: "delete" }));
|
|
605
|
+
});
|
|
606
|
+
expect(alert.mock.calls.at(-1)?.[0]).toBe("Delete “Contoso”?");
|
|
607
|
+
pressAlert("Delete");
|
|
608
|
+
expect((await screen.findByTestId("native-resource-view-refusal")).textContent).toContain("You can't delete this brand.");
|
|
609
|
+
expect(screen.getByTestId("native-resource-view-row-b2")).toBeTruthy();
|
|
610
|
+
});
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
describe("paging and search", () => {
|
|
614
|
+
it("loads the next page by cursor on onEndReached", async () => {
|
|
615
|
+
const getList = vi.fn(async (_r: string, params: ListParams) =>
|
|
616
|
+
params.pagination?.cursor
|
|
617
|
+
? { data: [ROWS[2]!], total: 3, nextCursor: null }
|
|
618
|
+
: { data: [ROWS[0]!, ROWS[1]!], total: 3, nextCursor: "c2" },
|
|
619
|
+
);
|
|
620
|
+
const p = provider({ getList: getList as unknown as DataProvider["getList"] });
|
|
621
|
+
const { result } = renderHook(() => useNativeResourceView(plainView, { perPage: 2 }), { wrapper: wrap(p) });
|
|
622
|
+
await waitFor(() => expect(result.current.rows).toHaveLength(2));
|
|
623
|
+
expect(result.current.hasMore).toBe(true);
|
|
624
|
+
act(() => result.current.loadMore());
|
|
625
|
+
await waitFor(() => expect(result.current.rows.map((r) => r.id)).toEqual(["b1", "b2", "b3"]));
|
|
626
|
+
expect(getList.mock.calls[1]![1].pagination).toMatchObject({ cursor: "c2", perPage: 2 });
|
|
627
|
+
// Grouped by status: the server sorts by it first, so page 2 never lands mid-section.
|
|
628
|
+
expect(getList.mock.calls[0]![1].sort).toEqual({ field: "status", order: "asc" });
|
|
629
|
+
expect(result.current.hasMore).toBe(false);
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
it("sends the search to the server 300 ms after the last keystroke", async () => {
|
|
633
|
+
const p = provider();
|
|
634
|
+
const { result } = renderHook(() => useNativeResourceView(plainView), { wrapper: wrap(p) });
|
|
635
|
+
await waitFor(() => expect(result.current.rows).toHaveLength(3));
|
|
636
|
+
vi.useFakeTimers();
|
|
637
|
+
try {
|
|
638
|
+
act(() => result.current.setSearch("No"));
|
|
639
|
+
act(() => result.current.setSearch("Nor"));
|
|
640
|
+
act(() => vi.advanceTimersByTime(299));
|
|
641
|
+
expect((p.getList as ReturnType<typeof vi.fn>).mock.calls.some((c) => (c[1] as ListParams).search)).toBe(false);
|
|
642
|
+
act(() => vi.advanceTimersByTime(1));
|
|
643
|
+
} finally {
|
|
644
|
+
vi.useRealTimers();
|
|
645
|
+
}
|
|
646
|
+
await waitFor(() =>
|
|
647
|
+
expect((p.getList as ReturnType<typeof vi.fn>).mock.calls.at(-1)![1].search).toEqual({ query: "Nor", fields: ["name"] }),
|
|
648
|
+
);
|
|
649
|
+
expect(result.current.search).toBe("Nor");
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
it("keeps at most 10 pages loaded by default, and takes the screen's number", () => {
|
|
653
|
+
hooks.infiniteOptions.length = 0;
|
|
654
|
+
renderHook(() => useNativeResourceView(plainView), { wrapper: wrap(provider()) });
|
|
655
|
+
expect(hooks.infiniteOptions.at(-1)).toMatchObject({ maxPages: 10, defaultSort: { field: "status", order: "asc" } });
|
|
656
|
+
renderHook(() => useNativeResourceView(summaryView, { maxPages: 3 }), { wrapper: wrap(provider()) });
|
|
657
|
+
expect(hooks.infiniteOptions.at(-1)).toMatchObject({ maxPages: 3 });
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
it("sorts by groupBy first, then by orderBy when there's no group", () => {
|
|
661
|
+
expect(serverSort(plainView)).toEqual({ field: "status", order: "asc" });
|
|
662
|
+
expect(serverSort(defineView(brands, { orderBy: { field: "name", direction: "desc" } }))).toEqual({ field: "name", order: "desc" });
|
|
663
|
+
expect(serverSort(defineView(brands, { groupBy: "status", orderBy: { field: "status", direction: "desc" } }))).toEqual({
|
|
664
|
+
field: "status",
|
|
665
|
+
order: "desc",
|
|
666
|
+
});
|
|
667
|
+
expect(serverSort(summaryView)).toBeUndefined();
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
it("doesn't redraw rows while the person types", async () => {
|
|
671
|
+
const summary = vi.fn((r: Brand) => ({ title: r.name }));
|
|
672
|
+
const counted = defineView(brands, { summary });
|
|
673
|
+
render(<NativeResourceView view={counted} />, { wrapper: wrap(provider()) });
|
|
674
|
+
await screen.findByTestId("native-resource-view-row-b1");
|
|
675
|
+
const before = summary.mock.calls.length;
|
|
676
|
+
const input = screen.getByTestId("native-resource-view-search").querySelector("input")!;
|
|
677
|
+
fireEvent.change(input, { target: { value: "N" } });
|
|
678
|
+
fireEvent.change(input, { target: { value: "No" } });
|
|
679
|
+
expect(summary.mock.calls.length).toBe(before);
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
it("keeps the rows and shows a banner when a later read fails", async () => {
|
|
683
|
+
let calls = 0;
|
|
684
|
+
const p = provider({
|
|
685
|
+
getList: vi.fn(async () => {
|
|
686
|
+
calls += 1;
|
|
687
|
+
if (calls > 1) throw new Error("network down");
|
|
688
|
+
return { data: ROWS, total: ROWS.length };
|
|
689
|
+
}) as unknown as DataProvider["getList"],
|
|
690
|
+
});
|
|
691
|
+
render(<NativeResourceView view={plainView} />, { wrapper: wrap(p) });
|
|
692
|
+
await screen.findByTestId("native-resource-view-row-b1");
|
|
693
|
+
fireEvent.click(screen.getByTestId("native-resource-view-refresh"));
|
|
694
|
+
expect((await screen.findByTestId("native-resource-view-read-error")).textContent).toContain("These brands couldn't be loaded.");
|
|
695
|
+
expect(screen.getByTestId("native-resource-view-row-b1")).toBeTruthy();
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
it("sends the screen's filter to the server", async () => {
|
|
699
|
+
const p = provider();
|
|
700
|
+
renderHook(() => useNativeResourceView(plainView, { filter: { practice_id: "p1", status: null } }), { wrapper: wrap(p) });
|
|
701
|
+
await waitFor(() => expect(p.getList).toHaveBeenCalled());
|
|
702
|
+
expect((p.getList as ReturnType<typeof vi.fn>).mock.calls[0]![1].filters).toEqual([
|
|
703
|
+
{ field: "practice_id", operator: "eq", value: "p1" },
|
|
704
|
+
{ field: "status", operator: "is_null" },
|
|
705
|
+
]);
|
|
706
|
+
});
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
describe("review follow-ups", () => {
|
|
710
|
+
it("diffs a save against what the sheet opened with, so another writer's change isn't sent back", async () => {
|
|
711
|
+
let website = "https://contoso.example";
|
|
712
|
+
const p = provider({
|
|
713
|
+
getOne: vi.fn(async (_r, { id }) => ({ data: { ...ROWS.find((r) => r.id === id)!, website } })) as DataProvider["getOne"],
|
|
714
|
+
});
|
|
715
|
+
const client = new QueryClient({ defaultOptions: { queries: { retry: false, staleTime: Infinity } } });
|
|
716
|
+
render(<NativeResourceView view={plainView} />, { wrapper: wrap(p, client) });
|
|
717
|
+
const row = await screen.findByTestId("native-resource-view-row-b2");
|
|
718
|
+
act(() => {
|
|
719
|
+
row.dispatchEvent(new CustomEvent("accessibilityaction", { detail: "edit" }));
|
|
720
|
+
});
|
|
721
|
+
const form = await screen.findByTestId("native-resource-view-form");
|
|
722
|
+
const name = (await within(form).findByTestId("native-resource-view-form-field-name")).querySelector("input")!;
|
|
723
|
+
fireEvent.change(name, { target: { value: "Contoso 2" } });
|
|
724
|
+
// Someone else changes the website; the record refetches while the sheet is open.
|
|
725
|
+
website = "https://changed.example";
|
|
726
|
+
await act(async () => {
|
|
727
|
+
await client.invalidateQueries({ queryKey: ["brands"] });
|
|
728
|
+
});
|
|
729
|
+
await waitFor(() => expect((p.getOne as ReturnType<typeof vi.fn>).mock.calls.length).toBeGreaterThan(1));
|
|
730
|
+
fireEvent.click(within(form).getByTestId("native-resource-view-form-save"));
|
|
731
|
+
await waitFor(() => expect(p.update).toHaveBeenCalled());
|
|
732
|
+
expect((p.update as ReturnType<typeof vi.fn>).mock.calls[0]![1]).toEqual({ id: "b2", data: { name: "Contoso 2" } });
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
// A provider that pages by cursor both ways: one row a page.
|
|
736
|
+
function cursorProvider() {
|
|
737
|
+
const pages: Record<string, { data: Brand[]; nextCursor: string | null; prevCursor: string | null }> = {
|
|
738
|
+
start: { data: [ROWS[0]!], nextCursor: "c2", prevCursor: null },
|
|
739
|
+
c1: { data: [ROWS[0]!], nextCursor: "c2", prevCursor: null },
|
|
740
|
+
c2: { data: [ROWS[1]!], nextCursor: "c3", prevCursor: "c1" },
|
|
741
|
+
c3: { data: [ROWS[2]!], nextCursor: null, prevCursor: "c2" },
|
|
742
|
+
};
|
|
743
|
+
const getList = vi.fn(async (_r: string, params: ListParams) => ({ ...pages[params.pagination?.cursor ?? "start"]!, total: 3 }));
|
|
744
|
+
return { getList, p: provider({ getList: getList as unknown as DataProvider["getList"] }) };
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
it("loads back a page maxPages dropped from the front", async () => {
|
|
748
|
+
const { getList, p } = cursorProvider();
|
|
749
|
+
const { result, rerender } = renderHook(() => useNativeResourceView(summaryView, { maxPages: 1, perPage: 1 }), { wrapper: wrap(p) });
|
|
750
|
+
await waitFor(() => expect(result.current.rows.map((r) => r.id)).toEqual(["b1"]));
|
|
751
|
+
// useInfiniteResource turns maxPages on once a render has seen the provider page by cursor.
|
|
752
|
+
rerender();
|
|
753
|
+
act(() => result.current.loadMore());
|
|
754
|
+
await waitFor(() => expect(result.current.rows.map((r) => r.id)).toEqual(["b2"]));
|
|
755
|
+
expect(result.current.hasPrevious).toBe(true);
|
|
756
|
+
act(() => result.current.loadPrevious());
|
|
757
|
+
await waitFor(() => expect(result.current.rows.map((r) => r.id)).toEqual(["b1"]));
|
|
758
|
+
expect(getList.mock.calls.at(-1)![1].pagination).toMatchObject({ cursor: "c1" });
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
it("wires the top of the list to the previous page and keeps the visible rows in place", async () => {
|
|
762
|
+
const { getList, p } = cursorProvider();
|
|
763
|
+
const { rerender } = render(<NativeResourceView view={summaryView} maxPages={1} perPage={1} />, { wrapper: wrap(p) });
|
|
764
|
+
await screen.findByTestId("native-resource-view-row-b1");
|
|
765
|
+
// useInfiniteResource turns maxPages on once a render has seen the provider page by cursor.
|
|
766
|
+
rerender(<NativeResourceView view={summaryView} maxPages={1} perPage={1} />);
|
|
767
|
+
const list = screen.getByTestId("native-resource-view-list");
|
|
768
|
+
expect(JSON.parse(list.getAttribute("data-maintain-visible-content-position")!)).toEqual({ minIndexForVisible: 0 });
|
|
769
|
+
act(() => {
|
|
770
|
+
list.dispatchEvent(new CustomEvent("endreached"));
|
|
771
|
+
});
|
|
772
|
+
await screen.findByTestId("native-resource-view-row-b2");
|
|
773
|
+
await waitFor(() => expect(screen.queryByTestId("native-resource-view-row-b1")).toBeNull());
|
|
774
|
+
act(() => {
|
|
775
|
+
screen.getByTestId("native-resource-view-list").dispatchEvent(new CustomEvent("startreached"));
|
|
776
|
+
});
|
|
777
|
+
await screen.findByTestId("native-resource-view-row-b1");
|
|
778
|
+
expect(getList.mock.calls.at(-1)![1].pagination).toMatchObject({ cursor: "c1" });
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
it("leaves fields the screen's filter fixes out of the create form, and saves the filter's value", async () => {
|
|
782
|
+
const p = provider();
|
|
783
|
+
const { result } = renderHook(() => useNativeResourceView(plainView, { filter: { status: "archived" } }), { wrapper: wrap(p) });
|
|
784
|
+
await waitFor(() => expect(result.current.rows).toHaveLength(3));
|
|
785
|
+
expect(result.current.formFields("create").map((f) => f.key)).toEqual(["name", "website", "launched_on"]);
|
|
786
|
+
// An edit still shows it: the filter only fills creates.
|
|
787
|
+
expect(result.current.formFields("edit").map((f) => f.key)).toContain("status");
|
|
788
|
+
await act(async () => {
|
|
789
|
+
expect(await result.current.save("create", { name: "Tailspin", status: "active" })).toEqual({ ok: true });
|
|
790
|
+
});
|
|
791
|
+
expect((p.create as ReturnType<typeof vi.fn>).mock.calls[0]![1].data).toMatchObject({ name: "Tailspin", status: "archived" });
|
|
792
|
+
|
|
793
|
+
render(<NativeResourceView view={plainView} filter={{ status: "archived" }} />, { wrapper: wrap(provider()) });
|
|
794
|
+
fireEvent.click(await screen.findByTestId("native-resource-view-create"));
|
|
795
|
+
await screen.findByTestId("native-resource-view-form-field-name");
|
|
796
|
+
expect(screen.queryByTestId("native-resource-view-form-field-status")).toBeNull();
|
|
797
|
+
});
|
|
798
|
+
|
|
799
|
+
it("calls the latest onRowPress without redrawing the rows", async () => {
|
|
800
|
+
const summary = vi.fn((r: Brand) => ({ title: r.name }));
|
|
801
|
+
const counted = defineView(brands, { summary });
|
|
802
|
+
const first = vi.fn();
|
|
803
|
+
const second = vi.fn();
|
|
804
|
+
const Wrapper = wrap(provider());
|
|
805
|
+
const { rerender } = render(<NativeResourceView view={counted} onRowPress={first} />, { wrapper: Wrapper });
|
|
806
|
+
await screen.findByTestId("native-resource-view-row-b1");
|
|
807
|
+
const before = summary.mock.calls.length;
|
|
808
|
+
// A screen passing an inline handler gives a new function every render.
|
|
809
|
+
rerender(<NativeResourceView view={counted} onRowPress={second} />);
|
|
810
|
+
expect(summary.mock.calls.length).toBe(before);
|
|
811
|
+
fireEvent.click(screen.getByTestId("native-resource-view-row-b1"));
|
|
812
|
+
expect(second).toHaveBeenCalledWith(expect.objectContaining({ id: "b1" }));
|
|
813
|
+
expect(first).not.toHaveBeenCalled();
|
|
814
|
+
});
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
describe("row pictures", () => {
|
|
818
|
+
const pictured = (image: (r: Brand) => { src?: string; name?: string; alt?: string } | undefined) =>
|
|
819
|
+
defineView(brands, { summary: (r) => ({ title: r.name, image: image(r) }) });
|
|
820
|
+
|
|
821
|
+
it("draws a safe image as the row's leading picture, decorative without alt", async () => {
|
|
822
|
+
const view = pictured((r) => ({ src: `https://img.example/${r.id}.png`, name: r.name }));
|
|
823
|
+
render(<NativeResourceView view={view} />, { wrapper: wrap(provider()) });
|
|
824
|
+
const image = await screen.findByTestId("native-resource-view-row-b1-image");
|
|
825
|
+
expect(image.getAttribute("aria-hidden")).toBe("true");
|
|
826
|
+
expect(screen.queryByTestId("native-resource-view-row-b1-initials")).toBeNull();
|
|
827
|
+
expect(rowItem(view, ROWS[0]!).picture).toEqual({ kind: "image", src: "https://img.example/b1.png", alt: "", fallback: "N" });
|
|
828
|
+
});
|
|
829
|
+
|
|
830
|
+
it("reads the image's alt with the row, when it has one", async () => {
|
|
831
|
+
const view = pictured((r) => ({ src: `https://img.example/${r.id}.png`, name: r.name, alt: `${r.name}'s logo` }));
|
|
832
|
+
render(<NativeResourceView view={view} />, { wrapper: wrap(provider()) });
|
|
833
|
+
expect((await screen.findByTestId("native-resource-view-row-b2")).getAttribute("aria-label")).toBe("Contoso's logo, Contoso");
|
|
834
|
+
});
|
|
835
|
+
|
|
836
|
+
it("falls back to initials when the image fails to load", async () => {
|
|
837
|
+
const view = pictured(() => ({ src: "https://img.example/missing.png", name: "Ada Lovelace" }));
|
|
838
|
+
render(<NativeResourceView view={view} />, { wrapper: wrap(provider()) });
|
|
839
|
+
const image = await screen.findByTestId("native-resource-view-row-b1-image");
|
|
840
|
+
fireEvent.error(image.querySelector("img")!);
|
|
841
|
+
expect((await screen.findByTestId("native-resource-view-row-b1-initials")).textContent).toBe("AL");
|
|
842
|
+
expect(screen.queryByTestId("native-resource-view-row-b1-image")).toBeNull();
|
|
843
|
+
});
|
|
844
|
+
|
|
845
|
+
it("draws initials when there's only a name", async () => {
|
|
846
|
+
render(<NativeResourceView view={pictured((r) => ({ name: r.name }))} />, { wrapper: wrap(provider()) });
|
|
847
|
+
expect((await screen.findByTestId("native-resource-view-row-b2-initials")).textContent).toBe("C");
|
|
848
|
+
expect(screen.queryByTestId("native-resource-view-row-b2-image")).toBeNull();
|
|
849
|
+
});
|
|
850
|
+
|
|
851
|
+
it("never loads an unsafe src: javascript: falls back to initials", async () => {
|
|
852
|
+
const view = pictured(() => ({ src: "javascript:alert(1)", name: "Ada Lovelace" }));
|
|
853
|
+
expect(rowItem(view, ROWS[0]!).picture).toEqual({ kind: "initials", text: "AL" });
|
|
854
|
+
render(<NativeResourceView view={view} />, { wrapper: wrap(provider()) });
|
|
855
|
+
expect((await screen.findByTestId("native-resource-view-row-b1-initials")).textContent).toBe("AL");
|
|
856
|
+
expect(document.querySelector("img")).toBeNull();
|
|
857
|
+
});
|
|
858
|
+
|
|
859
|
+
it("draws initials for a safe src a phone can't load (a root-relative path)", () => {
|
|
860
|
+
expect(rowItem(pictured(() => ({ src: "/logo.png", name: "Ada" })), ROWS[0]!).picture).toEqual({ kind: "initials", text: "A" });
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
it("draws nothing without a picture", async () => {
|
|
864
|
+
render(<NativeResourceView view={pictured(() => undefined)} />, { wrapper: wrap(provider()) });
|
|
865
|
+
await screen.findByTestId("native-resource-view-row-b1");
|
|
866
|
+
expect(screen.queryByTestId("native-resource-view-row-b1-image")).toBeNull();
|
|
867
|
+
expect(screen.queryByTestId("native-resource-view-row-b1-initials")).toBeNull();
|
|
868
|
+
expect(rowItem(pictured(() => ({ src: "javascript:x" })), ROWS[0]!).picture).toBeNull();
|
|
869
|
+
});
|
|
870
|
+
});
|