Button partials with any methods
This commit is contained in:
@@ -2150,6 +2150,288 @@ Deno.test({
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "partials - submit form with formmethod HEAD is sent as GET",
|
||||
fn: async () => {
|
||||
const app = testApp()
|
||||
.get("/partial", (ctx) => {
|
||||
const name = ctx.url.searchParams.get("name");
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
<Partial name="foo">
|
||||
<p class={`done-${ctx.req.method}-${name}`}>done</p>
|
||||
</Partial>
|
||||
</Doc>,
|
||||
);
|
||||
})
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
<div f-client-nav>
|
||||
<form action="/partial">
|
||||
<input name="name" value="foo" />
|
||||
<Partial name="foo">
|
||||
<p class="init">init</p>
|
||||
</Partial>
|
||||
<button type="submit" class="update" formmethod="head">
|
||||
update
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</Doc>,
|
||||
);
|
||||
});
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(address, { waitUntil: "load" });
|
||||
await page.locator(".init").wait();
|
||||
|
||||
await page.locator(".update").click();
|
||||
await page.locator(".done-GET-foo").wait();
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "partials - button f-method outside form",
|
||||
fn: async () => {
|
||||
const app = testApp()
|
||||
.delete("/partial", renderMethodPartial)
|
||||
.patch("/partial", renderMethodPartial)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
<div f-client-nav>
|
||||
<SelfCounter />
|
||||
<button
|
||||
type="button"
|
||||
class="delete"
|
||||
f-partial="/partial"
|
||||
f-method="DELETE"
|
||||
name="name"
|
||||
value="foo"
|
||||
>
|
||||
delete
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="patch"
|
||||
f-partial="/partial"
|
||||
f-method="patch"
|
||||
name="name"
|
||||
value="bar"
|
||||
>
|
||||
patch
|
||||
</button>
|
||||
<Partial name="foo">
|
||||
<p class="init">init</p>
|
||||
</Partial>
|
||||
</div>
|
||||
</Doc>,
|
||||
);
|
||||
});
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(address, { waitUntil: "load" });
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
await page.locator(".increment").click();
|
||||
await waitForText(page, ".output", "1");
|
||||
|
||||
await page.locator(".delete").click();
|
||||
await page.locator(".done-DELETE-foo").wait();
|
||||
|
||||
await page.locator(".patch").click();
|
||||
await page.locator(".done-PATCH-bar").wait();
|
||||
|
||||
// Island state survives and the URL is left alone
|
||||
await waitForText(page, ".output", "1");
|
||||
const pathname = await page.evaluate(() => window.location.pathname);
|
||||
expect(pathname).toEqual("/");
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "partials - button f-method inside form sends form fields",
|
||||
fn: async () => {
|
||||
const app = testApp()
|
||||
.put("/partial", async (ctx) => {
|
||||
const data = await ctx.req.formData();
|
||||
const name = data.get("name");
|
||||
const extra = data.get("extra");
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
<Partial name="foo">
|
||||
<p class={`done-${ctx.req.method}-${name}-${extra}`}>done</p>
|
||||
</Partial>
|
||||
</Doc>,
|
||||
);
|
||||
})
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
<div f-client-nav>
|
||||
<form>
|
||||
<input name="name" value="foo" />
|
||||
<Partial name="foo">
|
||||
<p class="init">init</p>
|
||||
</Partial>
|
||||
<button
|
||||
type="button"
|
||||
class="update"
|
||||
f-partial="/partial"
|
||||
f-method="PUT"
|
||||
name="extra"
|
||||
value="bar"
|
||||
>
|
||||
update
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</Doc>,
|
||||
);
|
||||
});
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(address, { waitUntil: "load" });
|
||||
await page.locator(".init").wait();
|
||||
|
||||
await page.locator(".update").click();
|
||||
await page.locator(".done-PUT-foo-bar").wait();
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "partials - button f-method cancelled with preventDefault",
|
||||
fn: async () => {
|
||||
let deleteCalls = 0;
|
||||
const app = testApp()
|
||||
.delete("/partial", (ctx) => {
|
||||
deleteCalls++;
|
||||
return renderMethodPartial(ctx);
|
||||
})
|
||||
.get("/other", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
<Partial name="foo">
|
||||
<p class="other">other</p>
|
||||
</Partial>
|
||||
</Doc>,
|
||||
);
|
||||
})
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
<div f-client-nav>
|
||||
<button
|
||||
type="button"
|
||||
class="delete"
|
||||
f-partial="/partial"
|
||||
f-method="DELETE"
|
||||
name="name"
|
||||
value="foo"
|
||||
>
|
||||
delete
|
||||
</button>
|
||||
<button type="button" class="other-btn" f-partial="/other">
|
||||
other
|
||||
</button>
|
||||
<Partial name="foo">
|
||||
<p class="init">init</p>
|
||||
</Partial>
|
||||
</div>
|
||||
</Doc>,
|
||||
);
|
||||
});
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(address, { waitUntil: "load" });
|
||||
await page.locator(".init").wait();
|
||||
|
||||
await page.evaluate(() => {
|
||||
document.querySelector(".delete")!.addEventListener(
|
||||
"click",
|
||||
(e) => e.preventDefault(),
|
||||
);
|
||||
});
|
||||
|
||||
await page.locator(".delete").click();
|
||||
|
||||
// Trigger a second partial request after the cancelled one so we know
|
||||
// the click handler has run before checking the DELETE handler.
|
||||
await page.locator(".other-btn").click();
|
||||
await page.locator(".other").wait();
|
||||
|
||||
expect(deleteCalls).toEqual(0);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "partials - button f-method indicator",
|
||||
fn: async () => {
|
||||
const app = testApp()
|
||||
.delete("/partial", renderMethodPartial)
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
<div f-client-nav>
|
||||
<button
|
||||
type="button"
|
||||
class="delete"
|
||||
f-partial="/partial"
|
||||
f-method="DELETE"
|
||||
name="name"
|
||||
value="foo"
|
||||
>
|
||||
delete
|
||||
</button>
|
||||
<Partial name="foo">
|
||||
<p class="init">init</p>
|
||||
</Partial>
|
||||
</div>
|
||||
</Doc>,
|
||||
);
|
||||
});
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(address, { waitUntil: "load" });
|
||||
await page.locator(".init").wait();
|
||||
|
||||
await page.evaluate(() => {
|
||||
const btn = document.querySelector(".delete")!;
|
||||
const indicator = { _wasTrue: false, _value: false };
|
||||
Object.defineProperty(indicator, "value", {
|
||||
get() {
|
||||
return this._value;
|
||||
},
|
||||
set(v: boolean) {
|
||||
this._value = v;
|
||||
if (v) this._wasTrue = true;
|
||||
},
|
||||
});
|
||||
// deno-lint-ignore no-explicit-any
|
||||
(btn as any)._freshIndicator = indicator;
|
||||
// deno-lint-ignore no-explicit-any
|
||||
(window as any).__indicator = indicator;
|
||||
});
|
||||
|
||||
await page.locator(".delete").click();
|
||||
await page.locator(".done-DELETE-foo").wait();
|
||||
|
||||
const result = await page.evaluate(() => {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const ind = (window as any).__indicator;
|
||||
return { wasTrue: ind._wasTrue, currentValue: ind.value };
|
||||
});
|
||||
expect(result.wasTrue).toBe(true);
|
||||
expect(result.currentValue).toBe(false);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "partials - submit form via external submitter",
|
||||
fn: async () => {
|
||||
|
||||
Reference in New Issue
Block a user