diff options
author | 2022-11-22 02:13:03 -0800 | |
---|---|---|
committer | 2022-11-22 02:13:03 -0800 | |
commit | d21aee514375e12335c61936ae302cd67f1f73bf (patch) | |
tree | e4985c55d27f7d1bcb7c8fa56441bd4711e15433 /test/bun.js/filesystem_router.test.ts | |
parent | 65a56c2560a1ef7b036995a8eade45236f76f819 (diff) | |
download | bun-d21aee514375e12335c61936ae302cd67f1f73bf.tar.gz bun-d21aee514375e12335c61936ae302cd67f1f73bf.tar.zst bun-d21aee514375e12335c61936ae302cd67f1f73bf.zip |
Introduce `Bun.FileSystemRouter` API
Diffstat (limited to 'test/bun.js/filesystem_router.test.ts')
-rw-r--r-- | test/bun.js/filesystem_router.test.ts | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/test/bun.js/filesystem_router.test.ts b/test/bun.js/filesystem_router.test.ts new file mode 100644 index 000000000..c375cbb22 --- /dev/null +++ b/test/bun.js/filesystem_router.test.ts @@ -0,0 +1,85 @@ +import { FileSystemRouter } from "bun"; +import { it, expect } from "bun:test"; +import path, { dirname, resolve } from "path"; +import fs, { realpathSync, rmSync } from "fs"; +import { tmpdir } from "os"; + +function createTree(basedir, paths) { + for (const end of paths) { + const abs = path.join(basedir, end); + try { + const dir = dirname(abs); + if (dir.length > 0 && dir !== "/") fs.mkdirSync(dir, { recursive: true }); + } catch (e) {} + fs.writeFileSync(abs, "export default " + JSON.stringify(end) + ";\n"); + } +} + +it("should find files", () => { + const tempdir = realpathSync(tmpdir()) + "/"; + + rmSync(tempdir + "fs-router-test-01", { recursive: true, force: true }); + + createTree(tempdir + "fs-router-test-01", [ + "a.tsx", + "b.tsx", + "abc/[id].tsx", + "abc/index.tsx", + "abc/def/[id].tsx", + "abc/def/index.tsx", + "abc/def/ghi/[id].tsx", + "abc/def/ghi/index.tsx", + "abc/def/ghi/jkl/[id].tsx", + "abc/def/ghi/jkl/index.tsx", + "[id].tsx", + "index.tsx", + "foo/[id].tsx", + "catch-all/[...id].tsx", + ]); + + const router = new FileSystemRouter({ + dir: tempdir + "fs-router-test-01/", + fileExtensions: [".tsx"], + style: "nextjs", + }); + + const routes = router.routes; + const fixture = { + "/": `${tempdir}fs-router-test-01/index.tsx`, + "/[id]": `${tempdir}fs-router-test-01/[id].tsx`, + "/a": `${tempdir}fs-router-test-01/a.tsx`, + "/abc": `${tempdir}fs-router-test-01/abc/index.tsx`, + "/abc/[id]": `${tempdir}fs-router-test-01/abc/[id].tsx`, + "/abc/def/[id]": `${tempdir}fs-router-test-01/abc/def/[id].tsx`, + "/abc/def/ghi": `${tempdir}fs-router-test-01/abc/def/ghi/index.tsx`, + "/abc/def/ghi/[id]": `${tempdir}fs-router-test-01/abc/def/ghi/[id].tsx`, + "/abc/def/ghi/jkl": `${tempdir}fs-router-test-01/abc/def/ghi/jkl/index.tsx`, + "/abc/def/ghi/jkl/[id]": `${tempdir}fs-router-test-01/abc/def/ghi/jkl/[id].tsx`, + "/abc/def": `${tempdir}fs-router-test-01/abc/def/index.tsx`, + "/b": `${tempdir}fs-router-test-01/b.tsx`, + "/foo/[id]": `${tempdir}fs-router-test-01/foo/[id].tsx`, + "/catch-all/[...id]": `${tempdir}fs-router-test-01/catch-all/[...id].tsx`, + }; + for (const route in fixture) { + if (!(route in routes)) { + throw new Error(`Route ${route} not found`); + } + + expect(routes[route]).toBe(fixture[route]); + } + + expect(Object.keys(routes).length).toBe(Object.keys(fixture).length); + + expect(router.match("/never/gonna/give/you/up")).toBe(null); + expect( + router.match( + "/catch-all/we-are-no-strangers-to-love/you/know/the/rules/and/so/do/i", + ).params.id, + ).toBe("we-are-no-strangers-to-love/you/know/the/rules/and/so/do/i"); + expect(router.match("/").name).toBe("/"); + expect(router.match("/index").name).toBe("/"); + expect(router.match("/index/").name).toBe("/"); + expect(router.match("/a").name).toBe("/a"); + expect(router.match("/b").name).toBe("/b"); + expect(router.match("/abc/123").params.id).toBe("123"); +}); |