Synthetic events drop non-enumerable fields (clipboardData/dataTransfer from Testing Library are undefined) #98
Version: @tanstack/redact 0.2.2
createSyntheticEvent (src/dom/synthetic-event.ts) copies the native event's fields with for (const key in native). That only sees enumerable properties. Own properties defined with Object.defineProperty (non-enumerable by default) are dropped, so the handler gets undefined where React hands over the value.
In real browsers this is fine for native events: WebIDL attributes such as ClipboardEvent.clipboardData and DragEvent.dataTransfer are enumerable accessors on the prototype (checked in Chromium). It breaks for:
- Testing Library / jsdom:
fireEvent.paste(el, { clipboardData })andfireEvent.drop(el, { dataTransfer })assign those fields withObject.defineProperty, because jsdom has noClipboardEvent/DragEventdata. Paste and drop tests that pass under React fail under Redact. - Any library or app code that dispatches a plain
Eventand attaches data the same way.
Repro (Vitest + jsdom + Testing Library)#
import { render, fireEvent } from "@testing-library/react";
it("handlers see non-enumerable event fields", () => {
let pasted: unknown;
const { getByRole } = render(
<textarea onPaste={(e) => { pasted = e.clipboardData?.getData("text/plain"); }} />,
);
fireEvent.paste(getByRole("textbox"), { clipboardData: { getData: () => "hello" } });
let dropped: unknown;
const { container } = render(<div onDrop={(e) => { dropped = e.dataTransfer?.getData("text"); }} />);
const ev = new Event("drop", { bubbles: true });
Object.defineProperty(ev, "dataTransfer", { value: { getData: () => "dropped" } });
container.firstChild!.dispatchEvent(ev);
expect(pasted).toBe("hello");
expect(dropped).toBe("dropped");
});
pasted | dropped | |
|---|---|---|
| React 19 | "hello" | "dropped" |
| Redact 0.2.2 | undefined | undefined |
Suggestion#
Also copy own non-enumerable properties (Object.getOwnPropertyNames(native)), or read fields lazily from nativeEvent (a getter or Proxy fallback) the way React's per-interface getters read nativeEvent.clipboardData / dataTransfer directly.
Found while running a TanStack Start app's component tests (chat composer paste and image drop) under Redact.
Sign in to join this conversation.