Skip to content

Commit 258c86a

Browse files
committed
Add tests for 'on' checkbox properties
1 parent 9039795 commit 258c86a

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/js/packages/event-to-object/src/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,29 @@ function classToObject(x: any, maxDepth: number): object {
156156
}
157157
}
158158

159+
// Explicitly include common input properties if they exist
160+
const extraProps = ["value", "checked", "files", "type"];
161+
for (const prop of extraProps) {
162+
if (
163+
x &&
164+
typeof x === "object" &&
165+
prop in x &&
166+
!Object.prototype.hasOwnProperty.call(result, prop)
167+
) {
168+
const val = x[prop];
169+
if (!shouldIgnoreValue(val, prop, x)) {
170+
if (typeof val === "object") {
171+
const converted = deepCloneClass(val, maxDepth);
172+
if (converted !== maxDepthSignal) {
173+
result[prop] = converted;
174+
}
175+
} else {
176+
result[prop] = val;
177+
}
178+
}
179+
}
180+
}
181+
159182
return result;
160183
}
161184

src/js/packages/event-to-object/tests/event-to-object.test.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// @ts-ignore
22
import { window } from "./tooling/setup";
3-
import { test } from "bun:test";
3+
import { test, expect } from "bun:test";
44
import { Event } from "happy-dom";
5+
import convert from "../src/index";
56
import { checkEventConversion } from "./tooling/check";
67
import { mockGamepad, mockTouch, mockTouchObject } from "./tooling/mock";
78

@@ -405,3 +406,61 @@ test("includes data-* attributes in dataset", () => {
405406
},
406407
});
407408
});
409+
410+
test("includes value and checked for radio and checkbox inputs", () => {
411+
const radio = document.createElement("input");
412+
radio.type = "radio";
413+
radio.checked = true;
414+
415+
const checkbox = document.createElement("input");
416+
checkbox.type = "checkbox";
417+
checkbox.checked = true;
418+
419+
const radioEvent = new window.Event("change");
420+
Object.defineProperty(radioEvent, "target", {
421+
value: radio,
422+
enumerable: true,
423+
writable: true,
424+
});
425+
426+
checkEventConversion(radioEvent, {
427+
target: {
428+
value: "on",
429+
checked: true,
430+
type: "radio",
431+
},
432+
});
433+
434+
const checkboxEvent = new window.Event("change");
435+
Object.defineProperty(checkboxEvent, "target", {
436+
value: checkbox,
437+
enumerable: true,
438+
writable: true,
439+
});
440+
441+
checkEventConversion(checkboxEvent, {
442+
target: {
443+
value: "on",
444+
checked: true,
445+
type: "checkbox",
446+
},
447+
});
448+
});
449+
450+
test("excludes 'on' properties when missing", () => {
451+
const div = document.createElement("div");
452+
div.onclick = () => {};
453+
// @ts-ignore
454+
div.oncustom = null;
455+
456+
const event = new window.Event("click");
457+
Object.defineProperty(event, "target", {
458+
value: div,
459+
enumerable: true,
460+
writable: true,
461+
});
462+
463+
const converted: any = convert(event);
464+
expect(converted.target.onclick).toBeUndefined();
465+
expect(converted.target.oncustom).toBeUndefined();
466+
});

0 commit comments

Comments
 (0)