release: WRNexusJS 0.7.0
This commit is contained in:
@@ -1,7 +1,21 @@
|
||||
import { test, expect } from "bun:test";
|
||||
import { Window } from "happy-dom";
|
||||
import {
|
||||
Window,
|
||||
type Event as HappyDOMEvent,
|
||||
type HTMLFormElement as HappyDOMHTMLFormElement,
|
||||
type HTMLInputElement as HappyDOMHTMLInputElement,
|
||||
type HTMLElement as HappyDOMHTMLElement,
|
||||
type IEventInit,
|
||||
} from "happy-dom";
|
||||
import { v, invalid, parseBody, checkField, parseEnv, VALIDATE_RUNTIME } from "../src/index.ts";
|
||||
|
||||
type HappyDOMEventConstructor = new (type: string, init?: IEventInit) => HappyDOMEvent;
|
||||
|
||||
function windowEvent(win: Window, type: string, init?: IEventInit): HappyDOMEvent {
|
||||
const EventConstructor = (win as unknown as { Event: HappyDOMEventConstructor }).Event;
|
||||
return new EventConstructor(type, init);
|
||||
}
|
||||
|
||||
const login = v.object({
|
||||
email: v.string().email(),
|
||||
password: v.string().min(8, "too short"),
|
||||
@@ -69,7 +83,7 @@ test("browser validation displays the serialized custom required message", () =>
|
||||
const runtime = win.__wireValidate as { init(root: Document): void };
|
||||
runtime.init(win.document as unknown as Document);
|
||||
const form = win.document.querySelector("form")!;
|
||||
win.document.querySelector("input")!.dispatchEvent(new win.Event("blur", { bubbles: true }));
|
||||
win.document.querySelector("input")!.dispatchEvent(windowEvent(win, "blur", { bubbles: true }));
|
||||
|
||||
expect(form.noValidate).toBe(true);
|
||||
expect(form.hasAttribute("novalidate")).toBe(true);
|
||||
@@ -257,7 +271,7 @@ test("validation can bind after a package registers a missing schema", () => {
|
||||
runtime.init(win.document as unknown as Document);
|
||||
win.document
|
||||
.querySelector("form")!
|
||||
.dispatchEvent(new win.Event("submit", { bubbles: true, cancelable: true }));
|
||||
.dispatchEvent(windowEvent(win, "submit", { bubbles: true, cancelable: true }));
|
||||
expect(win.document.querySelector("[data-error=identifier]")!.textContent).toBe(
|
||||
"Enter an identifier",
|
||||
);
|
||||
@@ -286,17 +300,17 @@ test("schema-backed forms validate on input, change, and blur and synchronize fi
|
||||
|
||||
try {
|
||||
(0, eval)(VALIDATE_RUNTIME);
|
||||
const input = win.document.querySelector("input") as unknown as HTMLInputElement;
|
||||
const field = win.document.querySelector(".wire-next--field") as unknown as HTMLElement;
|
||||
const error = win.document.querySelector("[data-error=email]") as unknown as HTMLElement;
|
||||
const input = win.document.querySelector("input") as HappyDOMHTMLInputElement;
|
||||
const field = win.document.querySelector(".wire-next--field") as HappyDOMHTMLElement;
|
||||
const error = win.document.querySelector("[data-error=email]") as HappyDOMHTMLElement;
|
||||
|
||||
input.dispatchEvent(new win.Event("input", { bubbles: true }) as unknown as Event);
|
||||
input.dispatchEvent(windowEvent(win, "input", { bubbles: true }));
|
||||
expect(error.textContent).toBe("Enter an email");
|
||||
expect(input.getAttribute("aria-invalid")).toBe("true");
|
||||
expect(field.dataset.invalid).toBe("true");
|
||||
|
||||
input.value = "person@example.com";
|
||||
input.dispatchEvent(new win.Event("change", { bubbles: true }) as unknown as Event);
|
||||
input.dispatchEvent(windowEvent(win, "change", { bubbles: true }));
|
||||
expect(error.textContent).toBe("");
|
||||
expect(input.hasAttribute("aria-invalid")).toBe(false);
|
||||
expect(field.dataset.invalid).toBe("false");
|
||||
@@ -321,7 +335,7 @@ test("submit guards run only after schema fields are valid", () => {
|
||||
|
||||
try {
|
||||
(0, eval)(VALIDATE_RUNTIME);
|
||||
const form = win.document.querySelector("form")! as unknown as HTMLFormElement & {
|
||||
const form = win.document.querySelector("form")! as HappyDOMHTMLFormElement & {
|
||||
__wireSubmitGuards?: Array<() => boolean>;
|
||||
};
|
||||
let guardCalls = 0;
|
||||
@@ -332,16 +346,12 @@ test("submit guards run only after schema fields are valid", () => {
|
||||
},
|
||||
];
|
||||
|
||||
form.dispatchEvent(
|
||||
new win.Event("submit", { bubbles: true, cancelable: true }) as unknown as Event,
|
||||
);
|
||||
form.dispatchEvent(windowEvent(win, "submit", { bubbles: true, cancelable: true }));
|
||||
expect(guardCalls).toBe(0);
|
||||
expect(win.document.querySelector("[data-error=email]")!.textContent).toBe("Enter an email");
|
||||
|
||||
(form.elements.namedItem("email") as HTMLInputElement).value = "person@example.com";
|
||||
form.dispatchEvent(
|
||||
new win.Event("submit", { bubbles: true, cancelable: true }) as unknown as Event,
|
||||
);
|
||||
(form.elements.namedItem("email") as HappyDOMHTMLInputElement).value = "person@example.com";
|
||||
form.dispatchEvent(windowEvent(win, "submit", { bubbles: true, cancelable: true }));
|
||||
expect(guardCalls).toBe(1);
|
||||
} finally {
|
||||
delete (globalThis as Record<string, unknown>).window;
|
||||
|
||||
Reference in New Issue
Block a user