|
1 | | -import { createRoot, createSignal, flush, createMemo } from "solid-js"; |
| 1 | +import { createRoot, createSignal, flush, createMemo, Loading } from "solid-js"; |
2 | 2 | import htmlTag from "../src/tagged-jsx.js"; |
3 | | -import { expect, it, describe, beforeEach } from "vitest"; |
| 3 | +import { expect, it, describe, beforeEach, vi } from "vitest"; |
4 | 4 | import { insert, render, registerElementClaim } from "@solidjs/web"; |
5 | 5 |
|
6 | 6 | const For = (props: any) => { |
@@ -429,6 +429,90 @@ describe("Tagged JSX Integration Tests", () => { |
429 | 429 | expect(container.querySelector("li")?.textContent).toBe("Item 1"); |
430 | 430 | dispose(); |
431 | 431 | })); |
| 432 | + |
| 433 | + it("wraps zero-arg functions in getters for non-handler `on*` props (#3728)", () => { |
| 434 | + const [value, setValue] = createSignal(1); |
| 435 | + const handler = () => "handled"; |
| 436 | + const ref = () => {}; |
| 437 | + let props!: any; |
| 438 | + const Probe = (p: any) => { |
| 439 | + props = p; |
| 440 | + return null; |
| 441 | + }; |
| 442 | + const dispose = createRoot(d => { |
| 443 | + html`<${Probe} |
| 444 | + on=${() => value()} |
| 445 | + only=${() => value() * 2} |
| 446 | + once=${() => value() * 3} |
| 447 | + onClick=${handler} |
| 448 | + ref=${ref} |
| 449 | + />`; |
| 450 | + return d; |
| 451 | + }); |
| 452 | + |
| 453 | + expect(props.on).toBe(1); |
| 454 | + expect(props.only).toBe(2); |
| 455 | + expect(props.once).toBe(3); |
| 456 | + expect(props.onClick).toBe(handler); |
| 457 | + expect(props.ref).toBe(ref); |
| 458 | + |
| 459 | + setValue(2); |
| 460 | + flush(); |
| 461 | + expect(props.on).toBe(2); |
| 462 | + expect(props.only).toBe(4); |
| 463 | + expect(props.once).toBe(6); |
| 464 | + dispose(); |
| 465 | + }); |
| 466 | + |
| 467 | + it("keeps zero-arg `onXxx` handlers on native elements as events", () => { |
| 468 | + let clicks = 0; |
| 469 | + const container = document.createElement("div"); |
| 470 | + document.body.append(container); |
| 471 | + const dispose = render( |
| 472 | + () => |
| 473 | + html`<div> |
| 474 | + <button onClick=${() => clicks++}>a</button> |
| 475 | + <button onclick="void 0">b</button> |
| 476 | + </div>`, |
| 477 | + container |
| 478 | + ); |
| 479 | + const [a, b] = container.querySelectorAll("button"); |
| 480 | + expect(clicks).toBe(0); |
| 481 | + a.dispatchEvent(new MouseEvent("click", { bubbles: true })); |
| 482 | + expect(clicks).toBe(1); |
| 483 | + expect(b.getAttribute("onclick")).toBe("void 0"); |
| 484 | + dispose(); |
| 485 | + }); |
| 486 | + |
| 487 | + it("re-keys <Loading> from a reactive `on` getter (#3728)", async () => { |
| 488 | + vi.useFakeTimers(); |
| 489 | + try { |
| 490 | + const delay = <T>(ms: number, value: T) => new Promise<T>(r => setTimeout(r, ms, value)); |
| 491 | + const [key, setKey] = createSignal(0); |
| 492 | + const container = document.createElement("div"); |
| 493 | + const dispose = render(() => { |
| 494 | + const data = createMemo(() => delay(100, key())); |
| 495 | + return html.define({ Loading })`<${Loading} on=${() => key()} fallback="loading"> |
| 496 | + <span>${() => data()}</span> |
| 497 | + <//>`; |
| 498 | + }, container); |
| 499 | + flush(); |
| 500 | + expect(container.textContent!.trim()).toBe("loading"); |
| 501 | + await vi.advanceTimersByTimeAsync(100); |
| 502 | + flush(); |
| 503 | + expect(container.textContent!.trim()).toBe("0"); |
| 504 | + |
| 505 | + setKey(1); |
| 506 | + flush(); |
| 507 | + expect(container.textContent!.trim()).toBe("loading"); |
| 508 | + await vi.advanceTimersByTimeAsync(100); |
| 509 | + flush(); |
| 510 | + expect(container.textContent!.trim()).toBe("1"); |
| 511 | + dispose(); |
| 512 | + } finally { |
| 513 | + vi.useRealTimers(); |
| 514 | + } |
| 515 | + }); |
432 | 516 | }); |
433 | 517 |
|
434 | 518 | describe("Special Elements and Namespaces", () => { |
|
0 commit comments