|  | 
|  | 1 | +import { useBody, useHeaders, useParams, useQuery } from "@backhooks/hooks"; | 
|  | 2 | +import * as supertest from "supertest"; | 
|  | 3 | +import { SuperTest, Test } from "supertest"; | 
|  | 4 | +import { App, toNodeListener, createApp, eventHandler, createRouter } from "h3"; | 
|  | 5 | +import { makeHookableApp } from "../src"; | 
|  | 6 | + | 
|  | 7 | +let app: App; | 
|  | 8 | +let request: SuperTest<Test>; | 
|  | 9 | + | 
|  | 10 | +beforeEach(() => { | 
|  | 11 | +  app = createApp({ debug: true }); | 
|  | 12 | +  makeHookableApp(app); | 
|  | 13 | +  request = supertest(toNodeListener(app)); | 
|  | 14 | +}); | 
|  | 15 | + | 
|  | 16 | +test("useBody", async () => { | 
|  | 17 | +  app.use( | 
|  | 18 | +    "/body", | 
|  | 19 | +    eventHandler(() => { | 
|  | 20 | +      const body = useBody(); | 
|  | 21 | +      return body; | 
|  | 22 | +    }) | 
|  | 23 | +  ); | 
|  | 24 | + | 
|  | 25 | +  const res = await request.post("/body").send({ | 
|  | 26 | +    foo: "bar", | 
|  | 27 | +  }); | 
|  | 28 | + | 
|  | 29 | +  expect(res.body.foo).toBe("bar"); | 
|  | 30 | +}); | 
|  | 31 | + | 
|  | 32 | +test("useHeaders", async () => { | 
|  | 33 | +  app.use( | 
|  | 34 | +    "/headers", | 
|  | 35 | +    eventHandler(() => { | 
|  | 36 | +      const headers = useHeaders(); | 
|  | 37 | +      return headers; | 
|  | 38 | +    }) | 
|  | 39 | +  ); | 
|  | 40 | +  const res = await request.get("/headers").send().set({ | 
|  | 41 | +    foo: "bar", | 
|  | 42 | +  }); | 
|  | 43 | +  expect(res.body.foo).toBe("bar"); | 
|  | 44 | +}); | 
|  | 45 | + | 
|  | 46 | +test("useParams", async () => { | 
|  | 47 | +  const router = createRouter(); | 
|  | 48 | +  router.get( | 
|  | 49 | +    "/:foo", | 
|  | 50 | +    eventHandler(() => { | 
|  | 51 | +      const params = useParams(); | 
|  | 52 | +      return params; | 
|  | 53 | +    }) | 
|  | 54 | +  ); | 
|  | 55 | +  app.use(router); | 
|  | 56 | +  const res = await request.get("/bar").send(); | 
|  | 57 | +  expect(res.body.foo).toBe("bar"); | 
|  | 58 | +}); | 
|  | 59 | + | 
|  | 60 | +test("useQuery", async () => { | 
|  | 61 | +  app.use( | 
|  | 62 | +    "/", | 
|  | 63 | +    eventHandler(() => { | 
|  | 64 | +      const query = useQuery(); | 
|  | 65 | +      return query; | 
|  | 66 | +    }) | 
|  | 67 | +  ); | 
|  | 68 | +  const res = await request.get("/?foo=bar").send(); | 
|  | 69 | +  expect(res.body.foo).toBe("bar"); | 
|  | 70 | +}); | 
0 commit comments