Skip to content

Commit

Permalink
Add many tests and some adjustments (#54)
Browse files Browse the repository at this point in the history
- Add many test especially ones about the validators
- Improve github workflow
- Fix some code source
  • Loading branch information
MaximeDan authored Aug 28, 2024
1 parent bc9ece0 commit 6b2ac9f
Show file tree
Hide file tree
Showing 29 changed files with 879 additions and 189 deletions.
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ DATABASE_URL=postgresql://testuser:testpassword@localhost:5433/testdb?schema=pub
NEXTAUTH_SECRET=secret
NODE_ENV="test"
DATABASE_PASSWORD="test"
NEXTAUTH_URL=http://localhost:3000
11 changes: 7 additions & 4 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ permissions:
checks: write

jobs:
build:
RunTests:
name: Run tests
runs-on: ubuntu-latest
steps:
- name: Checkout repository
Expand Down Expand Up @@ -52,8 +53,9 @@ jobs:
Pull request: ${{ github.event.pull_request.html_url }}
accessibility:
name: Accessibility
runs-on: ubuntu-latest
needs: build
needs: RunTests
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -75,7 +77,7 @@ jobs:
run: |
docker-compose -f docker-compose.dev.yml up -d
until docker exec -i <your_database_container_name> pg_isready -U <your_database_user>; do
until docker exec -i patrigma_db pg_isready -U postgres; do
echo "Waiting for the database to be ready..."
sleep 2
done
Expand Down Expand Up @@ -135,8 +137,9 @@ jobs:
fi
test-report:
name: publish test report
runs-on: ubuntu-latest
needs: build
needs: RunTests
if: always()
steps:
- name: Checkout repository
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/push-build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Check build when pushing
run-name: ${{ github.actor }} is pushing
on: [push]
on:
push:
branches-ignore:
- main
jobs:
build:
runs-on: ubuntu-latest
Expand Down
155 changes: 155 additions & 0 deletions __test__/comments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
* @jest-environment node
*/

import { testApiHandler } from "next-test-api-route-handler";
import * as postCommentHandler from "@/app/api/comments/route";
import * as commentByIdHandler from "@/app/api/comments/[id]/route";

let commentId: number;
describe("Comment API", () => {
describe("POST /api/comments", () => {
it("should create a new comment successfully", async () => {
await testApiHandler({
appHandler: postCommentHandler,
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
comment: {
content: "This is a test comment from test",
authorId: 1,
journeyId: 5,
rating: 5,
},
}),
});

const data = await res.json();
commentId = data.data.id;

expect(res.status).toBe(201);
expect(data).toEqual(
expect.objectContaining({
data: expect.objectContaining({
content: "This is a test comment from test",
authorId: 1,
journeyId: 5,
rating: 5,
}),
}),
);
},
});
});

it("should return an error when required fields are missing", async () => {
await testApiHandler({
appHandler: postCommentHandler,
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
comment: {
content: "Incomplete comment",
},
}),
});

expect(res.status).toBe(400);
const data = await res.json();
expect(data).toEqual(
expect.objectContaining({
message: expect.any(String),
}),
);
},
});
});
});

describe("GET /api/comments/[id]", () => {
it("should retrieve a comment by id", async () => {
await testApiHandler({
appHandler: commentByIdHandler,
params: { id: commentId.toString() },
test: async ({ fetch }) => {
const res = await fetch({ method: "GET" });
const data = await res.json();

expect(res.status).toBe(200);
expect(data).toEqual(
expect.objectContaining({
data: expect.objectContaining({
id: commentId,
authorId: 1,
content: "This is a test comment from test",
journeyId: 5,
rating: 5,
createdAt: expect.any(String),
updatedAt: expect.any(String),
}),
}),
);
},
});
});
});

describe("PUT /api/comments/[id]", () => {
it("should update a comment successfully", async () => {
await testApiHandler({
appHandler: commentByIdHandler,
paramsPatcher: (params) => {
params.id = commentId.toString();
},
test: async ({ fetch }) => {
const res = await fetch({
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
comment: {
content: "Updated test comment content",
authorId: 1,
journeyId: 5,
rating: 4,
},
}),
});

const data = await res.json();

expect(res.status).toBe(200);
expect(data).toEqual(
expect.objectContaining({
data: expect.objectContaining({
content: "Updated test comment content",
authorId: 1,
journeyId: 5,
rating: 4,
}),
}),
);
},
});
});
});

describe("DELETE /api/comments/[id]", () => {
it("should delete a comment successfully", async () => {
await testApiHandler({
appHandler: commentByIdHandler,
paramsPatcher: (params) => {
params.id = commentId.toString();
},
test: async ({ fetch }) => {
const res = await fetch({ method: "DELETE" });

expect(res.status).toBe(204);
},
});
});
});
});
5 changes: 0 additions & 5 deletions __test__/journey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ it("Create a journey successfully", async () => {
pictureHint: "https://test.com/image1.jpg",
latitude: 45.7578137,
longitude: 4.8320114,
// address: "1 Place Bellecour",
// city: "Lyon",
// postalCode: "69002",
// country: "France",
stepNumber: 1,
},
{
Expand Down Expand Up @@ -188,7 +184,6 @@ it("Update a journey with missing arguments", async () => {
body: JSON.stringify({
journey: {
authorId: 1,
// title: "Mon premier parcours (modifié)",
description: "Ceci est un exemple de parcours (modifié)",
requirement: "Aucun",
treasure: "Un trésor caché (modifié)",
Expand Down
Loading

0 comments on commit 6b2ac9f

Please sign in to comment.