-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
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,53 @@ | ||
import { type Request, type Response } from "express"; | ||
import Post from "../../../src/models/post.model"; | ||
import { getPosts } from "../../../src/controllers/posts"; | ||
|
||
// Mocking the Post model | ||
jest.mock("../../../src/models/post.model", () => ({ | ||
find: jest.fn().mockReturnThis(), | ||
exec: jest.fn(), | ||
})); | ||
|
||
describe("post controller", () => { | ||
describe("getPosts", () => { | ||
let req: Request; | ||
let res: Response; | ||
|
||
beforeEach(() => { | ||
res = { | ||
send: jest.fn(), | ||
sendStatus: jest.fn(), | ||
} as unknown as Response; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should retrieve and send posts", async () => { | ||
const posts = [{ title: "Post 1" }, { title: "Post 2" }]; | ||
|
||
// Mocking the Post.find().exec() method to return posts | ||
(Post.find().exec as jest.Mock).mockResolvedValue(posts); | ||
|
||
await getPosts(req, res); | ||
|
||
expect(res.send).toHaveBeenCalledWith(posts); | ||
expect(res.sendStatus).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test("should send 500 status on error", async () => { | ||
const errorMessage = "Internal server error"; | ||
|
||
// Mocking the Post.find().exec() method to throw an error | ||
(Post.find().exec as jest.Mock).mockRejectedValue( | ||
new Error(errorMessage) | ||
); | ||
|
||
await getPosts(req, res); | ||
|
||
expect(res.sendStatus).toHaveBeenCalledWith(500); | ||
expect(res.send).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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
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,15 @@ | ||
import { type Request, type Response } from "express"; | ||
import Post from "../../models/post.model"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
const getPosts = async (_req: Request, res: Response) => { | ||
try { | ||
const query = Post.find({}); | ||
const posts = await query.exec(); | ||
res.send(posts); | ||
} catch (error) { | ||
res.sendStatus(500); | ||
} | ||
}; | ||
|
||
export default getPosts; |
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,3 @@ | ||
import getPosts from "./getPosts.controller"; | ||
|
||
export { getPosts }; |