Skip to content

Commit 617ef91

Browse files
committed
add test for data-* attributes
1 parent 33c5cef commit 617ef91

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ function classToObject(x: any, maxDepth: number): object {
139139
result[key] = x[key];
140140
}
141141
}
142+
143+
// Explicitly include dataset if it exists (it might not be enumerable)
144+
if (
145+
x &&
146+
typeof x === "object" &&
147+
"dataset" in x &&
148+
!Object.prototype.hasOwnProperty.call(result, "dataset")
149+
) {
150+
const dataset = x["dataset"];
151+
if (!shouldIgnoreValue(dataset, "dataset", x)) {
152+
const converted = deepCloneClass(dataset, maxDepth);
153+
if (converted !== maxDepthSignal) {
154+
result["dataset"] = converted;
155+
}
156+
}
157+
}
158+
142159
return result;
143160
}
144161

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,36 @@ test("adds text of current selection", () => {
372372
isTrusted: undefined,
373373
});
374374
});
375+
376+
test("includes data-* attributes in dataset", () => {
377+
const div = document.createElement("div");
378+
div.setAttribute("data-test-value", "123");
379+
div.setAttribute("data-other", "foo");
380+
381+
const event = new window.Event("click");
382+
Object.defineProperty(event, "target", {
383+
value: div,
384+
enumerable: true,
385+
writable: true,
386+
});
387+
Object.defineProperty(event, "currentTarget", {
388+
value: div,
389+
enumerable: true,
390+
writable: true,
391+
});
392+
393+
checkEventConversion(event, {
394+
target: {
395+
dataset: {
396+
testValue: "123",
397+
other: "foo",
398+
},
399+
},
400+
currentTarget: {
401+
dataset: {
402+
testValue: "123",
403+
other: "foo",
404+
},
405+
},
406+
});
407+
});

src/js/packages/event-to-object/tests/tooling/check.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from "bun:test";
22
import { Event } from "happy-dom";
33
// @ts-ignore
44
import lodash from "lodash";
5-
import { convert } from "../../src/index";
5+
import convert from "../../src/index";
66

77
export function checkEventConversion(
88
givenEvent: Event,

0 commit comments

Comments
 (0)