| @@ -0,0 +1,237 @@ |
| 1 | +// <pebble-vote>: the optimistic press, reconciliation with the server's tally, |
| 2 | +// rollback with a toast, the captcha fallback to a native submission, and the |
| 3 | +// in-flight guard. fetch is a fake per test; the tally arithmetic is also |
| 4 | +// proven on its own. |
| 5 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
| 6 | +import { fromAnswer, press } from "../packages/vote/index"; |
| 7 | +import styles from "../packages/vote/vote.css"; |
| 8 | + |
| 9 | +const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms)); |
| 10 | + |
| 11 | +const MARKUP = `<pebble-vote error="Le vote n’a pas passé."> |
| 12 | + <form method="post" action="/suggestions/42/vote"> |
| 13 | + <input type="hidden" name="valeur" value="1"><input type="hidden" name="csrf" value="tok"> |
| 14 | + <button aria-pressed="false" data-count="12" data-label="Cadeau" aria-label="Cadeau (12)">🎁 <span data-count-label>12</span></button> |
| 15 | + </form> |
| 16 | + <form method="post" action="/suggestions/42/vote"> |
| 17 | + <input type="hidden" name="valeur" value="-1"><input type="hidden" name="csrf" value="tok"> |
| 18 | + <button aria-pressed="false" data-count="3" data-label="Charbon" aria-label="Charbon (3)">⚫ <span data-count-label>3</span></button> |
| 19 | + </form> |
| 20 | +</pebble-vote>`; |
| 21 | + |
| 22 | +interface Call { |
| 23 | + url: string; |
| 24 | + init: RequestInit; |
| 25 | +} |
| 26 | +let calls: Call[] = []; |
| 27 | +let answer: () => Promise<Response>; |
| 28 | +let release: (() => void) | null = null; |
| 29 | +const realFetch = globalThis.fetch; |
| 30 | +const toasts: unknown[] = []; |
| 31 | +const onToast = (event: Event) => toasts.push((event as CustomEvent).detail); |
| 32 | + |
| 33 | +const json = (body: unknown, status = 200) => |
| 34 | + new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } }); |
| 35 | + |
| 36 | +beforeEach(() => { |
| 37 | + calls = []; |
| 38 | + toasts.length = 0; |
| 39 | + answer = async () => json({ up: 13, down: 3, mine: 1 }); |
| 40 | + globalThis.fetch = (async (url: string | URL, init: RequestInit = {}) => { |
| 41 | + calls.push({ url: String(url), init }); |
| 42 | + return answer(); |
| 43 | + }) as typeof fetch; |
| 44 | + window.addEventListener("toast", onToast); |
| 45 | +}); |
| 46 | + |
| 47 | +afterEach(async () => { |
| 48 | + globalThis.fetch = realFetch; |
| 49 | + window.removeEventListener("toast", onToast); |
| 50 | + document.body.textContent = ""; |
| 51 | + await sleep(0); // define() releases one macrotask later |
| 52 | +}); |
| 53 | + |
| 54 | +// Built detached, then attached: the element reads its children when it connects |
| 55 | +// (a browser's innerHTML and a deferred module script both guarantee that). |
| 56 | +const mount = (html = MARKUP) => { |
| 57 | + const wrap = document.createElement("div"); |
| 58 | + wrap.innerHTML = html; |
| 59 | + document.body.appendChild(wrap); |
| 60 | + const host = wrap.querySelector("pebble-vote") as HTMLElement; |
| 61 | + const [up, down] = [...host.querySelectorAll("button")] as HTMLButtonElement[]; |
| 62 | + return { host, up: up!, down: down! }; |
| 63 | +}; |
| 64 | + |
| 65 | +const state = (b: HTMLButtonElement) => ({ |
| 66 | + count: b.dataset.count, |
| 67 | + pressed: b.getAttribute("aria-pressed"), |
| 68 | + label: b.getAttribute("aria-label"), |
| 69 | + text: b.querySelector("[data-count-label]")!.textContent, |
| 70 | +}); |
| 71 | + |
| 72 | +const submit = (button: HTMLButtonElement) => { |
| 73 | + const form = button.form!; |
| 74 | + const event = new Event("submit", { bubbles: true, cancelable: true }); |
| 75 | + form.dispatchEvent(event); |
| 76 | + return event; |
| 77 | +}; |
| 78 | + |
| 79 | +describe("the tally", () => { |
| 80 | + test("pressing a side casts, pressing it again takes it back, the other side moves it", () => { |
| 81 | + expect(press({ up: 12, down: 3, mine: 0 }, 1)).toEqual({ up: 13, down: 3, mine: 1 }); |
| 82 | + expect(press({ up: 13, down: 3, mine: 1 }, 1)).toEqual({ up: 12, down: 3, mine: 0 }); |
| 83 | + expect(press({ up: 13, down: 3, mine: 1 }, -1)).toEqual({ up: 12, down: 4, mine: -1 }); |
| 84 | + expect(press({ up: 0, down: 0, mine: 1 }, 1)).toEqual({ up: 0, down: 0, mine: 0 }); |
| 85 | + }); |
| 86 | + |
| 87 | + test("only a well-formed answer is a tally", () => { |
| 88 | + expect(fromAnswer({ up: 1, down: 2, mine: -1 })).toEqual({ up: 1, down: 2, mine: -1 }); |
| 89 | + expect(fromAnswer({ up: 1, down: 2, mine: 2 })).toBeNull(); |
| 90 | + expect(fromAnswer({ up: "1", down: 2, mine: 0 })).toBeNull(); |
| 91 | + expect(fromAnswer(null)).toBeNull(); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe("pebble-vote", () => { |
| 96 | + test("marks each side; the server's labels and counts stand", () => { |
| 97 | + const { host, up, down } = mount(); |
| 98 | + expect(up.dataset.side).toBe("up"); |
| 99 | + expect(down.dataset.side).toBe("down"); |
| 100 | + expect(host.dataset.mine).toBe("0"); |
| 101 | + expect(state(up)).toEqual({ count: "12", pressed: "false", label: "Cadeau (12)", text: "12" }); |
| 102 | + }); |
| 103 | + |
| 104 | + test("a press is painted at once and posted as the same form data, asking for JSON", async () => { |
| 105 | + let resolve!: (r: Response) => void; |
| 106 | + answer = () => new Promise(r => (resolve = r)); |
| 107 | + const { host, up, down } = mount(); |
| 108 | + const event = submit(up); |
| 109 | + expect(event.defaultPrevented).toBe(true); |
| 110 | + expect(state(up)).toEqual({ count: "13", pressed: "true", label: "Cadeau (13)", text: "13" }); |
| 111 | + expect(state(down).pressed).toBe("false"); |
| 112 | + expect(host.hasAttribute("data-busy")).toBe(true); |
| 113 | + expect(host.getAttribute("aria-busy")).toBe("true"); |
| 114 | + expect(up.classList.contains("bounce")).toBe(true); |
| 115 | + |
| 116 | + expect(calls.length).toBe(1); |
| 117 | + expect(calls[0]!.url).toEndWith("/suggestions/42/vote"); |
| 118 | + expect(calls[0]!.init.method).toBe("POST"); |
| 119 | + expect((calls[0]!.init.headers as Record<string, string>).Accept).toBe("application/json"); |
| 120 | + const body = calls[0]!.init.body as FormData; |
| 121 | + expect(body.get("valeur")).toBe("1"); |
| 122 | + expect(body.get("csrf")).toBe("tok"); |
| 123 | + |
| 124 | + resolve(json({ up: 20, down: 5, mine: 1 })); |
| 125 | + await sleep(5); |
| 126 | + expect(state(up)).toEqual({ count: "20", pressed: "true", label: "Cadeau (20)", text: "20" }); |
| 127 | + expect(state(down)).toEqual({ count: "5", pressed: "false", label: "Charbon (5)", text: "5" }); |
| 128 | + expect(host.hasAttribute("data-busy")).toBe(false); |
| 129 | + }); |
| 130 | + |
| 131 | + test("the bounce lasts 400ms", async () => { |
| 132 | + const { up } = mount(); |
| 133 | + submit(up); |
| 134 | + expect(up.classList.contains("bounce")).toBe(true); |
| 135 | + await sleep(450); |
| 136 | + expect(up.classList.contains("bounce")).toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + test("switching sides moves the vote; pressing the chosen side takes it back", async () => { |
| 140 | + const { up, down } = mount(); |
| 141 | + answer = async () => json({ up: 13, down: 3, mine: 1 }); |
| 142 | + submit(up); |
| 143 | + await sleep(5); |
| 144 | + answer = async () => json({ up: 12, down: 4, mine: -1 }); |
| 145 | + submit(down); |
| 146 | + expect(state(up)).toMatchObject({ count: "12", pressed: "false" }); |
| 147 | + expect(state(down)).toMatchObject({ count: "4", pressed: "true", label: "Charbon (4)" }); |
| 148 | + await sleep(5); |
| 149 | + answer = async () => json({ up: 12, down: 3, mine: 0 }); |
| 150 | + submit(down); |
| 151 | + expect(state(down)).toMatchObject({ count: "3", pressed: "false" }); |
| 152 | + }); |
| 153 | + |
| 154 | + test("presses while a vote is in flight are cancelled and ignored", async () => { |
| 155 | + let resolve!: (r: Response) => void; |
| 156 | + answer = () => new Promise(r => (resolve = r)); |
| 157 | + const { up, down } = mount(); |
| 158 | + submit(up); |
| 159 | + const second = submit(down); |
| 160 | + const third = submit(up); |
| 161 | + expect(second.defaultPrevented).toBe(true); |
| 162 | + expect(third.defaultPrevented).toBe(true); |
| 163 | + expect(calls.length).toBe(1); |
| 164 | + expect(state(up)).toMatchObject({ count: "13", pressed: "true" }); |
| 165 | + resolve(json({ up: 13, down: 3, mine: 1 })); |
| 166 | + await sleep(5); |
| 167 | + submit(down); |
| 168 | + expect(calls.length).toBe(2); |
| 169 | + }); |
| 170 | + |
| 171 | + test("a refusal rolls back and toasts the server's error", async () => { |
| 172 | + answer = async () => json({ error: "Trop de votes, réessayez plus tard." }, 429); |
| 173 | + const { host, up } = mount(); |
| 174 | + submit(up); |
| 175 | + await sleep(5); |
| 176 | + expect(state(up)).toEqual({ count: "12", pressed: "false", label: "Cadeau (12)", text: "12" }); |
| 177 | + expect(host.dataset.mine).toBe("0"); |
| 178 | + expect(toasts).toEqual([{ message: "Trop de votes, réessayez plus tard.", type: "error" }]); |
| 179 | + }); |
| 180 | + |
| 181 | + test("a refusal without a message, or no network, toasts the host's error", async () => { |
| 182 | + answer = async () => new Response("oops", { status: 500 }); |
| 183 | + const { up } = mount(); |
| 184 | + submit(up); |
| 185 | + await sleep(5); |
| 186 | + answer = async () => { |
| 187 | + throw new TypeError("offline"); |
| 188 | + }; |
| 189 | + submit(up); |
| 190 | + await sleep(5); |
| 191 | + expect(state(up).pressed).toBe("false"); |
| 192 | + expect(toasts).toEqual([ |
| 193 | + { message: "Le vote n’a pas passé.", type: "error" }, |
| 194 | + { message: "Le vote n’a pas passé.", type: "error" }, |
| 195 | + ]); |
| 196 | + }); |
| 197 | + |
| 198 | + test("a captcha answer, or a 403, rolls back and submits the form natively", async () => { |
| 199 | + for (const reply of [() => json({ captcha: true }), () => json({ error: "no" }, 403)]) { |
| 200 | + answer = async () => reply(); |
| 201 | + const { host, up } = mount(); |
| 202 | + let submitted = 0; |
| 203 | + up.form!.submit = () => void submitted++; |
| 204 | + submit(up); |
| 205 | + await sleep(5); |
| 206 | + expect(submitted).toBe(1); |
| 207 | + expect(state(up).pressed).toBe("false"); |
| 208 | + expect(host.hasAttribute("data-busy")).toBe(false); |
| 209 | + expect(toasts).toEqual([]); |
| 210 | + } |
| 211 | + }); |
| 212 | + |
| 213 | + test("an OK answer that is not a tally keeps the guess", async () => { |
| 214 | + answer = async () => new Response("<p>merci</p>", { status: 200, headers: { "content-type": "text/html" } }); |
| 215 | + const { up } = mount(); |
| 216 | + submit(up); |
| 217 | + await sleep(5); |
| 218 | + expect(state(up)).toMatchObject({ count: "13", pressed: "true" }); |
| 219 | + }); |
| 220 | + |
| 221 | + test("the field attribute names another input; without two sides the forms are left alone", async () => { |
| 222 | + const { up } = mount(MARKUP.replaceAll('name="valeur"', 'name="v"').replace("<pebble-vote", '<pebble-vote field="v"')); |
| 223 | + expect(up.dataset.side).toBe("up"); |
| 224 | + document.body.textContent = ""; |
| 225 | + await sleep(0); |
| 226 | + const lone = mount(MARKUP.replaceAll('name="valeur"', 'name="v"')); |
| 227 | + expect(lone.up.dataset.side).toBeUndefined(); |
| 228 | + expect(submit(lone.up).defaultPrevented).toBe(false); |
| 229 | + }); |
| 230 | + |
| 231 | + test("the sheet: 44px targets, the reduced-motion opt-out, dark defaults", () => { |
| 232 | + expect(styles).toContain("44px"); |
| 233 | + expect(styles).toContain("prefers-reduced-motion"); |
| 234 | + expect(styles).toContain("prefers-color-scheme:dark"); |
| 235 | + expect(styles).toContain("--pebble-vote-up"); |
| 236 | + }); |
| 237 | +}); |