Skip to content

Commit

Permalink
[backend] feat: Adding health endpoint (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
helloitsdave authored Feb 11, 2024
1 parent 07896e0 commit 1d4b9c8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const PORT = 5000
app.use(express.json())
app.use(cors())

app.get("/health", (req, res) => {
res.json({ status: "ok"});
});

app.get("/api/notes", async (req, res) => {
try {
const notes = await prisma.note.findMany({ orderBy: { updatedAt: "desc" }});
Expand Down
8 changes: 7 additions & 1 deletion backend/tests/service/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import request from "supertest";

config();

const URL = `${process.env.API_URL}/api/notes`;
const BASE_URL = `${process.env.API_URL}`;
const URL = `${BASE_URL}/api/notes`;

let getNoteResponse: any;
let createdID: number;

test("Health check", async () => {
const response = await request(BASE_URL).get('/health');
expect(response.status).toBe(200);
});

test("Get the list of Notes", async () => {
getNoteResponse = await request(URL).get("/");
expect(getNoteResponse.status).toBe(200);
Expand Down
43 changes: 42 additions & 1 deletion backend/tests/unit/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,64 @@ const seed = [
id: 1,
title: "Meeting Notes",
content: "Discussed project timelines and goals.",
updatedAt: new Date("2024-02-05T23:33:42.252Z"),
createdAt: new Date("2024-02-05T23:43:42.252Z")
},
{
id: 2,
title: "Shopping List",
content: "Milk, eggs, bread, and fruits.",
updatedAt: new Date("2024-02-05T23:33:42.252Z"),
createdAt: new Date("2024-02-05T23:43:42.252Z")
},
{
id: 3,
title: "Recipe",
content: "Ingredients: Chicken, tomatoes, onions, garlic.",
updatedAt: new Date("2024-02-05T23:33:42.252Z"),
createdAt: new Date("2024-02-05T23:43:42.252Z")
},
{
id: 4,
title: "Ideas",
content: "Brainstorming ideas for the next feature release. 🚀",
updatedAt: new Date("2024-02-05T23:33:42.252Z"),
createdAt: new Date("2024-02-05T23:43:42.252Z")
},
{
id: 5,
title: "Personal Goals",
content: "Exercise for 30 minutes daily. Read a book every week.",
updatedAt: new Date("2024-02-05T23:33:42.252Z"),
createdAt: new Date("2024-02-05T23:43:42.252Z")
},
{
id: 6,
title: "Fête d'anniversaire",
content: "Préparer une surprise pour la fête d'anniversaire.",
updatedAt: new Date("2024-02-05T23:33:42.252Z"),
createdAt: new Date("2024-02-05T23:43:42.252Z")
},
{
id: 7,
title: "日本旅行",
content: "計画: 東京、京都、大阪を訪れる。",
updatedAt: new Date("2024-02-05T23:33:42.252Z"),
createdAt: new Date("2024-02-05T23:43:42.252Z")
},
{
id: 8,
title: "Семейный ужин",
content: "Приготовить вкусный ужин для всей семьи.",
updatedAt: new Date("2024-02-05T23:33:42.252Z"),
createdAt: new Date("2024-02-05T23:43:42.252Z")
},
{
id: 9,
title: "Coding Project",
content: "Implement new features using React and Express.",
updatedAt: new Date("2024-02-05T23:33:42.252Z"),
createdAt: new Date("2024-02-05T23:43:42.252Z")
},
];

Expand All @@ -70,6 +88,8 @@ describe("View notes", () => {
id: 1,
title: "Meeting Notes",
content: "Discussed project timelines and goals.",
createdAt: "2024-02-05T23:43:42.252Z",
updatedAt: "2024-02-05T23:33:42.252Z",
},
]);
});
Expand All @@ -78,7 +98,14 @@ describe("View notes", () => {
prisma.note.findMany.mockResolvedValue(seed);
const response = await request(app).get("/api/notes");
expect(response.status).toBe(200);
expect(response.body).toEqual(seed);

const expectedResult = seed.map(item => ({
...item,
createdAt: new Date(item.createdAt).toISOString(),
updatedAt: new Date(item.updatedAt).toISOString()
}));

expect(response.body).toEqual(expectedResult);
});
test("500 error - failure", async ({}) => {
prisma.note.findMany.mockImplementation(() => {
Expand All @@ -95,6 +122,8 @@ describe("Create a note", () => {
content: "Test",
title: "Test",
id: 1,
updatedAt: new Date("2024-02-05T23:33:42.252Z"),
createdAt: new Date("2024-02-05T23:33:42.252Z"),
});
const response = await request(app)
.post("/api/notes")
Expand Down Expand Up @@ -142,6 +171,8 @@ describe("Update a note", () => {
title: "Test update",
content: "Test",
id: 1,
updatedAt: new Date("2024-02-05T23:33:42.252Z"),
createdAt: new Date("2024-02-05T23:33:42.252Z"),
});
const response = await request(app)
.put("/api/notes/1")
Expand All @@ -150,7 +181,9 @@ describe("Update a note", () => {
expect(response.body).toStrictEqual({
title: "Test update",
content: "Test",
createdAt: "2024-02-05T23:33:42.252Z",
id: 1,
updatedAt: "2024-02-05T23:33:42.252Z"
});
});
test("PUT without title - failure", async ({}) => {
Expand Down Expand Up @@ -224,3 +257,11 @@ describe("Delete a note", () => {
expect(response.body).toStrictEqual({ error: "ID field required" });
});
});

describe("Health check", () => {
test("GET /health", async ({}) => {
const response = await request(app).get("/health");
expect(response.status).toBe(200);
expect(response.body).toStrictEqual({ status: "ok" });
});
});

1 comment on commit 1d4b9c8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

100.00%

Coverage Report
FileBranchesFuncsLinesUncovered Lines
src
   index.ts100%100%100%
   prisma.ts100%100%100%
src/__mocks__
   prisma.ts100%100%100%

Please sign in to comment.