library
Prebuilt pebble components: drop-in custom elements, no compiler needed
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runAucun
0Étoiles
0Forks
2Issues
| 1 | // The per-toast timers against a fake timer, ported from the toboggan and |
| 2 | // toki iterations: a subject per call, fired by the test. No real time, no |
| 3 | // DOM. The toast is a signal taken back into time with fromSignal, as the |
| 4 | // card does. |
| 5 | |
| 6 | import { describe, expect, test } from "bun:test"; |
| 7 | import { fromSignal, signal, subject, subscribeUnowned, type Signal } from "@pebble/pebble"; |
| 8 | import { exit, expiry, type Timer } from "../packages/toaster/lifetime"; |
| 9 | import { toastFromDetail, toasterReducer, type Toast } from "../packages/toaster/reducers"; |
| 10 | |
| 11 | /** Every timer asked for, in order, so a test can fire one. */ |
| 12 | const fakeTimer = () => { |
| 13 | const asked: { ms: number; fire: () => void }[] = []; |
| 14 | const timer: Timer = (ms) => { |
| 15 | const tick = subject<void>(); |
| 16 | asked.push({ ms, fire: () => tick.next() }); |
| 17 | return tick; |
| 18 | }; |
| 19 | return { asked, timer }; |
| 20 | }; |
| 21 | |
| 22 | const fresh = (now = 1000): Toast => toastFromDetail({ duration: 5000 }, "t", now); |
| 23 | |
| 24 | const hover = (toast: Signal<Toast>, hovered: boolean, now: number) => |
| 25 | toast(toasterReducer({ toasts: [toast()], expanded: false }, { type: "hover", id: "t", hovered, now }).toasts[0]!); |
| 26 | |
| 27 | describe("expiry", () => { |
| 28 | test("fires after the whole duration of a toast left alone", () => { |
| 29 | const toast = signal(fresh()); |
| 30 | const { asked, timer } = fakeTimer(); |
| 31 | let fired = 0; |
| 32 | subscribeUnowned(expiry(fromSignal(toast), timer))(() => fired++); |
| 33 | expect(asked.map((a) => a.ms)).toEqual([5000]); |
| 34 | asked[0]!.fire(); |
| 35 | expect(fired).toBe(1); |
| 36 | }); |
| 37 | |
| 38 | test("a hover cancels the run; the resume asks for what is left", () => { |
| 39 | const toast = signal(fresh(1000)); |
| 40 | const { asked, timer } = fakeTimer(); |
| 41 | let fired = 0; |
| 42 | subscribeUnowned(expiry(fromSignal(toast), timer))(() => fired++); |
| 43 | // hovered after 2s: paused |
| 44 | hover(toast, true, 3000); |
| 45 | asked[0]!.fire(); |
| 46 | expect(fired).toBe(0); |
| 47 | // resumed: 3s remain |
| 48 | hover(toast, false, 4000); |
| 49 | expect(asked.map((a) => a.ms)).toEqual([5000, 3000]); |
| 50 | asked[1]!.fire(); |
| 51 | expect(fired).toBe(1); |
| 52 | }); |
| 53 | |
| 54 | test("a dismissed toast never expires, and an expired one fires once", () => { |
| 55 | const toast = signal<Toast>({ ...fresh(), dismissed: true }); |
| 56 | const { asked, timer } = fakeTimer(); |
| 57 | let fired = 0; |
| 58 | subscribeUnowned(expiry(fromSignal(toast), timer))(() => fired++); |
| 59 | expect(asked).toEqual([]); |
| 60 | |
| 61 | const alive = signal(fresh()); |
| 62 | const second = fakeTimer(); |
| 63 | subscribeUnowned(expiry(fromSignal(alive), second.timer))(() => fired++); |
| 64 | second.asked[0]!.fire(); |
| 65 | second.asked[0]!.fire(); |
| 66 | expect(fired).toBe(1); |
| 67 | }); |
| 68 | }); |
| 69 | |
| 70 | describe("exit", () => { |
| 71 | test("fires EXIT_MS after the dismissal, once", () => { |
| 72 | const toast = signal(fresh()); |
| 73 | const { asked, timer } = fakeTimer(); |
| 74 | let fired = 0; |
| 75 | subscribeUnowned(exit(fromSignal(toast), timer))(() => fired++); |
| 76 | expect(asked).toEqual([]); |
| 77 | toast({ ...toast(), dismissed: true }); |
| 78 | expect(asked.map((a) => a.ms)).toEqual([300]); |
| 79 | asked[0]!.fire(); |
| 80 | toast({ ...toast(), hovered: true }); |
| 81 | expect(fired).toBe(1); |
| 82 | expect(asked.length).toBe(1); |
| 83 | }); |
| 84 | }); |