GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — yieldparser/src/routing.test.ts at master · JavaScriptRegenerated/yieldparser Expand file tree/ Copy pathrouting.test.tsMore file actions 199 lines (175 loc) · 5.13 KB / Copy pathrouting.test.tsFile metadata and controls199 lines (175 loc) · 5.13 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 import { describe, expect, it } from "./test-deps.ts"; import { invert, mustEnd, parse } from "./index.ts"; describe("Router", () => { type Route = | { type: "home" } | { type: "about" } | { type: "albums" } | { type: "album"; id: string } | { type: "albumArt"; id: string }; function* Home() { yield "/"; yield mustEnd; return { type: "home" } as Route; } function* About() { yield "/about"; yield mustEnd; return { type: "about" } as Route; } const Albums = { *List() { yield "/albums"; yield mustEnd; return { type: "albums" } as Route; }, *ItemPrefix() { yield "/albums/"; const [id]: [string] = yield /^\d+/; return { id }; }, *Item() { const { id }: { id: string } = yield Albums.ItemPrefix; yield mustEnd; return { type: "album", id } as Route; }, *ItemArt() { const { id }: { id: string } = yield Albums.ItemPrefix; yield "/art"; yield mustEnd; return { type: "albumArt", id } as Route; }, }; function* AlbumRoutes() { return yield [Albums.List, Albums.Item, Albums.ItemArt]; } function* Route() { return yield [Home, About, AlbumRoutes]; } it("works with home", () => { expect(parse("/", Route())).toEqual({ success: true, result: { type: "home" }, remaining: "", }); }); it("works with about", () => { expect(parse("/about", Route())).toEqual({ success: true, result: { type: "about" }, remaining: "", }); }); it("works with albums", () => { expect(parse("/albums", Route())).toEqual({ success: true, result: { type: "albums" }, remaining: "", }); }); it("works with album for id", () => { expect(parse("/albums/42", Route())).toEqual({ success: true, result: { type: "album", id: "42" }, remaining: "", }); }); it("works with album art for id", () => { expect(parse("/albums/42/art", Route())).toEqual({ success: true, result: { type: "albumArt", id: "42" }, remaining: "", }); }); }); describe("Router inversion", () => { type Route = | { type: "home" } | { type: "about" } | { type: "terms" } | { type: "albums" } | { type: "album"; id: string } | { type: "albumArt"; id: string }; function* Home() { yield "/"; yield mustEnd; return { type: "home" } as Route; } function* About() { yield "/about"; yield mustEnd; return { type: "about" } as Route; } function* Terms() { yield "/legal"; yield "/terms"; yield mustEnd; return { type: "terms" } as Route; } function* AlbumItem() { yield "/albums/"; const [id]: [string] = yield /^\d+/; return { type: "album", id }; } function* blogPrefix() { yield "/blog"; } function* BlogHome() { yield blogPrefix; yield mustEnd; return { type: "blog" }; } function* BlogArticle() { yield blogPrefix; yield "/"; const [slug]: [string] = yield /^.+/; return { type: "blogArticle", slug }; } function* BlogRoutes() { return yield [BlogHome, BlogArticle]; } function* Routes() { return yield [Home, About, Terms]; } function* DoubleNested() { return yield [BlogRoutes, Routes]; } it("works with single route definition", () => { expect(invert({ type: "home" }, Home())).toEqual("/"); expect(invert({ type: "about" }, About())).toEqual("/about"); expect(invert({ type: "terms" }, Terms())).toEqual("/legal/terms"); expect(invert({ type: "BLAH" }, Terms())).toBeNull(); }); it("works with single route definition with param", () => { expect(invert({ type: "album", id: "123" }, AlbumItem())).toEqual( "/albums/123", ); expect(invert({ type: "album", id: "678" }, AlbumItem())).toEqual( "/albums/678", ); expect(invert({ type: "album", id: "abc" }, AlbumItem())).toBeNull(); expect(invert({ type: "BLAH", id: "123" }, AlbumItem())).toBeNull(); }); it("works with nested routes", () => { expect(invert({ type: "home" }, Routes())).toEqual("/"); expect(invert({ type: "about" }, Routes())).toEqual("/about"); expect(invert({ type: "terms" }, Routes())).toEqual("/legal/terms"); expect(invert({ type: "BLAH" }, Routes())).toBeNull(); }); it("works with routes with nested prefix", () => { expect(invert({ type: "blog" }, BlogHome())).toEqual("/blog"); expect(invert({ type: "blogArticle", slug: "hello-world" }, BlogArticle())) .toEqual("/blog/hello-world"); expect(invert({ type: "blog" }, BlogRoutes())).toEqual("/blog"); expect(invert({ type: "blogArticle", slug: "hello-world" }, BlogRoutes())) .toEqual("/blog/hello-world"); expect(invert({ type: "BLAH" }, BlogRoutes())).toBeNull(); }); it("all works with double nested routes", () => { expect(invert({ type: "home" }, DoubleNested())).toEqual("/"); expect(invert({ type: "blog" }, DoubleNested())).toEqual("/blog"); expect(invert({ type: "blogArticle", slug: "hello-world" }, DoubleNested())) .toEqual("/blog/hello-world"); expect(invert({ type: "BLAH" }, DoubleNested())).toBeNull(); }); }); |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |