@terreno/ui 0.30.0 → 0.31.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.
@@ -0,0 +1,253 @@
1
+ import {beforeEach, describe, expect, it, mock} from "bun:test";
2
+
3
+ import type {ConsentHistoryEntry} from "./useConsentHistory";
4
+
5
+ interface PdfCall {
6
+ args: unknown[];
7
+ method: string;
8
+ }
9
+
10
+ interface MockDoc {
11
+ calls: PdfCall[];
12
+ savedFilename?: string;
13
+ }
14
+
15
+ let docs: MockDoc[] = [];
16
+ let addImageShouldThrow = false;
17
+
18
+ class MockJsPdf {
19
+ calls: PdfCall[] = [];
20
+ savedFilename?: string;
21
+
22
+ constructor(..._args: unknown[]) {
23
+ docs.push(this as unknown as MockDoc);
24
+ }
25
+
26
+ private record(method: string, args: unknown[]): void {
27
+ this.calls.push({args, method});
28
+ }
29
+
30
+ setFontSize(...args: unknown[]): void {
31
+ this.record("setFontSize", args);
32
+ }
33
+
34
+ setFont(...args: unknown[]): void {
35
+ this.record("setFont", args);
36
+ }
37
+
38
+ setDrawColor(...args: unknown[]): void {
39
+ this.record("setDrawColor", args);
40
+ }
41
+
42
+ setTextColor(...args: unknown[]): void {
43
+ this.record("setTextColor", args);
44
+ }
45
+
46
+ text(...args: unknown[]): void {
47
+ this.record("text", args);
48
+ }
49
+
50
+ line(...args: unknown[]): void {
51
+ this.record("line", args);
52
+ }
53
+
54
+ addPage(...args: unknown[]): void {
55
+ this.record("addPage", args);
56
+ }
57
+
58
+ addImage(...args: unknown[]): void {
59
+ this.record("addImage", args);
60
+ if (addImageShouldThrow) {
61
+ throw new Error("cannot embed image");
62
+ }
63
+ }
64
+
65
+ splitTextToSize(textValue: string, ..._rest: unknown[]): string[] {
66
+ this.record("splitTextToSize", [textValue]);
67
+ return String(textValue).split("\n");
68
+ }
69
+
70
+ save(...args: unknown[]): void {
71
+ this.record("save", args);
72
+ this.savedFilename = String(args[0]);
73
+ }
74
+ }
75
+
76
+ mock.module("jspdf", () => ({jsPDF: MockJsPdf}));
77
+
78
+ const {generateConsentHistoryPdf} = await import("./generateConsentHistoryPdf");
79
+
80
+ const lastDoc = (): MockDoc => docs[docs.length - 1]!;
81
+
82
+ const textStrings = (doc: MockDoc): string[] =>
83
+ doc.calls.filter((c) => c.method === "text").map((c) => String(c.args[0]));
84
+
85
+ const wasCalled = (doc: MockDoc, method: string): boolean =>
86
+ doc.calls.some((c) => c.method === method);
87
+
88
+ const baseEntry: ConsentHistoryEntry = {
89
+ _id: "response-1",
90
+ agreed: true,
91
+ agreedAt: "2024-01-15T10:30:00.000Z",
92
+ form: {
93
+ captureSignature: true,
94
+ checkboxes: [
95
+ {label: "I agree to the terms", required: true},
96
+ {label: "I consent to data use", required: false},
97
+ ],
98
+ slug: "privacy-policy",
99
+ title: "Privacy Policy",
100
+ type: "consent",
101
+ version: 3,
102
+ },
103
+ };
104
+
105
+ describe("generateConsentHistoryPdf", () => {
106
+ beforeEach(() => {
107
+ docs = [];
108
+ addImageShouldThrow = false;
109
+ });
110
+
111
+ it("renders a complete consent record with every section", async () => {
112
+ const entry: ConsentHistoryEntry = {
113
+ ...baseEntry,
114
+ checkboxValues: {"0": true, "1": false, "2": true},
115
+ contentSnapshot: "First line of the snapshot\nSecond line of the snapshot",
116
+ formVersionSnapshot: 3,
117
+ ipAddress: "203.0.113.7",
118
+ locale: "en-US",
119
+ signature: "data:image/png;base64,AAAA",
120
+ signedAt: "2024-01-15T10:31:00.000Z",
121
+ userAgent: "Mozilla/5.0",
122
+ };
123
+
124
+ await generateConsentHistoryPdf(entry);
125
+
126
+ const doc = lastDoc();
127
+ const texts = textStrings(doc);
128
+ expect(texts).toContain("Consent Record");
129
+ expect(texts).toContain("Privacy Policy");
130
+ expect(texts).toContain("Response Details");
131
+ expect(texts).toContain("Checkbox Responses");
132
+ expect(texts).toContain("Audit Trail");
133
+ expect(texts).toContain("Signature");
134
+ expect(texts).toContain("Content Snapshot");
135
+ // Known checkbox labels render with their check state.
136
+ expect(texts).toContain("[x] I agree to the terms");
137
+ expect(texts).toContain("[ ] I consent to data use");
138
+ // Unknown checkbox index falls back to a generated label.
139
+ expect(texts).toContain("[x] Checkbox 2");
140
+ // Agreed decision.
141
+ expect(texts.some((t) => t === "Agreed")).toBe(true);
142
+ // Snapshot lines are split and rendered individually.
143
+ expect(texts).toContain("First line of the snapshot");
144
+ expect(texts).toContain("Second line of the snapshot");
145
+ // Image embedded as PNG.
146
+ const image = doc.calls.find((c) => c.method === "addImage");
147
+ expect(image?.args[1]).toBe("PNG");
148
+ // Footer includes the response id.
149
+ expect(texts.some((t) => t.includes("Response ID: response-1"))).toBe(true);
150
+ // Saved with a slug-based filename.
151
+ expect(doc.savedFilename).toContain("consent-privacy-policy-");
152
+ expect(doc.savedFilename?.endsWith(".pdf")).toBe(true);
153
+ });
154
+
155
+ it("handles a minimal declined entry with no optional sections", async () => {
156
+ const entry: ConsentHistoryEntry = {
157
+ _id: "",
158
+ agreed: false,
159
+ agreedAt: "",
160
+ form: null,
161
+ };
162
+
163
+ await generateConsentHistoryPdf(entry);
164
+
165
+ const doc = lastDoc();
166
+ const texts = textStrings(doc);
167
+ expect(texts).toContain("Unknown Form");
168
+ expect(texts.some((t) => t === "Declined")).toBe(true);
169
+ expect(texts).not.toContain("Checkbox Responses");
170
+ expect(texts).not.toContain("Audit Trail");
171
+ expect(texts).not.toContain("Signature");
172
+ expect(texts).not.toContain("Content Snapshot");
173
+ expect(wasCalled(doc, "addImage")).toBe(false);
174
+ // No _id means no response-id footer line.
175
+ expect(texts.some((t) => t.includes("Response ID:"))).toBe(false);
176
+ // Falls back to "response" in the filename when there is no slug.
177
+ expect(doc.savedFilename).toContain("consent-response-");
178
+ });
179
+
180
+ it("embeds a JPEG signature when the data URI is not PNG", async () => {
181
+ const entry: ConsentHistoryEntry = {
182
+ ...baseEntry,
183
+ signature: "data:image/jpeg;base64,BBBB",
184
+ };
185
+
186
+ await generateConsentHistoryPdf(entry);
187
+
188
+ const doc = lastDoc();
189
+ const image = doc.calls.find((c) => c.method === "addImage");
190
+ expect(image?.args[1]).toBe("JPEG");
191
+ });
192
+
193
+ it("falls back to a placeholder when the signature image cannot be embedded", async () => {
194
+ addImageShouldThrow = true;
195
+ const entry: ConsentHistoryEntry = {
196
+ ...baseEntry,
197
+ signature: "data:image/png;base64,CCCC",
198
+ };
199
+
200
+ await generateConsentHistoryPdf(entry);
201
+
202
+ const doc = lastDoc();
203
+ const texts = textStrings(doc);
204
+ expect(texts).toContain("(Signature image could not be embedded)");
205
+ });
206
+
207
+ it("adds pages when content overflows the page height", async () => {
208
+ const manyLines = Array.from({length: 200}, (_v, i) => `Line ${i}`).join("\n");
209
+ const entry: ConsentHistoryEntry = {
210
+ ...baseEntry,
211
+ contentSnapshot: manyLines,
212
+ };
213
+
214
+ await generateConsentHistoryPdf(entry);
215
+
216
+ const doc = lastDoc();
217
+ expect(wasCalled(doc, "addPage")).toBe(true);
218
+ });
219
+
220
+ it("uses the audit-trail form version when the form has no version", async () => {
221
+ const entry: ConsentHistoryEntry = {
222
+ _id: "id-2",
223
+ agreed: true,
224
+ agreedAt: "2024-01-15T10:30:00.000Z",
225
+ form: null,
226
+ formVersionSnapshot: 7,
227
+ };
228
+
229
+ await generateConsentHistoryPdf(entry);
230
+
231
+ const doc = lastDoc();
232
+ const texts = textStrings(doc);
233
+ // formVersion resolves from formVersionSnapshot in Response Details...
234
+ expect(texts).toContain("Audit Trail");
235
+ // ...and the snapshot version shows up as a value.
236
+ expect(texts.some((t) => t === "7")).toBe(true);
237
+ });
238
+
239
+ it("renders raw invalid dates and skips empty dates via formatDate", async () => {
240
+ const entry: ConsentHistoryEntry = {
241
+ ...baseEntry,
242
+ agreedAt: "not-a-real-date",
243
+ signedAt: "",
244
+ };
245
+
246
+ await generateConsentHistoryPdf(entry);
247
+
248
+ const doc = lastDoc();
249
+ const texts = textStrings(doc);
250
+ // Invalid date string is passed through unchanged.
251
+ expect(texts).toContain("not-a-real-date");
252
+ });
253
+ });
package/src/index.tsx CHANGED
@@ -28,6 +28,7 @@ export * from "./DateUtilities";
28
28
  export * from "./DecimalRangeActionSheet";
29
29
  export * from "./DismissButton";
30
30
  export {DraggableList} from "./DraggableList";
31
+ export * from "./EditableCard";
31
32
  export * from "./EmailField";
32
33
  export {default as EmojiSelector} from "./EmojiSelector";
33
34
  export * from "./ErrorBoundary";