Skip to content

Commit

Permalink
fix: fix + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rawnly committed Feb 23, 2023
1 parent 313f6c4 commit 870e4da
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from "next/server";
import { test, expect } from "vitest";

import { type Middleware } from "../src/types";
import { addParams, findMiddleware, getParams } from "../src/utils";
import { addParams, findMiddleware, getParams, inject } from "../src/utils";

const queryForDomain = { domain: "app.acme.org", path: "/" };

Expand Down Expand Up @@ -108,8 +108,24 @@ test("should add the params", () => {
"/dashboard/it"
);

expect(requestWithParams).toHaveProperty("params");
expect(request).toHaveProperty("params");
expect(requestWithParams.params).toHaveProperty("lang");
expect(requestWithParams.params).not.toHaveProperty("path");
expect(requestWithParams.params.lang).toBe("it");
});

test("shuld inject", () => {
const middleware = findMiddleware(middlewares, queryForPath);

expect(middleware).not.toBeUndefined();
expect(middleware).toHaveProperty("matcher");

if (!middleware?.matcher) return;

const request = new NextRequest(new URL("http://localhost:3000"));
const injectedRequest = inject(request, { ok: true });

expect(request).toHaveProperty("injected");
expect(injectedRequest).toHaveProperty("injected");
expect(injectedRequest.injected).toHaveProperty("ok", true);
});
3 changes: 2 additions & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { UrlParams } from "@/types";
import { NextRequest } from "next/server";
import { test, expect } from "vitest";

import { UrlParams } from "@/types";

import { parse, replaceValues } from "../src/utils";

test("should parse next url", () => {
Expand Down
1 change: 0 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export const getInjectorDescriptor = <T>(data: T): PropertyDescriptor => ({
enumerable: true,
writable: false,
value: data,
get: () => data,
});

export const inject = <T>(
Expand Down

0 comments on commit 870e4da

Please sign in to comment.