|
1 | | -import { act, useRef, useState } from "react"; |
| 1 | +import { Activity, act, useRef, useState } from "react"; |
2 | 2 | import { createRoot } from "react-dom/client"; |
3 | | -import { describe, expect, test } from "bun:test"; |
| 3 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
4 | 4 |
|
| 5 | +import { |
| 6 | + useAttachments, |
| 7 | + type AttachmentItem, |
| 8 | +} from "../hooks/use-attachments.js"; |
5 | 9 | import { |
6 | 10 | Attachment, |
| 11 | + AttachmentMore, |
7 | 12 | AttachmentPreview, |
8 | 13 | AttachmentRemove, |
9 | 14 | Attachments, |
@@ -114,4 +119,190 @@ describe("Attachments", () => { |
114 | 119 | expect(opened).toEqual(["notes.txt"]); |
115 | 120 | unmount(); |
116 | 121 | }); |
| 122 | + |
| 123 | + test("a preview that opens takes its name from label", () => { |
| 124 | + const { container, unmount } = mount( |
| 125 | + <Attachments> |
| 126 | + <Attachment data={NOTES}> |
| 127 | + <AttachmentPreview label="View the notes" onOpen={() => {}} /> |
| 128 | + </Attachment> |
| 129 | + </Attachments>, |
| 130 | + ); |
| 131 | + expect(button(container, "View the notes").dataset["slot"]).toBe( |
| 132 | + "attachment-preview", |
| 133 | + ); |
| 134 | + unmount(); |
| 135 | + }); |
| 136 | + |
| 137 | + test("an uncontrolled list folds until More expands it", () => { |
| 138 | + const files: readonly AttachmentData[] = ["a.txt", "b.txt", "c.txt"].map( |
| 139 | + (name) => ({ name, mediaType: "text/plain" }), |
| 140 | + ); |
| 141 | + const { container, unmount } = mount( |
| 142 | + <Attachments variant="list"> |
| 143 | + {({ expanded }) => { |
| 144 | + const shown = expanded ? files : files.slice(0, 1); |
| 145 | + return ( |
| 146 | + <> |
| 147 | + {shown.map((data) => ( |
| 148 | + <Attachment key={data.name} data={data}> |
| 149 | + <AttachmentRemove /> |
| 150 | + </Attachment> |
| 151 | + ))} |
| 152 | + {expanded ? null : <AttachmentMore count={files.length - 1} />} |
| 153 | + </> |
| 154 | + ); |
| 155 | + }} |
| 156 | + </Attachments>, |
| 157 | + ); |
| 158 | + const names = () => |
| 159 | + [...container.querySelectorAll("[data-slot=attachment-remove]")].map( |
| 160 | + (element) => element.getAttribute("aria-label"), |
| 161 | + ); |
| 162 | + expect(names()).toEqual(["Remove a.txt"]); |
| 163 | + |
| 164 | + const more = button(container, "Show 2 more"); |
| 165 | + more.focus(); |
| 166 | + act(() => more.click()); |
| 167 | + expect(names()).toEqual(["Remove a.txt", "Remove b.txt", "Remove c.txt"]); |
| 168 | + expect(container.querySelector("ul")?.dataset["expanded"]).toBe("true"); |
| 169 | + // Focus goes to the first attachment that was folded away. |
| 170 | + expect(document.activeElement).toBe(button(container, "Remove b.txt")); |
| 171 | + unmount(); |
| 172 | + }); |
| 173 | + |
| 174 | + test("defaultExpanded starts an uncontrolled list expanded", () => { |
| 175 | + const { container, unmount } = mount( |
| 176 | + <Attachments variant="list" defaultExpanded> |
| 177 | + {({ expanded }) => (expanded ? <li>all</li> : <li>some</li>)} |
| 178 | + </Attachments>, |
| 179 | + ); |
| 180 | + expect(container.textContent).toBe("all"); |
| 181 | + unmount(); |
| 182 | + }); |
| 183 | +}); |
| 184 | + |
| 185 | +describe("a preview held across a hidden Activity", () => { |
| 186 | + let live = new Set<string>(); |
| 187 | + let made = 0; |
| 188 | + const realCreate = URL.createObjectURL; |
| 189 | + const realRevoke = URL.revokeObjectURL; |
| 190 | + |
| 191 | + beforeEach(() => { |
| 192 | + live = new Set(); |
| 193 | + made = 0; |
| 194 | + URL.createObjectURL = () => { |
| 195 | + made += 1; |
| 196 | + const url = `blob:test/${made}`; |
| 197 | + live.add(url); |
| 198 | + return url; |
| 199 | + }; |
| 200 | + URL.revokeObjectURL = (url: string) => { |
| 201 | + live.delete(url); |
| 202 | + }; |
| 203 | + }); |
| 204 | + |
| 205 | + afterEach(() => { |
| 206 | + URL.createObjectURL = realCreate; |
| 207 | + URL.revokeObjectURL = realRevoke; |
| 208 | + }); |
| 209 | + |
| 210 | + function tick(): Promise<void> { |
| 211 | + return act(() => new Promise<void>((resolve) => setTimeout(resolve, 0))); |
| 212 | + } |
| 213 | + |
| 214 | + test("is rebuilt from its file once its URL stops loading", async () => { |
| 215 | + let attach: (files: readonly File[]) => void = () => {}; |
| 216 | + let setHidden: (hidden: boolean) => void = () => {}; |
| 217 | + |
| 218 | + function Composer() { |
| 219 | + const [held, setHeld] = useState<readonly AttachmentItem[]>([]); |
| 220 | + const { add } = useAttachments({ attachments: held }); |
| 221 | + attach = (files) => { |
| 222 | + const { accepted } = add(files); |
| 223 | + setHeld((previous) => [...previous, ...accepted]); |
| 224 | + }; |
| 225 | + return ( |
| 226 | + <Attachments variant="tiles"> |
| 227 | + {held.map((item) => ( |
| 228 | + <Attachment key={item.id} data={item}> |
| 229 | + <AttachmentPreview /> |
| 230 | + </Attachment> |
| 231 | + ))} |
| 232 | + </Attachments> |
| 233 | + ); |
| 234 | + } |
| 235 | + |
| 236 | + function Route() { |
| 237 | + const [hidden, setRouteHidden] = useState(false); |
| 238 | + setHidden = setRouteHidden; |
| 239 | + return ( |
| 240 | + <Activity mode={hidden ? "hidden" : "visible"}> |
| 241 | + <Composer /> |
| 242 | + </Activity> |
| 243 | + ); |
| 244 | + } |
| 245 | + |
| 246 | + const { container, unmount } = mount(<Route />); |
| 247 | + act(() => attach([new File(["x"], "shot.png", { type: "image/png" })])); |
| 248 | + const picture = () => container.querySelector("img"); |
| 249 | + const first = picture()?.getAttribute("src") ?? ""; |
| 250 | + expect(live.has(first)).toBe(true); |
| 251 | + |
| 252 | + // Hidden for longer than a tick: the hook's previews go. |
| 253 | + act(() => setHidden(true)); |
| 254 | + await tick(); |
| 255 | + act(() => setHidden(false)); |
| 256 | + await tick(); |
| 257 | + expect(live.has(first)).toBe(false); |
| 258 | + |
| 259 | + // The browser fails to load the revoked URL. |
| 260 | + act(() => { |
| 261 | + picture()?.dispatchEvent(new Event("error")); |
| 262 | + }); |
| 263 | + const rebuilt = picture()?.getAttribute("src") ?? ""; |
| 264 | + expect(rebuilt).not.toBe(first); |
| 265 | + expect(live.has(rebuilt)).toBe(true); |
| 266 | + |
| 267 | + // The rebuilt URL follows the same rule: gone while hidden, made again |
| 268 | + // on show. |
| 269 | + act(() => setHidden(true)); |
| 270 | + expect(live.has(rebuilt)).toBe(false); |
| 271 | + act(() => setHidden(false)); |
| 272 | + const again = picture()?.getAttribute("src") ?? ""; |
| 273 | + expect(live.has(again)).toBe(true); |
| 274 | + |
| 275 | + unmount(); |
| 276 | + await tick(); |
| 277 | + expect([...live]).toEqual([]); |
| 278 | + }); |
| 279 | + |
| 280 | + test("falls back to the type when the rebuilt URL fails too", () => { |
| 281 | + const shot = new File(["x"], "shot.png", { type: "image/png" }); |
| 282 | + const { container, unmount } = mount( |
| 283 | + <Attachments variant="tiles"> |
| 284 | + <Attachment |
| 285 | + data={{ |
| 286 | + name: "shot.png", |
| 287 | + mediaType: "image/png", |
| 288 | + url: "blob:test/gone", |
| 289 | + file: shot, |
| 290 | + }} |
| 291 | + > |
| 292 | + <AttachmentPreview /> |
| 293 | + </Attachment> |
| 294 | + </Attachments>, |
| 295 | + ); |
| 296 | + const fail = () => |
| 297 | + act(() => { |
| 298 | + container.querySelector("img")?.dispatchEvent(new Event("error")); |
| 299 | + }); |
| 300 | + fail(); |
| 301 | + expect(container.querySelector("img")).not.toBeNull(); |
| 302 | + fail(); |
| 303 | + expect(container.querySelector("img")).toBeNull(); |
| 304 | + expect(container.textContent).toContain("PNG"); |
| 305 | + unmount(); |
| 306 | + expect([...live]).toEqual([]); |
| 307 | + }); |
117 | 308 | }); |
0 commit comments