diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 46f5c4b..c177ec2 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -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: "/" }; @@ -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); +}); diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 9dbd6a0..81cb5f1 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -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", () => { diff --git a/src/utils.ts b/src/utils.ts index 07f27a6..603dc6e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -59,7 +59,6 @@ export const getInjectorDescriptor = (data: T): PropertyDescriptor => ({ enumerable: true, writable: false, value: data, - get: () => data, }); export const inject = (