Skip to content

Commit

Permalink
Add test case for .use() in new routers
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangvvo committed Jul 23, 2022
1 parent b5bad44 commit 58ae0d6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
} from "./types.js";

export type Route<H> = {
prefix?: string;
method: HttpMethod | "";
fns: (H | Router<H extends FunctionLike ? H : never>)[];
isMiddle: boolean;
Expand Down
37 changes: 37 additions & 0 deletions test/edge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ test("add()", async (t) => {
t.equal(returned, ctx, "returned itself");
});

test("use()", async (t) => {
t.test("it defaults to / if base is not provided", async (t) => {
const ctx = new EdgeRouter();

// @ts-expect-error: private field
const useSpy = spyOn(ctx.router, "use");

ctx.use(noop);

t.same(useSpy.calls, [["/", noop]]);
});

t.test("it call this.router.use() with fn", async (t) => {
const ctx = new EdgeRouter();

// @ts-expect-error: private field
const useSpy = spyOn(ctx.router, "use");

ctx.use("/test", noop, noop);

t.same(useSpy.calls, [["/test", noop, noop]]);
});

t.test("it call this.router.use() with fn.router", async (t) => {
const ctx = new EdgeRouter();
const ctx2 = new EdgeRouter();

// @ts-expect-error: private field
const useSpy = spyOn(ctx.router, "use");

ctx.use("/test", ctx2, ctx2);

// @ts-expect-error: private field
t.same(useSpy.calls, [["/test", ctx2.router, ctx2.router]]);
});
});

test("clone()", (t) => {
const ctx = new EdgeRouter();
// @ts-expect-error: private property
Expand Down
37 changes: 37 additions & 0 deletions test/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,43 @@ test("add()", async (t) => {
t.equal(returned, ctx, "returned itself");
});

test("use()", async (t) => {
t.test("it defaults to / if base is not provided", async (t) => {
const ctx = new NodeRouter();

// @ts-expect-error: private field
const useSpy = spyOn(ctx.router, "use");

ctx.use(noop);

t.same(useSpy.calls, [["/", noop]]);
});

t.test("it call this.router.use() with fn", async (t) => {
const ctx = new NodeRouter();

// @ts-expect-error: private field
const useSpy = spyOn(ctx.router, "use");

ctx.use("/test", noop, noop);

t.same(useSpy.calls, [["/test", noop, noop]]);
});

t.test("it call this.router.use() with fn.router", async (t) => {
const ctx = new NodeRouter();
const ctx2 = new NodeRouter();

// @ts-expect-error: private field
const useSpy = spyOn(ctx.router, "use");

ctx.use("/test", ctx2, ctx2);

// @ts-expect-error: private field
t.same(useSpy.calls, [["/test", ctx2.router, ctx2.router]]);
});
});

test("clone()", (t) => {
const ctx = new NodeRouter();
// @ts-expect-error: private property
Expand Down

0 comments on commit 58ae0d6

Please sign in to comment.