From cd6bc0e86fb6067afa1f8d7289a8587303737662 Mon Sep 17 00:00:00 2001 From: PJ Date: Mon, 27 Apr 2026 00:05:34 +0700 Subject: [PATCH] test(spec): cover whenRoute matching, gating, and array routes --- pkg/spec/test/api.test.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pkg/spec/test/api.test.ts b/pkg/spec/test/api.test.ts index f919bf8..e97502a 100644 --- a/pkg/spec/test/api.test.ts +++ b/pkg/spec/test/api.test.ts @@ -20,6 +20,7 @@ import { taps, waitOnce, weighted, + whenRoute, } from "../src/index.ts"; import type { AccessibilityElement, @@ -323,6 +324,41 @@ test("keyedBy substitutes empty strings for missing children", () => { ); }); +test("whenRoute returns [] when current route does not match", () => { + installFakeRuntime(); + const route = { current: "home" as string | null }; + let bodyCalled = false; + const generator = whenRoute(route, "ledger", () => { + bodyCalled = true; + return [Tap({ on: "id:x" })]; + }); + assert.deepEqual(generator.generate(), []); + assert.equal(bodyCalled, false); +}); + +test("whenRoute calls body when current route matches", () => { + installFakeRuntime(); + const route = { current: "ledger" as string | null }; + const generator = whenRoute(route, "ledger", () => [Tap({ on: "id:x" })]); + const result = generator.generate(); + assert.equal(result.length, 1); + assert.equal(result[0]?.kind, "Tap"); +}); + +test("whenRoute accepts an array of allowed routes", () => { + installFakeRuntime(); + const route = { current: "add-account" as string | null }; + const generator = whenRoute(route, ["home", "add-account"], () => [Tap({ on: "id:x" })]); + assert.equal(generator.generate().length, 1); +}); + +test("whenRoute returns [] for null route", () => { + installFakeRuntime(); + const route = { current: null as string | null }; + const generator = whenRoute(route, ["home"], () => [Tap({ on: "id:x" })]); + assert.deepEqual(generator.generate(), []); +}); + test("default generators proxy through to the runtime", () => { installFakeRuntime(); assert.equal(taps.__sanderlingActionGenerator, true);