-
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
Pacific NDAHIRO
authored and
Pacific NDAHIRO
committed
Mar 1, 2024
1 parent
6d20f60
commit 56ddb9b
Showing
13 changed files
with
5,078 additions
and
791 deletions.
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,8 @@ | ||
{ | ||
"preset": "ts-jest", | ||
"testEnvironment": "node", | ||
"testPathIgnorePatterns": ["/node_modules/", "/dist/"], | ||
"transform": { | ||
"^.+\\.(ts|tsx)?$": "ts-jest" | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Binary file not shown.
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
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,107 @@ | ||
import request from "supertest"; | ||
import app from "../index"; | ||
import { token } from "./setup"; | ||
|
||
var articleId: string; | ||
|
||
const article = { | ||
title: "Hello World", | ||
content: "This is a test article", | ||
}; | ||
|
||
const updatedArticle = { | ||
title: "World Hello", | ||
content: "This is an updated test article", | ||
}; | ||
|
||
describe("POST /api/articles", () => { | ||
it("should create a new article", async () => { | ||
const res = await request(app) | ||
.post("/api/articles") | ||
.set("isAuthenticated", token) | ||
.send(article); | ||
|
||
articleId = res.body.article._id; | ||
|
||
expect(res.status).toEqual(201); | ||
expect(res.body.status).toEqual("success"); | ||
expect(res.body.article).toBeInstanceOf(Object); | ||
expect(res.body.article.title).toEqual(article.title); | ||
expect(res.body.article.content).toEqual(article.content); | ||
}); | ||
}); | ||
|
||
describe("GET /api/articles", () => { | ||
it("should list all articles", async () => { | ||
const res = await request(app).get("/api/articles"); | ||
|
||
expect(res.status).toEqual(200); | ||
expect(res.body.status).toEqual("success"); | ||
expect(res.body.articles).toBeInstanceOf(Array); | ||
expect(res.body.articles.length).toBeGreaterThan(0); | ||
}); | ||
}); | ||
|
||
describe("GET /api/articles/:id", () => { | ||
it("should return a single article", async () => { | ||
const res = await request(app).get(`/api/articles/${articleId}`); | ||
|
||
expect(res.status).toEqual(200); | ||
expect(res.body.status).toEqual("success"); | ||
expect(res.body.article).toBeInstanceOf(Object); | ||
expect(res.body.article._id).toEqual(articleId); | ||
}); | ||
}); | ||
|
||
describe("GET /api/articles/:id/like", () => { | ||
it("should like an article", async () => { | ||
const res = await request(app) | ||
.get(`/api/articles/${articleId}/like`) | ||
.set("isAuthenticated", token); | ||
|
||
expect(res.status).toEqual(200); | ||
expect(res.body.status).toEqual("success"); | ||
expect(res.body.article).toBeInstanceOf(Object); | ||
expect(res.body.article._id).toEqual(articleId); | ||
expect(res.body.article.likes).toBeGreaterThan(0); | ||
}); | ||
}); | ||
|
||
describe("GET /api/articles/:id/unlike", () => { | ||
it("should unlike an article", async () => { | ||
const res = await request(app) | ||
.get(`/api/articles/${articleId}/unlike`) | ||
.set("isAuthenticated", token); | ||
|
||
expect(res.status).toEqual(200); | ||
expect(res.body.status).toEqual("success"); | ||
expect(res.body.article).toBeInstanceOf(Object); | ||
expect(res.body.article._id).toEqual(articleId); | ||
expect(res.body.article.likes).toEqual(0); | ||
}); | ||
}); | ||
|
||
describe("PUT /api/articles/:id", () => { | ||
it("should update an article", async () => { | ||
const res = await request(app) | ||
.put(`/api/articles/${articleId}`) | ||
.set("isAuthenticated", token) | ||
.send(updatedArticle); | ||
|
||
expect(res.status).toEqual(200); | ||
expect(res.body.status).toEqual("success"); | ||
expect(res.body.article).toBeInstanceOf(Object); | ||
expect(res.body.article.title).toEqual(updatedArticle.title); | ||
expect(res.body.article.content).toEqual(updatedArticle.content); | ||
}); | ||
}); | ||
|
||
describe("DELETE /api/articles/:id", () => { | ||
it("should delete an article", async () => { | ||
const res = await request(app) | ||
.delete(`/api/articles/${articleId}`) | ||
.set("isAuthenticated", token); | ||
|
||
expect(res.status).toEqual(204); | ||
}); | ||
}); |
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,92 @@ | ||
import request from "supertest"; | ||
import app from "../index"; | ||
import { token } from "./setup"; | ||
|
||
let articleId: string; | ||
let commentId: string; | ||
|
||
const article = { | ||
title: "Hello World", | ||
content: "This is a test article", | ||
}; | ||
|
||
const comment = { | ||
content: "This is a test comment", | ||
}; | ||
|
||
const updatedComment = { | ||
content: "This is an updated test comment", | ||
}; | ||
|
||
describe("POST /api/articles/:id/comments", () => { | ||
it("should create a new comment for an article", async () => { | ||
const articleResponse = await request(app) | ||
.post("/api/articles") | ||
.set("isAuthenticated", token) | ||
.send(article); | ||
|
||
articleId = articleResponse.body.article._id; | ||
|
||
const commentResponse = await request(app) | ||
.post(`/api/articles/${articleId}/comments`) | ||
.set("isAuthenticated", token) | ||
.send(comment); | ||
|
||
commentId = commentResponse.body.comment._id; | ||
|
||
expect(commentResponse.status).toEqual(201); | ||
expect(commentResponse.body.status).toEqual("success"); | ||
expect(commentResponse.body.comment).toBeInstanceOf(Object); | ||
expect(commentResponse.body.comment.content).toEqual(comment.content); | ||
}); | ||
}); | ||
|
||
describe("GET /api/articles/:id/comments", () => { | ||
it("should get all comments for an article", async () => { | ||
const response = await request(app) | ||
.get(`/api/articles/${articleId}/comments`) | ||
.set("isAuthenticated", token); | ||
|
||
expect(response.status).toEqual(200); | ||
expect(response.body.status).toEqual("success"); | ||
expect(response.body.comments).toBeInstanceOf(Array); | ||
expect(response.body.comments.length).toBeGreaterThan(0); | ||
}); | ||
}); | ||
|
||
describe("GET /api/articles/:id/comments/:id", () => { | ||
it("should get a single comment for an article", async () => { | ||
const response = await request(app) | ||
.get(`/api/articles/${articleId}/comments/${commentId}`) | ||
.set("isAuthenticated", token); | ||
|
||
expect(response.status).toEqual(200); | ||
expect(response.body.status).toEqual("success"); | ||
expect(response.body.comment).toBeInstanceOf(Object); | ||
expect(response.body.comment.content).toEqual(comment.content); | ||
}); | ||
}); | ||
|
||
describe("PUT /api/articles/:id/comments/:id", () => { | ||
it("should update a single comment for an article", async () => { | ||
const response = await request(app) | ||
.put(`/api/articles/${articleId}/comments/${commentId}`) | ||
.set("isAuthenticated", token) | ||
.send(updatedComment); | ||
|
||
expect(response.status).toEqual(200); | ||
expect(response.body.status).toEqual("success"); | ||
expect(response.body.comment).toBeInstanceOf(Object); | ||
expect(response.body.comment.content).toEqual(updatedComment.content); | ||
}); | ||
}); | ||
|
||
describe("DELETE /api/articles/:id/comments/:id", () => { | ||
it("should delete a single comment for an article", async () => { | ||
const response = await request(app) | ||
.delete(`/api/articles/${articleId}/comments/${commentId}`) | ||
.set("isAuthenticated", token); | ||
|
||
expect(response.status).toEqual(204); | ||
}); | ||
}); |
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,25 @@ | ||
import mongoose from "mongoose"; | ||
import request from "supertest"; | ||
import app from "../index"; | ||
|
||
var token: string; | ||
|
||
const user = { | ||
name: "Test User", | ||
email: Math.random() + "@test.com", | ||
password: "password", | ||
}; | ||
|
||
beforeEach(async () => { | ||
jest.spyOn(console, "log").mockImplementation(() => {}); | ||
await request(app).post("/api/user/register").send(user); | ||
const res = await request(app).post("/api/user/login").send(user); | ||
token = res.body.token; | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.connection.db?.dropDatabase(); | ||
await mongoose.disconnect(); | ||
}); | ||
|
||
export { token, user }; |
Oops, something went wrong.