@terreno/admin-frontend 0.9.3 → 0.11.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/AdminFieldRenderer.js +7 -2
- package/dist/AdminFieldRenderer.js.map +1 -1
- package/dist/AdminObjectPicker.d.ts +14 -0
- package/dist/AdminObjectPicker.js +128 -0
- package/dist/AdminObjectPicker.js.map +1 -0
- package/dist/AdminPrimitiveArrayField.d.ts +26 -0
- package/dist/AdminPrimitiveArrayField.js +55 -0
- package/dist/AdminPrimitiveArrayField.js.map +1 -0
- package/dist/AdminRefField.d.ts +0 -35
- package/dist/AdminRefField.js +2 -75
- package/dist/AdminRefField.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +8 -1
- package/dist/types.js.map +1 -1
- package/package.json +5 -4
- package/src/AdminFieldRenderer.test.tsx +445 -0
- package/src/AdminFieldRenderer.tsx +22 -2
- package/src/AdminModelForm.test.tsx +541 -0
- package/src/AdminModelList.test.tsx +81 -0
- package/src/AdminModelTable.test.tsx +267 -0
- package/src/AdminObjectPicker.tsx +253 -0
- package/src/AdminPrimitiveArrayField.test.tsx +186 -0
- package/src/AdminPrimitiveArrayField.tsx +186 -0
- package/src/AdminRefField.tsx +6 -81
- package/src/AdminScriptList.test.tsx +49 -2
- package/src/AdminVersionConfig.test.tsx +196 -0
- package/src/CheckboxListEditor.test.tsx +80 -0
- package/src/ConfigurationScreen.test.tsx +140 -0
- package/src/ConsentFormEditor.test.tsx +512 -0
- package/src/ConsentFormList.test.tsx +115 -0
- package/src/ConsentResponseViewer.test.tsx +115 -0
- package/src/DocumentStorageBrowser.test.tsx +398 -0
- package/src/LocaleContentEditor.test.tsx +143 -0
- package/src/index.tsx +2 -0
- package/src/isolated/AdminFieldRenderer.isolated.tsx +289 -0
- package/src/isolated/AdminNestedArrayField.isolated.tsx +297 -0
- package/src/isolated/AdminObjectPicker.isolated.tsx +382 -0
- package/src/isolated/AdminRefField.isolated.tsx +29 -0
- package/src/isolated/AdminScriptRunModal.isolated.tsx +414 -0
- package/src/isolated/DocumentStorageBrowser.isolated.tsx +680 -0
- package/src/isolated/generateConsentPdf.isolated.ts +295 -0
- package/src/isolated/hooks.isolated.tsx +390 -0
- package/src/types.ts +8 -1
- package/src/AdminScriptRunModal.test.tsx +0 -69
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {renderWithTheme} from "@terreno/ui/src/test-utils";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import {act, fireEvent} from "../../ui/node_modules/@testing-library/react-native";
|
|
5
|
+
|
|
6
|
+
interface ListState {
|
|
7
|
+
data: any;
|
|
8
|
+
isLoading: boolean;
|
|
9
|
+
error: unknown;
|
|
10
|
+
}
|
|
11
|
+
const listState: ListState = {data: undefined, error: null, isLoading: false};
|
|
12
|
+
|
|
13
|
+
mock.module("./useAdminApi", () => ({
|
|
14
|
+
useAdminApi: () => ({
|
|
15
|
+
useListQuery: () => ({
|
|
16
|
+
data: listState.data,
|
|
17
|
+
error: listState.error,
|
|
18
|
+
isLoading: listState.isLoading,
|
|
19
|
+
}),
|
|
20
|
+
}),
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
import {ConsentFormList} from "./ConsentFormList";
|
|
24
|
+
|
|
25
|
+
describe("ConsentFormList", () => {
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
listState.data = undefined;
|
|
28
|
+
listState.isLoading = false;
|
|
29
|
+
listState.error = null;
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("renders loading state", () => {
|
|
33
|
+
listState.isLoading = true;
|
|
34
|
+
const {toJSON} = renderWithTheme(<ConsentFormList api={{} as any} baseUrl="/admin" />);
|
|
35
|
+
expect(toJSON()).toBeDefined();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("renders error state", () => {
|
|
39
|
+
listState.error = new Error("x");
|
|
40
|
+
const {getByText} = renderWithTheme(<ConsentFormList api={{} as any} baseUrl="/admin" />);
|
|
41
|
+
expect(getByText(/Failed to load consent forms/)).toBeDefined();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("renders empty state", () => {
|
|
45
|
+
listState.data = {data: [], total: 0};
|
|
46
|
+
const {getByText} = renderWithTheme(<ConsentFormList api={{} as any} baseUrl="/admin" />);
|
|
47
|
+
expect(getByText(/No consent forms found/)).toBeDefined();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("renders data without onRowClick so the ActionsCell edit-callback branch is absent", () => {
|
|
51
|
+
listState.data = {
|
|
52
|
+
data: [{_id: "a", active: true, order: 1, title: "T1", type: "standard", version: "2"}],
|
|
53
|
+
total: 1,
|
|
54
|
+
};
|
|
55
|
+
// No onRowClick is provided. The ActionsCell edit-button path should be hidden.
|
|
56
|
+
const {toJSON} = renderWithTheme(<ConsentFormList api={{} as any} baseUrl="/admin" />);
|
|
57
|
+
expect(toJSON()).toBeDefined();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("renders data with create button and edit callback", async () => {
|
|
61
|
+
listState.data = {
|
|
62
|
+
data: [
|
|
63
|
+
{
|
|
64
|
+
_id: "a",
|
|
65
|
+
active: true,
|
|
66
|
+
order: 1,
|
|
67
|
+
title: "T1",
|
|
68
|
+
type: "standard",
|
|
69
|
+
version: "2",
|
|
70
|
+
},
|
|
71
|
+
{_id: "b", active: false, order: "x", title: "T2", type: "privacy", version: 3},
|
|
72
|
+
],
|
|
73
|
+
total: 2,
|
|
74
|
+
};
|
|
75
|
+
const onCreateNew = mock(() => undefined);
|
|
76
|
+
const onRowClick = mock((_: string) => undefined);
|
|
77
|
+
const {getByTestId} = renderWithTheme(
|
|
78
|
+
<ConsentFormList
|
|
79
|
+
api={{} as any}
|
|
80
|
+
baseUrl="/admin"
|
|
81
|
+
onCreateNew={onCreateNew}
|
|
82
|
+
onRowClick={onRowClick}
|
|
83
|
+
/>
|
|
84
|
+
);
|
|
85
|
+
await act(async () => {
|
|
86
|
+
fireEvent.press(getByTestId("consent-form-list-create-button"));
|
|
87
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
88
|
+
});
|
|
89
|
+
expect(onCreateNew).toHaveBeenCalled();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("falls back when sort column is out of range (buildSortString returns undefined)", async () => {
|
|
93
|
+
listState.data = {
|
|
94
|
+
data: [{_id: "a", active: true, order: 1, title: "T1", type: "standard", version: "2"}],
|
|
95
|
+
total: 1,
|
|
96
|
+
};
|
|
97
|
+
const {UNSAFE_root, toJSON} = renderWithTheme(
|
|
98
|
+
<ConsentFormList api={{} as any} baseUrl="/admin" onRowClick={() => undefined} />
|
|
99
|
+
);
|
|
100
|
+
const tables = UNSAFE_root.findAll((n: any) => typeof n.props?.setSortColumn === "function");
|
|
101
|
+
expect(tables.length).toBeGreaterThan(0);
|
|
102
|
+
await act(async () => {
|
|
103
|
+
// Column 99 is out of range, so buildSortString returns undefined and
|
|
104
|
+
// the default "-created" sort should be used.
|
|
105
|
+
(tables[0] as any).props.setSortColumn({column: 99, direction: "asc"});
|
|
106
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
107
|
+
});
|
|
108
|
+
// Also exercise the desc branch on a valid column.
|
|
109
|
+
await act(async () => {
|
|
110
|
+
(tables[0] as any).props.setSortColumn({column: 0, direction: "desc"});
|
|
111
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
112
|
+
});
|
|
113
|
+
expect(toJSON()).toBeDefined();
|
|
114
|
+
});
|
|
115
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {renderWithTheme} from "@terreno/ui/src/test-utils";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import {act, fireEvent} from "../../ui/node_modules/@testing-library/react-native";
|
|
5
|
+
|
|
6
|
+
interface ReadState {
|
|
7
|
+
data: any;
|
|
8
|
+
isLoading: boolean;
|
|
9
|
+
}
|
|
10
|
+
const readState: ReadState = {data: undefined, isLoading: false};
|
|
11
|
+
|
|
12
|
+
mock.module("./useAdminApi", () => ({
|
|
13
|
+
useAdminApi: () => ({
|
|
14
|
+
useReadQuery: (_id: string, opts: {skip?: boolean}) => {
|
|
15
|
+
if (opts?.skip) {
|
|
16
|
+
return {data: undefined, isLoading: false};
|
|
17
|
+
}
|
|
18
|
+
return {data: readState.data, isLoading: readState.isLoading};
|
|
19
|
+
},
|
|
20
|
+
}),
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
const pdfCalls: any[] = [];
|
|
24
|
+
let pdfImpl: (r: any) => Promise<void> = async () => {};
|
|
25
|
+
mock.module("./generateConsentPdf", () => ({
|
|
26
|
+
generateConsentPdf: async (r: any) => {
|
|
27
|
+
pdfCalls.push(r);
|
|
28
|
+
return pdfImpl(r);
|
|
29
|
+
},
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
import {ConsentResponseViewer} from "./ConsentResponseViewer";
|
|
33
|
+
|
|
34
|
+
describe("ConsentResponseViewer", () => {
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
readState.data = undefined;
|
|
37
|
+
readState.isLoading = false;
|
|
38
|
+
pdfCalls.length = 0;
|
|
39
|
+
pdfImpl = async () => {};
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("renders loading state", () => {
|
|
43
|
+
readState.isLoading = true;
|
|
44
|
+
const {toJSON} = renderWithTheme(
|
|
45
|
+
<ConsentResponseViewer api={{} as any} baseUrl="/admin" id="r1" />
|
|
46
|
+
);
|
|
47
|
+
expect(toJSON()).toBeDefined();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("shows not-found state when there is no id / data", () => {
|
|
51
|
+
const {getByText} = renderWithTheme(
|
|
52
|
+
<ConsentResponseViewer api={{} as any} baseUrl="/admin" id="" />
|
|
53
|
+
);
|
|
54
|
+
expect(getByText(/Response not found\./)).toBeDefined();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("renders a full response and triggers PDF download", async () => {
|
|
58
|
+
readState.data = {
|
|
59
|
+
agreed: true,
|
|
60
|
+
agreedAt: "2024-01-02T03:04:05.000Z",
|
|
61
|
+
checkboxValues: {0: true, 1: false},
|
|
62
|
+
consentFormId: {title: "My Form"},
|
|
63
|
+
contentSnapshot: "# Hello",
|
|
64
|
+
formVersionSnapshot: {title: "My Form", version: 1},
|
|
65
|
+
ipAddress: "127.0.0.1",
|
|
66
|
+
locale: "en",
|
|
67
|
+
signature: "data:image/png;base64,abc",
|
|
68
|
+
userAgent: "jest",
|
|
69
|
+
userId: {_id: "u1"},
|
|
70
|
+
};
|
|
71
|
+
const {getByText} = renderWithTheme(
|
|
72
|
+
<ConsentResponseViewer api={{} as any} baseUrl="/admin" id="r1" />
|
|
73
|
+
);
|
|
74
|
+
await act(async () => {
|
|
75
|
+
fireEvent.press(getByText("Download PDF"));
|
|
76
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
77
|
+
});
|
|
78
|
+
expect(pdfCalls.length).toBe(1);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("renders minimal declined response without optional sections", () => {
|
|
82
|
+
readState.data = {
|
|
83
|
+
agreed: false,
|
|
84
|
+
consentFormId: "raw-id",
|
|
85
|
+
userId: "u2",
|
|
86
|
+
};
|
|
87
|
+
const {toJSON} = renderWithTheme(
|
|
88
|
+
<ConsentResponseViewer api={{} as any} baseUrl="/admin" id="r2" />
|
|
89
|
+
);
|
|
90
|
+
expect(toJSON()).toBeDefined();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("handles PDF generation errors", async () => {
|
|
94
|
+
readState.data = {agreed: true};
|
|
95
|
+
pdfImpl = async () => {
|
|
96
|
+
throw new Error("broken");
|
|
97
|
+
};
|
|
98
|
+
const {getByText} = renderWithTheme(
|
|
99
|
+
<ConsentResponseViewer api={{} as any} baseUrl="/admin" id="r1" />
|
|
100
|
+
);
|
|
101
|
+
await act(async () => {
|
|
102
|
+
fireEvent.press(getByText("Download PDF"));
|
|
103
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
104
|
+
});
|
|
105
|
+
expect(pdfCalls.length).toBe(1);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("formats invalid dates gracefully", () => {
|
|
109
|
+
readState.data = {agreed: true, agreedAt: "not-a-date"};
|
|
110
|
+
const {toJSON} = renderWithTheme(
|
|
111
|
+
<ConsentResponseViewer api={{} as any} baseUrl="/admin" id="r1" />
|
|
112
|
+
);
|
|
113
|
+
expect(toJSON()).toBeDefined();
|
|
114
|
+
});
|
|
115
|
+
});
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {renderWithTheme} from "@terreno/ui/src/test-utils";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import {Platform} from "react-native";
|
|
5
|
+
import {act, fireEvent} from "../../ui/node_modules/@testing-library/react-native";
|
|
6
|
+
|
|
7
|
+
mock.module("react-native-webview", () => ({
|
|
8
|
+
WebView: (props: any) => React.createElement("WebView", {...props, testID: "mock-webview"}),
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
interface ListState {
|
|
12
|
+
data: any;
|
|
13
|
+
isLoading: boolean;
|
|
14
|
+
isError: boolean;
|
|
15
|
+
error: any;
|
|
16
|
+
}
|
|
17
|
+
const listState: ListState = {data: undefined, error: null, isError: false, isLoading: false};
|
|
18
|
+
const refetchMock = mock(() => {});
|
|
19
|
+
const uploadCalls: any[] = [];
|
|
20
|
+
const deleteCalls: string[] = [];
|
|
21
|
+
const deleteFolderCalls: string[] = [];
|
|
22
|
+
const createFolderCalls: any[] = [];
|
|
23
|
+
const downloadCalls: string[] = [];
|
|
24
|
+
let uploadImpl: (body: any) => Promise<any> = async () => ({});
|
|
25
|
+
let deleteImpl: (p: string) => Promise<any> = async () => ({});
|
|
26
|
+
let deleteFolderImpl: (p: string) => Promise<any> = async () => ({});
|
|
27
|
+
let createFolderImpl: (body: any) => Promise<any> = async () => ({});
|
|
28
|
+
let downloadImpl: (p: string) => Promise<any> = async () => new Blob(["hi"]);
|
|
29
|
+
|
|
30
|
+
mock.module("./useDocumentStorageApi", () => ({
|
|
31
|
+
useDocumentStorageApi: () => ({
|
|
32
|
+
useCreateFolderMutation: () => [
|
|
33
|
+
(body: any) => ({
|
|
34
|
+
unwrap: async () => {
|
|
35
|
+
createFolderCalls.push(body);
|
|
36
|
+
return createFolderImpl(body);
|
|
37
|
+
},
|
|
38
|
+
}),
|
|
39
|
+
],
|
|
40
|
+
useDeleteFolderMutation: () => [
|
|
41
|
+
(p: string) => ({
|
|
42
|
+
unwrap: async () => {
|
|
43
|
+
deleteFolderCalls.push(p);
|
|
44
|
+
return deleteFolderImpl(p);
|
|
45
|
+
},
|
|
46
|
+
}),
|
|
47
|
+
],
|
|
48
|
+
useDeleteMutation: () => [
|
|
49
|
+
(p: string) => ({
|
|
50
|
+
unwrap: async () => {
|
|
51
|
+
deleteCalls.push(p);
|
|
52
|
+
return deleteImpl(p);
|
|
53
|
+
},
|
|
54
|
+
}),
|
|
55
|
+
],
|
|
56
|
+
useLazyDownloadQuery: () => [
|
|
57
|
+
(p: string) => ({
|
|
58
|
+
unwrap: async () => {
|
|
59
|
+
downloadCalls.push(p);
|
|
60
|
+
return downloadImpl(p);
|
|
61
|
+
},
|
|
62
|
+
}),
|
|
63
|
+
],
|
|
64
|
+
useListQuery: (_prefix: string | undefined, opts: {skip?: boolean}) => {
|
|
65
|
+
if (opts?.skip) {
|
|
66
|
+
return {
|
|
67
|
+
data: undefined,
|
|
68
|
+
error: null,
|
|
69
|
+
isError: false,
|
|
70
|
+
isLoading: false,
|
|
71
|
+
refetch: refetchMock,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return {...listState, refetch: refetchMock};
|
|
75
|
+
},
|
|
76
|
+
useUploadMutation: () => [
|
|
77
|
+
(body: any) => ({
|
|
78
|
+
unwrap: async () => {
|
|
79
|
+
uploadCalls.push(body);
|
|
80
|
+
return uploadImpl(body);
|
|
81
|
+
},
|
|
82
|
+
}),
|
|
83
|
+
{isLoading: false},
|
|
84
|
+
],
|
|
85
|
+
}),
|
|
86
|
+
}));
|
|
87
|
+
|
|
88
|
+
import {DocumentStorageBrowser} from "./DocumentStorageBrowser";
|
|
89
|
+
|
|
90
|
+
const press = async (el: any): Promise<void> => {
|
|
91
|
+
await act(async () => {
|
|
92
|
+
fireEvent.press(el);
|
|
93
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
describe("DocumentStorageBrowser", () => {
|
|
98
|
+
const originalPlatformOS = Platform.OS;
|
|
99
|
+
|
|
100
|
+
beforeEach(() => {
|
|
101
|
+
listState.data = undefined;
|
|
102
|
+
listState.isLoading = false;
|
|
103
|
+
listState.isError = false;
|
|
104
|
+
listState.error = null;
|
|
105
|
+
refetchMock.mockClear();
|
|
106
|
+
uploadCalls.length = 0;
|
|
107
|
+
deleteCalls.length = 0;
|
|
108
|
+
deleteFolderCalls.length = 0;
|
|
109
|
+
createFolderCalls.length = 0;
|
|
110
|
+
downloadCalls.length = 0;
|
|
111
|
+
uploadImpl = async () => ({});
|
|
112
|
+
deleteImpl = async () => ({});
|
|
113
|
+
deleteFolderImpl = async () => ({});
|
|
114
|
+
createFolderImpl = async () => ({});
|
|
115
|
+
downloadImpl = async () => new Blob(["hi"]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
afterEach(() => {
|
|
119
|
+
Object.defineProperty(Platform, "OS", {configurable: true, value: originalPlatformOS});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("renders loading state", () => {
|
|
123
|
+
listState.isLoading = true;
|
|
124
|
+
const {toJSON} = renderWithTheme(
|
|
125
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
126
|
+
);
|
|
127
|
+
expect(toJSON()).toBeDefined();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("renders error state", () => {
|
|
131
|
+
listState.isError = true;
|
|
132
|
+
listState.error = {status: 500};
|
|
133
|
+
const {getByText} = renderWithTheme(
|
|
134
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
135
|
+
);
|
|
136
|
+
expect(getByText(/Failed to load files/)).toBeDefined();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("renders not-configured state on 503", () => {
|
|
140
|
+
listState.isError = true;
|
|
141
|
+
listState.error = {status: 503};
|
|
142
|
+
const onSettingsPress = mock(() => undefined);
|
|
143
|
+
const {getByText} = renderWithTheme(
|
|
144
|
+
<DocumentStorageBrowser
|
|
145
|
+
api={{} as any}
|
|
146
|
+
basePath="/documents"
|
|
147
|
+
onSettingsPress={onSettingsPress}
|
|
148
|
+
/>
|
|
149
|
+
);
|
|
150
|
+
expect(getByText(/Storage is not configured/)).toBeDefined();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("renders empty state when no files or folders", () => {
|
|
154
|
+
listState.data = {files: [], folders: []};
|
|
155
|
+
const {getByText} = renderWithTheme(
|
|
156
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
157
|
+
);
|
|
158
|
+
expect(getByText(/No files found/)).toBeDefined();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("renders a data table with files and folders", () => {
|
|
162
|
+
listState.data = {
|
|
163
|
+
files: [
|
|
164
|
+
{
|
|
165
|
+
contentType: "image/png",
|
|
166
|
+
fullPath: "a.png",
|
|
167
|
+
name: "a.png",
|
|
168
|
+
size: 2048,
|
|
169
|
+
updated: "2024-01-01T00:00:00Z",
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
contentType: null,
|
|
173
|
+
fullPath: "b.bin",
|
|
174
|
+
name: "b.bin",
|
|
175
|
+
size: 0,
|
|
176
|
+
updated: "",
|
|
177
|
+
},
|
|
178
|
+
],
|
|
179
|
+
folders: ["sub/"],
|
|
180
|
+
};
|
|
181
|
+
const {toJSON} = renderWithTheme(
|
|
182
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
183
|
+
);
|
|
184
|
+
expect(toJSON()).toBeDefined();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("refreshes the list when refresh is pressed", async () => {
|
|
188
|
+
listState.data = {files: [], folders: []};
|
|
189
|
+
const {getByTestId} = renderWithTheme(
|
|
190
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
191
|
+
);
|
|
192
|
+
await press(getByTestId("document-refresh-button"));
|
|
193
|
+
expect(refetchMock).toHaveBeenCalled();
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("opens the new-folder modal when pressed", async () => {
|
|
197
|
+
listState.data = {files: [], folders: []};
|
|
198
|
+
const {getByTestId, toJSON} = renderWithTheme(
|
|
199
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
200
|
+
);
|
|
201
|
+
await press(getByTestId("document-new-folder-button"));
|
|
202
|
+
expect(toJSON()).toBeDefined();
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("does not show upload/new folder when allowUpload is false", () => {
|
|
206
|
+
listState.data = {files: [], folders: []};
|
|
207
|
+
const {queryByTestId} = renderWithTheme(
|
|
208
|
+
<DocumentStorageBrowser allowUpload={false} api={{} as any} basePath="/documents" />
|
|
209
|
+
);
|
|
210
|
+
expect(queryByTestId("document-upload-button")).toBeNull();
|
|
211
|
+
expect(queryByTestId("document-new-folder-button")).toBeNull();
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("uses onFileSelect when provided", () => {
|
|
215
|
+
listState.data = {
|
|
216
|
+
files: [{contentType: "text/plain", fullPath: "a.txt", name: "a.txt", size: 1, updated: ""}],
|
|
217
|
+
folders: [],
|
|
218
|
+
};
|
|
219
|
+
const onFileSelect = mock(() => undefined);
|
|
220
|
+
const {toJSON} = renderWithTheme(
|
|
221
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" onFileSelect={onFileSelect} />
|
|
222
|
+
);
|
|
223
|
+
expect(toJSON()).toBeDefined();
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("renders settings button when onSettingsPress is provided", () => {
|
|
227
|
+
listState.data = {files: [], folders: []};
|
|
228
|
+
const onSettingsPress = mock(() => undefined);
|
|
229
|
+
const {toJSON} = renderWithTheme(
|
|
230
|
+
<DocumentStorageBrowser
|
|
231
|
+
api={{} as any}
|
|
232
|
+
basePath="/documents"
|
|
233
|
+
onSettingsPress={onSettingsPress}
|
|
234
|
+
/>
|
|
235
|
+
);
|
|
236
|
+
expect(toJSON()).toBeDefined();
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("does not render when allowDelete is false (folder actions return null)", () => {
|
|
240
|
+
listState.data = {files: [], folders: ["sub/"]};
|
|
241
|
+
const {toJSON} = renderWithTheme(
|
|
242
|
+
<DocumentStorageBrowser allowDelete={false} api={{} as any} basePath="/documents" />
|
|
243
|
+
);
|
|
244
|
+
expect(toJSON()).toBeDefined();
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("uses custom title when provided", () => {
|
|
248
|
+
listState.data = {files: [], folders: []};
|
|
249
|
+
const {getByText} = renderWithTheme(
|
|
250
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" title="Custom Storage" />
|
|
251
|
+
);
|
|
252
|
+
expect(getByText("Custom Storage")).toBeDefined();
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("reconfigures and refetches when refresh is pressed after 503", async () => {
|
|
256
|
+
listState.isError = true;
|
|
257
|
+
listState.error = {status: 503};
|
|
258
|
+
const {getByTestId, rerender} = renderWithTheme(
|
|
259
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
260
|
+
);
|
|
261
|
+
await act(async () => {
|
|
262
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
263
|
+
});
|
|
264
|
+
await press(getByTestId("document-refresh-button"));
|
|
265
|
+
listState.isError = false;
|
|
266
|
+
listState.error = null;
|
|
267
|
+
listState.data = {files: [], folders: []};
|
|
268
|
+
rerender(<DocumentStorageBrowser api={{} as any} basePath="/documents" />);
|
|
269
|
+
expect(true).toBe(true);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("supports originalStatus 503 error shape", () => {
|
|
273
|
+
listState.isError = true;
|
|
274
|
+
listState.error = {originalStatus: 503};
|
|
275
|
+
const {getByText} = renderWithTheme(
|
|
276
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
277
|
+
);
|
|
278
|
+
expect(getByText(/Storage is not configured/)).toBeDefined();
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("renders breadcrumbs that can be clicked", () => {
|
|
282
|
+
listState.data = {files: [], folders: []};
|
|
283
|
+
const {toJSON} = renderWithTheme(
|
|
284
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
285
|
+
);
|
|
286
|
+
// No current prefix, only root crumb
|
|
287
|
+
expect(toJSON()).toBeDefined();
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("renders new folder modal markup when opened", async () => {
|
|
291
|
+
listState.data = {files: [], folders: []};
|
|
292
|
+
const {getByTestId, toJSON} = renderWithTheme(
|
|
293
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
294
|
+
);
|
|
295
|
+
await press(getByTestId("document-new-folder-button"));
|
|
296
|
+
expect(toJSON()).toBeDefined();
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("uploads a file when the hidden input fires change", async () => {
|
|
300
|
+
Object.defineProperty(Platform, "OS", {configurable: true, value: "web"});
|
|
301
|
+
listState.data = {files: [], folders: []};
|
|
302
|
+
const {UNSAFE_getAllByType} = renderWithTheme(
|
|
303
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
304
|
+
);
|
|
305
|
+
const hostInputs = UNSAFE_getAllByType("input" as any);
|
|
306
|
+
const fileInput = hostInputs.find((node: any) => node.props?.type === "file") as any;
|
|
307
|
+
expect(fileInput).toBeDefined();
|
|
308
|
+
const fakeFile = new File(["x"], "x.txt", {type: "text/plain"});
|
|
309
|
+
await act(async () => {
|
|
310
|
+
fileInput.props.onChange({target: {files: [fakeFile], value: ""}});
|
|
311
|
+
await new Promise((r) => setTimeout(r, 30));
|
|
312
|
+
});
|
|
313
|
+
expect(uploadCalls.length).toBe(1);
|
|
314
|
+
expect(uploadCalls[0].prefix).toBeUndefined();
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("ignores upload change when no file selected", async () => {
|
|
318
|
+
Object.defineProperty(Platform, "OS", {configurable: true, value: "web"});
|
|
319
|
+
listState.data = {files: [], folders: []};
|
|
320
|
+
const {UNSAFE_getAllByType} = renderWithTheme(
|
|
321
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
322
|
+
);
|
|
323
|
+
const hostInputs = UNSAFE_getAllByType("input" as any);
|
|
324
|
+
const fileInput = hostInputs.find((node: any) => node.props?.type === "file") as any;
|
|
325
|
+
await act(async () => {
|
|
326
|
+
fileInput.props.onChange({target: {files: []}});
|
|
327
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
328
|
+
});
|
|
329
|
+
expect(uploadCalls.length).toBe(0);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it("handles upload errors without throwing", async () => {
|
|
333
|
+
Object.defineProperty(Platform, "OS", {configurable: true, value: "web"});
|
|
334
|
+
listState.data = {files: [], folders: []};
|
|
335
|
+
uploadImpl = async () => {
|
|
336
|
+
throw new Error("upload failed");
|
|
337
|
+
};
|
|
338
|
+
const {UNSAFE_getAllByType} = renderWithTheme(
|
|
339
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
340
|
+
);
|
|
341
|
+
const hostInputs = UNSAFE_getAllByType("input" as any);
|
|
342
|
+
const fileInput = hostInputs.find((node: any) => node.props?.type === "file") as any;
|
|
343
|
+
const fakeFile = new File(["x"], "y.txt", {type: "text/plain"});
|
|
344
|
+
await act(async () => {
|
|
345
|
+
fileInput.props.onChange({target: {files: [fakeFile], value: ""}});
|
|
346
|
+
await new Promise((r) => setTimeout(r, 30));
|
|
347
|
+
});
|
|
348
|
+
expect(uploadCalls.length).toBe(1);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("renders folder rows with folder delete action available", () => {
|
|
352
|
+
listState.data = {
|
|
353
|
+
files: [
|
|
354
|
+
{
|
|
355
|
+
contentType: "application/pdf",
|
|
356
|
+
fullPath: "doc.pdf",
|
|
357
|
+
name: "doc.pdf",
|
|
358
|
+
size: 1024,
|
|
359
|
+
updated: "2024-01-01T00:00:00Z",
|
|
360
|
+
},
|
|
361
|
+
],
|
|
362
|
+
folders: ["sub-folder/"],
|
|
363
|
+
};
|
|
364
|
+
const {toJSON} = renderWithTheme(
|
|
365
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
366
|
+
);
|
|
367
|
+
expect(toJSON()).toBeDefined();
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it("formats file sizes across all unit ranges", () => {
|
|
371
|
+
listState.data = {
|
|
372
|
+
files: [
|
|
373
|
+
{contentType: "b/b", fullPath: "0", name: "0", size: 0, updated: ""},
|
|
374
|
+
{contentType: "b/b", fullPath: "b", name: "b", size: 512, updated: ""},
|
|
375
|
+
{contentType: "b/b", fullPath: "kb", name: "kb", size: 2048, updated: ""},
|
|
376
|
+
{
|
|
377
|
+
contentType: "b/b",
|
|
378
|
+
fullPath: "mb",
|
|
379
|
+
name: "mb",
|
|
380
|
+
size: 5 * 1024 * 1024,
|
|
381
|
+
updated: "",
|
|
382
|
+
},
|
|
383
|
+
{
|
|
384
|
+
contentType: "b/b",
|
|
385
|
+
fullPath: "gb",
|
|
386
|
+
name: "gb",
|
|
387
|
+
size: 3 * 1024 * 1024 * 1024,
|
|
388
|
+
updated: "",
|
|
389
|
+
},
|
|
390
|
+
],
|
|
391
|
+
folders: [],
|
|
392
|
+
};
|
|
393
|
+
const {toJSON} = renderWithTheme(
|
|
394
|
+
<DocumentStorageBrowser api={{} as any} basePath="/documents" />
|
|
395
|
+
);
|
|
396
|
+
expect(toJSON()).toBeDefined();
|
|
397
|
+
});
|
|
398
|
+
});
|