release: WRNexusJS 0.5.0
This commit is contained in:
@@ -212,3 +212,96 @@ test("parseEnv defaults to reading the ambient environment", () => {
|
||||
expect(env.WIRE_TEST_ENV_VAR).toBe("present");
|
||||
delete process.env.WIRE_TEST_ENV_VAR;
|
||||
});
|
||||
|
||||
test("object schemas can be extended without mutating package defaults", () => {
|
||||
const base = v.object({ identifier: v.string().required("Enter an identifier") });
|
||||
const extended = base.extend({
|
||||
identifier: v.string().email("Use an email address"),
|
||||
remember: v.boolean().optional(),
|
||||
});
|
||||
|
||||
expect(base.parse({ identifier: "username" }).ok).toBe(true);
|
||||
expect(extended.parse({ identifier: "username" }).errors.identifier).toBe("Use an email address");
|
||||
expect(extended.parse({ identifier: "person@example.com" }).ok).toBe(true);
|
||||
});
|
||||
|
||||
test("unknown schemas preserve structured request payloads", () => {
|
||||
const schema = v.object({ response: v.unknown().required("Response is required") });
|
||||
const payload = { id: "credential", nested: { ok: true } };
|
||||
const result = schema.parse({ response: payload });
|
||||
expect(result.ok).toBe(true);
|
||||
expect(result.value.response).toEqual(payload);
|
||||
expect(schema.parse({}).errors.response).toBe("Response is required");
|
||||
});
|
||||
|
||||
test("validation can bind after a package registers a missing schema", () => {
|
||||
const win = new Window() as unknown as Window & Record<string, unknown>;
|
||||
win.document.body.innerHTML = `
|
||||
<form data-schema="late-schema">
|
||||
<input name="identifier">
|
||||
<span data-error="identifier"></span>
|
||||
</form>`;
|
||||
win.__wireSchemas = {};
|
||||
(globalThis as Record<string, unknown>).window = win;
|
||||
(globalThis as Record<string, unknown>).document = win.document;
|
||||
|
||||
try {
|
||||
(0, eval)(VALIDATE_RUNTIME);
|
||||
const runtime = win.__wireValidate as { init(root: Document): void };
|
||||
runtime.init(win.document as unknown as Document);
|
||||
win.__wireSchemas = {
|
||||
"late-schema": v
|
||||
.object({ identifier: v.string().required("Enter an identifier") })
|
||||
.describe(),
|
||||
};
|
||||
runtime.init(win.document as unknown as Document);
|
||||
win.document
|
||||
.querySelector("form")!
|
||||
.dispatchEvent(new win.Event("submit", { bubbles: true, cancelable: true }));
|
||||
expect(win.document.querySelector("[data-error=identifier]")!.textContent).toBe(
|
||||
"Enter an identifier",
|
||||
);
|
||||
} finally {
|
||||
delete (globalThis as Record<string, unknown>).window;
|
||||
delete (globalThis as Record<string, unknown>).document;
|
||||
}
|
||||
});
|
||||
|
||||
test("schema-backed forms validate on input, change, and blur and synchronize field state", () => {
|
||||
const win = new Window() as unknown as Window & Record<string, unknown>;
|
||||
win.document.body.innerHTML = `
|
||||
<form data-schema="profile">
|
||||
<div class="wire-next--field" data-invalid="false">
|
||||
<input name="email">
|
||||
<span data-error="email"></span>
|
||||
</div>
|
||||
</form>`;
|
||||
win.__wireSchemas = {
|
||||
profile: v
|
||||
.object({ email: v.string().required("Enter an email").email("Use a valid email") })
|
||||
.describe(),
|
||||
};
|
||||
(globalThis as Record<string, unknown>).window = win;
|
||||
(globalThis as Record<string, unknown>).document = win.document;
|
||||
|
||||
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;
|
||||
|
||||
input.dispatchEvent(new win.Event("input", { bubbles: true }) as unknown as Event);
|
||||
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);
|
||||
expect(error.textContent).toBe("");
|
||||
expect(input.hasAttribute("aria-invalid")).toBe(false);
|
||||
expect(field.dataset.invalid).toBe("false");
|
||||
} finally {
|
||||
delete (globalThis as Record<string, unknown>).window;
|
||||
delete (globalThis as Record<string, unknown>).document;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user