| @@ -3,7 +3,7 @@ |
| 3 | 3 | // in-flight guard. fetch is a fake per test; the tally arithmetic is also |
| 4 | 4 | // proven on its own. |
| 5 | 5 | import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
| 6 | | −import { fromAnswer, press } from "../packages/vote/index"; |
| 6 | +import { bodyOf, fromAnswer, press } from "../packages/vote/index"; |
| 7 | 7 | import styles from "../packages/vote/vote.css"; |
| 8 | 8 | |
| 9 | 9 | const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms)); |
| @@ -93,6 +93,23 @@ describe("the tally", () => { |
| 93 | 93 | }); |
| 94 | 94 | |
| 95 | 95 | describe("pebble-vote", () => { |
| 96 | + test("coal sends -1, URL-encoded, as the form itself would", async () => { |
| 97 | + const { down } = mount(); |
| 98 | + submit(down); |
| 99 | + await sleep(5); |
| 100 | + const body = calls[0]!.init.body as URLSearchParams; |
| 101 | + expect(body).toBeInstanceOf(URLSearchParams); |
| 102 | + expect(body.get("valeur")).toBe("-1"); |
| 103 | + expect(body.toString()).toContain("valeur=-1"); |
| 104 | + }); |
| 105 | + |
| 106 | + test("a form that asks for multipart sends multipart", () => { |
| 107 | + const form = document.createElement("form"); |
| 108 | + form.setAttribute("enctype", "multipart/form-data"); |
| 109 | + form.innerHTML = '<input name="valeur" value="-1">'; |
| 110 | + expect(bodyOf(form)).toBeInstanceOf(FormData); |
| 111 | + }); |
| 112 | + |
| 96 | 113 | test("marks each side; the server's labels and counts stand", () => { |
| 97 | 114 | const { host, up, down } = mount(); |
| 98 | 115 | expect(up.dataset.side).toBe("up"); |
| @@ -117,7 +134,9 @@ describe("pebble-vote", () => { |
| 117 | 134 | expect(calls[0]!.url).toEndWith("/suggestions/42/vote"); |
| 118 | 135 | expect(calls[0]!.init.method).toBe("POST"); |
| 119 | 136 | expect((calls[0]!.init.headers as Record<string, string>).Accept).toBe("application/json"); |
| 120 | | − const body = calls[0]!.init.body as FormData; |
| 137 | + // URL-encoded, as the form itself would post it (not multipart). |
| 138 | + const body = calls[0]!.init.body as URLSearchParams; |
| 139 | + expect(body).toBeInstanceOf(URLSearchParams); |
| 121 | 140 | expect(body.get("valeur")).toBe("1"); |
| 122 | 141 | expect(body.get("csrf")).toBe("tok"); |
| 123 | 142 | |