Files
uptimesebi/internal/web/testdata/forms.html

77 lines
4.1 KiB
HTML

<!doctype html><html><body data-msg-network="net">
<form data-api="/org/monitors" data-next="#saved" id="f">
<select name="type" data-type-select><option value="http">http</option><option value="json" selected>json</option><option value="heartbeat">heartbeat</option></select>
<input name="name" value="Queue">
<div data-hide-for="heartbeat"><input name="target" value="https://x" required></div>
<fieldset data-type="json">
<select name="config.method"><option value="POST" selected>POST</option></select>
<textarea name="config.headers" data-headers>Authorization: Bearer a:b
X-Empty:
: nothing</textarea>
<input name="config.assertions.0.path" value="status"><input name="config.assertions.0.op" value="==">
<input name="config.assertions.1.path" value="queue.depth"><input name="config.assertions.1.op" value="<">
</fieldset>
<fieldset data-type="http" disabled hidden><select name="config.method"><option value="HEAD" selected>HEAD</option></select></fieldset>
<fieldset data-hide-for="heartbeat"><input type="checkbox" name="regions" value="r1" data-multi checked></fieldset>
<input type="hidden" name="alert_group_ids" data-multi>
<input type="checkbox" name="alert_group_ids" value="default" data-multi checked>
<input type="checkbox" name="alert_group_ids" value="ops" data-multi>
<input type="hidden" name="channel_ids" data-multi>
<button type="submit">save</button>
</form>
<form data-api="/org/agents/a1" data-method="patch" data-autosave id="auto">
<input type="checkbox" name="allow_private" checked>
</form>
<pre id="out"></pre>
<script>
const sent = [];
window.axios = { create: () => ({ interceptors: { request: { use() {} } }, request: async (cfg) => { sent.push(cfg.data); return { data: {} }; }, post: async () => ({ data: {} }) }) };
</script>
<script src="../assets/app.js"></script>
<script>
(async () => {
const out = [];
const check = (name, got, want) => {
const g = JSON.stringify(got), w = JSON.stringify(want);
out.push((g === w ? "PASS " : "FAIL ") + name + (g === w ? "" : " got=" + g + " want=" + w));
};
const form = document.getElementById("f");
const submit = async () => {
form.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));
await new Promise((r) => setTimeout(r, 20));
return sent.at(-1);
};
let body = await submit();
check("fields of a disabled fieldset are skipped", body.config.method, "POST");
check("numbered names build arrays", body.config.assertions, [{ path: "status", op: "==" }, { path: "queue.depth", op: "<" }]);
check("header lines become an object", body.config.headers, { Authorization: "Bearer a:b", "X-Empty": "" });
check("regions sent", body.regions, ["r1"]);
check("default alert group selected", body.alert_group_ids, ["default"]);
check("empty channel list stays an array", body.channel_ids, []);
form.querySelector('[name="alert_group_ids"][value="default"]').checked = false;
body = await submit();
check("cleared alert groups sent explicitly", body.alert_group_ids, []);
form.querySelector('[name="alert_group_ids"][value="ops"]').checked = true;
body = await submit();
check("custom alert group selected", body.alert_group_ids, ["ops"]);
const sel = form.querySelector("[data-type-select]");
sel.value = "heartbeat";
sel.dispatchEvent(new Event("change", { bubbles: true }));
check("target hidden for heartbeats", form.querySelector('[data-hide-for]').hidden, true);
check("target not required for heartbeats", form.querySelector('[name="target"]').required, false);
body = await submit();
check("regions of a hidden fieldset are not sent", body.regions, []);
sel.value = "json";
sel.dispatchEvent(new Event("change", { bubbles: true }));
check("target required again", form.querySelector('[name="target"]').required, true);
const box = document.querySelector("#auto input");
box.checked = false;
box.dispatchEvent(new Event("change", { bubbles: true }));
await new Promise((r) => setTimeout(r, 20));
check("autosave forms are sent on change", sent.at(-1), { allow_private: false });
document.getElementById("out").textContent = out.join("\n");
})();
</script></body></html>