-
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.
Merge pull request #16 from Divyateja04/deletepost
deletePost
- Loading branch information
Showing
3 changed files
with
121 additions
and
17 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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { describe, expect } from "@jest/globals"; | ||
import { prismaMock } from "./_mockdb"; | ||
import { createPost, getAllPosts, getMyPosts, getPostDetails, editPost } from "../src/service/posts.service"; | ||
import { Post, User } from ".prisma/client"; | ||
import { createPost, getAllPosts, getMyPosts, getPostDetails, editPost, deletePost } from "../src/service/posts.service"; | ||
import { Post, User, Request } from ".prisma/client"; | ||
|
||
const userWith10KarmaPoints: User = { | ||
id: "1", | ||
|
@@ -34,6 +34,13 @@ const post: Post & { | |
status: "open", | ||
} | ||
|
||
const request: Request = { | ||
id: "1", | ||
postId: "1", | ||
senderEmail: "[email protected]", | ||
status: "open" | ||
} | ||
|
||
describe("Create a new post", () => { | ||
it("should create a new post", () => { | ||
|
||
|
@@ -135,7 +142,7 @@ describe("Get my posts", () => { | |
}) | ||
|
||
describe("Get post details", () => { | ||
it("should get post details", () => { | ||
it("should get post details successfully", () => { | ||
prismaMock.post.findUnique.mockResolvedValue(post); | ||
|
||
expect(getPostDetails( | ||
|
@@ -170,7 +177,7 @@ describe("Get post details", () => { | |
}) | ||
|
||
describe("Update post", () => { | ||
it("should update post", () => { | ||
it("should update the post successfully", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(userWith10KarmaPoints); | ||
prismaMock.post.update.mockResolvedValue(post); | ||
|
||
|
@@ -188,6 +195,23 @@ describe("Update post", () => { | |
}); | ||
}); | ||
|
||
it("should throw an error if user does not exist", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(null); | ||
|
||
expect(editPost({ | ||
id: post.id, | ||
source: post.source, | ||
destination: post.destination, | ||
costInPoints: post.costInPoints, | ||
service: post.service, | ||
status: post.status, | ||
authorEmail: post.authorEmail | ||
})).resolves.toEqual({ | ||
error: true, | ||
data: "User does not exist." | ||
}); | ||
}) | ||
|
||
it("should throw an error if new karma higher than user karma", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(userWith0KarmaPoints); | ||
|
||
|
@@ -201,7 +225,25 @@ describe("Update post", () => { | |
authorEmail: post.authorEmail | ||
})).resolves.toEqual({ | ||
error: true, | ||
data: "Karma points not enough to create a post." | ||
data: "Karma points not enough to edit a post." | ||
}); | ||
|
||
}) | ||
|
||
it("should throw an error if post has already been closed", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(userWith10KarmaPoints); | ||
|
||
expect(editPost({ | ||
id: post.id, | ||
source: post.source, | ||
destination: post.destination, | ||
costInPoints: post.costInPoints, | ||
service: post.service, | ||
status: "closed", | ||
authorEmail: post.authorEmail | ||
})).resolves.toEqual({ | ||
error: true, | ||
data: "Post has already been closed." | ||
}); | ||
|
||
}) | ||
|
@@ -222,4 +264,54 @@ describe("Update post", () => { | |
data: "Some error occurred while updating the post" | ||
}); | ||
}); | ||
}) | ||
|
||
describe("Delete post", () => { | ||
it("should throw an error if user does not exist", () => { | ||
prismaMock.post.findUnique.mockResolvedValue(post); | ||
expect(deletePost(post)).resolves.toEqual({ | ||
error: true, | ||
data: "User does not exist." | ||
}); | ||
}); | ||
|
||
it("should throw an error if post has already been closed", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(userWith10KarmaPoints); | ||
const post: Post & { | ||
authorEmail: string | ||
} = { | ||
id: "1", | ||
authorId: "1", | ||
authorEmail: "[email protected]", | ||
source: "source", | ||
destination: "destination", | ||
costInPoints: 10, | ||
service: "service", | ||
status: "closed", | ||
} | ||
prismaMock.post.findUnique.mockResolvedValue(post); | ||
expect(deletePost(post)).resolves.toEqual({ | ||
error: true, | ||
data: "Post has already been closed." | ||
}); | ||
}); | ||
|
||
it("should catch any error occurred", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(userWith10KarmaPoints); | ||
prismaMock.post.findUnique.mockRejectedValue(new Error("Some Error ocurred")); | ||
expect(deletePost(post)).resolves.toEqual({ | ||
error: true, | ||
data: "Some error occurred while deleting the post" | ||
}); | ||
}); | ||
|
||
it("should delete the post successfully", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(userWith10KarmaPoints); | ||
prismaMock.$transaction.mockResolvedValue([[request], post]); | ||
|
||
expect(deletePost(post)).resolves.toEqual({ | ||
error: false, | ||
data: post | ||
}); | ||
}); | ||
}) |
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