generated from expressots/expressots-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit tests for serverShutdown and setGlobalRoutePrefix methods
- Loading branch information
Showing
10 changed files
with
902 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/adapter-express/application-express.early.spec/serverShutdown.early.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Unit tests for: serverShutdown | ||
|
||
import process from "process"; | ||
import { AppExpress } from "../application-express"; | ||
|
||
jest.mock("../render/engine", () => { | ||
const actual = jest.requireActual("../render/engine"); | ||
return { | ||
...actual, | ||
setEngineEjs: jest.fn(), | ||
setEngineHandlebars: jest.fn(), | ||
setEnginePug: jest.fn(), | ||
}; | ||
}); | ||
|
||
jest.mock("fs"); | ||
jest.mock("process", () => ({ | ||
...jest.requireActual("process"), | ||
exit: jest.fn((code?: number) => { | ||
throw new Error(`Mocked process.exit called with code ${code}`); | ||
}), | ||
})); | ||
|
||
class MockLogger { | ||
error = jest.fn(); | ||
} | ||
|
||
class MockConsole { | ||
messageServer = jest.fn(); | ||
} | ||
|
||
describe("AppExpress.serverShutdown() serverShutdown method", () => { | ||
let appExpress: AppExpress; | ||
|
||
beforeEach(() => { | ||
appExpress = new AppExpress(); | ||
appExpress["logger"] = new MockLogger() as any; | ||
appExpress["console"] = new MockConsole() as any; | ||
|
||
jest.spyOn(appExpress as any, "serverShutdown").mockImplementation(() => { | ||
console.log("Mocked serverShutdown called"); | ||
}); | ||
}); | ||
|
||
describe("Happy Paths", () => { | ||
it("should call serverShutdown and exit the process", () => { | ||
// Act & Assert | ||
expect(() => appExpress["handleExit"]()).toThrow("Mocked process.exit called with code 0"); | ||
expect((appExpress as any).serverShutdown).toHaveBeenCalled(); | ||
expect(process.exit).toHaveBeenCalledWith(0); | ||
}); | ||
}); | ||
|
||
describe("Edge Cases", () => { | ||
it("should handle serverShutdown gracefully when no additional logic is present", () => { | ||
// Act & Assert | ||
expect(() => appExpress["handleExit"]()).toThrow("Mocked process.exit called with code 0"); | ||
expect((appExpress as any).serverShutdown).toHaveBeenCalled(); | ||
expect(process.exit).toHaveBeenCalledWith(0); | ||
}); | ||
}); | ||
}); |
76 changes: 76 additions & 0 deletions
76
src/adapter-express/application-express.early.spec/setEngine.early.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Unit tests for: setEngine | ||
|
||
import { RenderEngine } from "../../../node_modules/@expressots/shared"; | ||
import { IMiddleware } from "../../../node_modules/@expressots/core"; | ||
import { AppExpress } from "../application-express"; | ||
|
||
// Mocking the necessary functions from './render/engine' | ||
jest.mock("../render/engine", () => { | ||
const actual = jest.requireActual("../render/engine"); | ||
return { | ||
...actual, | ||
setEngineEjs: jest.fn(), | ||
setEngineHandlebars: jest.fn(), | ||
setEnginePug: jest.fn(), | ||
}; | ||
}); | ||
|
||
// Mock classes and types | ||
class MockLogger { | ||
error = jest.fn(); | ||
} | ||
|
||
class MockConsole { | ||
messageServer = jest.fn(); | ||
} | ||
|
||
class MockAppContainer { | ||
Container = {}; | ||
} | ||
|
||
class MockProviderManager {} | ||
|
||
interface MockIMiddleware {} | ||
|
||
describe("AppExpress.setEngine() setEngine method", () => { | ||
let appExpress: AppExpress; | ||
|
||
beforeEach(() => { | ||
appExpress = new AppExpress() as any; | ||
appExpress["logger"] = new MockLogger() as any; | ||
appExpress["console"] = new MockConsole() as any; | ||
appExpress["appContainer"] = new MockAppContainer() as any; | ||
appExpress["providerManager"] = new MockProviderManager() as any; | ||
appExpress["middlewareManager"] = {} as MockIMiddleware as IMiddleware; | ||
}); | ||
|
||
describe("Happy Paths", () => { | ||
it("should set the render options with engine and options", async () => { | ||
const engine = RenderEngine.Engine.EJS; | ||
const options: RenderEngine.EjsOptions = { viewsDir: "views" }; | ||
|
||
await appExpress.setEngine(engine, options); | ||
|
||
expect(appExpress["renderOptions"]).toEqual({ engine, options }); | ||
}); | ||
|
||
it("should set the render options with engine only", async () => { | ||
const engine = RenderEngine.Engine.PUG; | ||
|
||
await appExpress.setEngine(engine); | ||
|
||
expect(appExpress["renderOptions"]).toEqual({ engine }); | ||
}); | ||
}); | ||
|
||
describe("Edge Cases", () => { | ||
it("should handle unsupported engine type gracefully", async () => { | ||
const engine = "UNSUPPORTED_ENGINE" as RenderEngine.Engine; | ||
|
||
await expect(appExpress.setEngine(engine)).resolves.not.toThrow(); | ||
expect(appExpress["renderOptions"]).toEqual({ engine }); | ||
}); | ||
}); | ||
}); | ||
|
||
// End of unit tests for: setEngine |
60 changes: 60 additions & 0 deletions
60
src/adapter-express/application-express.early.spec/setGlobalRoutePrefix.early.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Unit tests for: setGlobalRoutePrefix | ||
|
||
import { AppExpress } from "../application-express"; | ||
|
||
// Mocking the necessary functions from './render/engine' | ||
jest.mock("../render/engine", () => { | ||
const actual = jest.requireActual("../render/engine"); | ||
return { | ||
...actual, | ||
setEngineEjs: jest.fn(), | ||
setEngineHandlebars: jest.fn(), | ||
setEnginePug: jest.fn(), | ||
}; | ||
}); | ||
|
||
// Mock classes and types | ||
|
||
describe("AppExpress.setGlobalRoutePrefix() setGlobalRoutePrefix method", () => { | ||
let appExpress: AppExpress; | ||
|
||
beforeEach(() => { | ||
appExpress = new AppExpress() as any; | ||
}); | ||
|
||
describe("Happy Paths", () => { | ||
it("should set the global route prefix to a valid string", () => { | ||
const prefix = "/api"; | ||
appExpress.setGlobalRoutePrefix(prefix); | ||
expect((appExpress as any).globalPrefix).toBe(prefix); | ||
}); | ||
|
||
it("should set the global route prefix to an empty string", () => { | ||
const prefix = ""; | ||
appExpress.setGlobalRoutePrefix(prefix); | ||
expect((appExpress as any).globalPrefix).toBe(prefix); | ||
}); | ||
}); | ||
|
||
describe("Edge Cases", () => { | ||
it("should handle setting a prefix with special characters", () => { | ||
const prefix = "/api/v1!"; | ||
appExpress.setGlobalRoutePrefix(prefix); | ||
expect((appExpress as any).globalPrefix).toBe(prefix); | ||
}); | ||
|
||
it("should handle setting a prefix with a long string", () => { | ||
const prefix = "/".repeat(1000); | ||
appExpress.setGlobalRoutePrefix(prefix); | ||
expect((appExpress as any).globalPrefix).toBe(prefix); | ||
}); | ||
|
||
it("should handle setting a prefix with spaces", () => { | ||
const prefix = "/api v1"; | ||
appExpress.setGlobalRoutePrefix(prefix); | ||
expect((appExpress as any).globalPrefix).toBe(prefix); | ||
}); | ||
}); | ||
}); | ||
|
||
// End of unit tests for: setGlobalRoutePrefix |
121 changes: 121 additions & 0 deletions
121
...er-express/micro-api/application-express-micro-route.early.spec/applyRoutes.early.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Unit tests for: applyRoutes | ||
|
||
import express from "express"; | ||
import { Route } from "../application-express-micro-route"; | ||
|
||
// Mock classes and interfaces | ||
|
||
describe("Route.applyRoutes() applyRoutes method", () => { | ||
let mockApp: express.Application; | ||
let route: Route; | ||
|
||
beforeEach(() => { | ||
mockApp = { | ||
get: jest.fn(), | ||
post: jest.fn(), | ||
put: jest.fn(), | ||
delete: jest.fn(), | ||
patch: jest.fn(), | ||
} as any; | ||
|
||
route = new Route(mockApp as any); | ||
}); | ||
|
||
describe("Happy paths", () => { | ||
it("should apply a GET route correctly", () => { | ||
const handler = jest.fn(); | ||
const middleware = [jest.fn()]; | ||
|
||
route.get("/test", handler, ...middleware); | ||
route.applyRoutes(); | ||
|
||
expect(mockApp.get).toHaveBeenCalledWith("/test", ...middleware, handler); | ||
}); | ||
|
||
it("should apply a POST route correctly", () => { | ||
const handler = jest.fn(); | ||
const middleware = [jest.fn()]; | ||
|
||
route.post("/test", handler, ...middleware); | ||
route.applyRoutes(); | ||
|
||
expect(mockApp.post).toHaveBeenCalledWith("/test", ...middleware, handler); | ||
}); | ||
|
||
it("should apply a PUT route correctly", () => { | ||
const handler = jest.fn(); | ||
const middleware = [jest.fn()]; | ||
|
||
route.put("/test", handler, ...middleware); | ||
route.applyRoutes(); | ||
|
||
expect(mockApp.put).toHaveBeenCalledWith("/test", ...middleware, handler); | ||
}); | ||
|
||
it("should apply a DELETE route correctly", () => { | ||
const handler = jest.fn(); | ||
const middleware = [jest.fn()]; | ||
|
||
route.delete("/test", handler, ...middleware); | ||
route.applyRoutes(); | ||
|
||
expect(mockApp.delete).toHaveBeenCalledWith("/test", ...middleware, handler); | ||
}); | ||
|
||
it("should apply a PATCH route correctly", () => { | ||
const handler = jest.fn(); | ||
const middleware = [jest.fn()]; | ||
|
||
route.patch("/test", handler, ...middleware); | ||
route.applyRoutes(); | ||
|
||
expect(mockApp.patch).toHaveBeenCalledWith("/test", ...middleware, handler); | ||
}); | ||
}); | ||
|
||
describe("Edge cases", () => { | ||
it("should handle routes with no middleware", () => { | ||
const handler = jest.fn(); | ||
|
||
route.get("/test", handler); | ||
route.applyRoutes(); | ||
|
||
expect(mockApp.get).toHaveBeenCalledWith("/test", handler); | ||
}); | ||
|
||
it("should handle routes with multiple middleware", () => { | ||
const handler = jest.fn(); | ||
const middleware1 = jest.fn(); | ||
const middleware2 = jest.fn(); | ||
|
||
route.get("/test", handler, middleware1, middleware2); | ||
route.applyRoutes(); | ||
|
||
expect(mockApp.get).toHaveBeenCalledWith("/test", middleware1, middleware2, handler); | ||
}); | ||
|
||
it("should apply global prefix to routes", () => { | ||
const handler = jest.fn(); | ||
const middleware = [jest.fn()]; | ||
|
||
route.setGlobalRoutePrefix("/api"); | ||
route.get("/test", handler, ...middleware); | ||
route.applyRoutes(); | ||
|
||
expect(mockApp.get).toHaveBeenCalledWith("/api/test", ...middleware, handler); | ||
}); | ||
|
||
it("should normalize paths correctly", () => { | ||
const handler = jest.fn(); | ||
const middleware = [jest.fn()]; | ||
|
||
route.setGlobalRoutePrefix("/api/"); | ||
route.get("/test", handler, ...middleware); | ||
route.applyRoutes(); | ||
|
||
expect(mockApp.get).toHaveBeenCalledWith("/api/test", ...middleware, handler); | ||
}); | ||
}); | ||
}); | ||
|
||
// End of unit tests for: applyRoutes |
Oops, something went wrong.