|
1 | 1 | // @ts-ignore |
2 | 2 | import { window } from "./tooling/setup"; |
3 | | -import { test } from "bun:test"; |
| 3 | +import { test, expect } from "bun:test"; |
4 | 4 | import { Event } from "happy-dom"; |
| 5 | +import convert from "../src/index"; |
5 | 6 | import { checkEventConversion } from "./tooling/check"; |
6 | 7 | import { mockGamepad, mockTouch, mockTouchObject } from "./tooling/mock"; |
7 | 8 |
|
@@ -405,3 +406,61 @@ test("includes data-* attributes in dataset", () => { |
405 | 406 | }, |
406 | 407 | }); |
407 | 408 | }); |
| 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