Browse Source

add controller class test

Richard Köhl 2 năm trước cách đây
mục cha
commit
f1506fd1d5
4 tập tin đã thay đổi với 31 bổ sung3 xóa
  1. 2 1
      deno.jsonc
  2. 2 1
      deno.lock
  3. 1 1
      test/deps.ts
  4. 26 0
      test/interfaces/controllers/controller.class.test.ts

+ 2 - 1
deno.jsonc

@@ -5,11 +5,12 @@
     "infra/": "./src/infrastructure/",
     "if/": "./src/interfaces/",
     "deps": "./src/deps.ts",
+    "test-deps": "./test/deps.ts",
     "std/": "https://deno.land/std@0.191.0/"
   },
   "tasks": {
     "dev": "deno run --watch --allow-read --allow-env --allow-net ./src/main.ts",
-    "test": "deno test ./test/test.ts"
+    "test": "deno test --allow-read --allow-env ./test/"
   },
   "lint": {
     "include": [

+ 2 - 1
deno.lock

@@ -51,6 +51,7 @@
     "https://deno.land/std@0.191.0/log/mod.ts": "36d156ad18de3f1806c6ddafa4965129be99ccafc27f1813de528d65b6c528bf",
     "https://deno.land/std@0.191.0/testing/_diff.ts": "1a3c044aedf77647d6cac86b798c6417603361b66b54c53331b312caeb447aea",
     "https://deno.land/std@0.191.0/testing/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7",
-    "https://deno.land/std@0.191.0/testing/asserts.ts": "e16d98b4d73ffc4ed498d717307a12500ae4f2cbe668f1a215632d19fcffc22f"
+    "https://deno.land/std@0.191.0/testing/asserts.ts": "e16d98b4d73ffc4ed498d717307a12500ae4f2cbe668f1a215632d19fcffc22f",
+    "https://deno.land/std@0.191.0/types.d.ts": "dbaeb2c4d7c526db9828fc8df89d8aecf53b9ced72e0c4568f97ddd8cda616a4"
   }
 }

+ 1 - 1
test/deps.ts

@@ -1 +1 @@
-export { assert } from 'std/testing/asserts.ts';
+export { assert, assertEquals } from 'std/testing/asserts.ts';

+ 26 - 0
test/interfaces/controllers/controller.class.test.ts

@@ -0,0 +1,26 @@
+import { assertEquals } from 'test-deps';
+import Controller from 'if/controllers/controller.class.ts';
+
+Deno.test('Controller class', async () => {
+  const mockHandler = (req: Request) => new Response('Hello, world!');
+  const controller = new Controller({
+    '/_GET': mockHandler,
+  });
+
+  // Test that the handler was stored correctly
+  assertEquals(controller.handlers['/_GET'], mockHandler);
+
+  // Test the response method
+  const req = new Request('http://localhost/');
+  const res = Controller.response(req, 'Hello, world!');
+  assertEquals(res.status, 200);
+  assertEquals(
+    new TextDecoder().decode(new Uint8Array(await res.arrayBuffer())),
+    'Hello, world!',
+  );
+
+  // Test the responseJSON method
+  const jsonRes = Controller.responseJSON(req, { message: 'Hello, world!' });
+  assertEquals(jsonRes.status, 200);
+  assertEquals(await jsonRes.json(), { message: 'Hello, world!' });
+});