Skip to content

Commit

Permalink
tests: add tests for update post
Browse files Browse the repository at this point in the history
  • Loading branch information
thenicekat committed Mar 26, 2024
1 parent 57d8cab commit 6423a45
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 19 deletions.
35 changes: 17 additions & 18 deletions backend/src/service/posts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,15 @@ export const createPost = async (post: {
}


export const editPost = async(post: {
requestId: string,
export const editPost = async (post: {
id: string,
authorEmail: string,
source: string,
destination: string
costInPoints: number,
status: string,
service: string
}): Promise<CustomReturn<Post>> => {

if (!post.authorEmail) return {
error: true,
data: "Author email is required."
Expand All @@ -181,14 +180,14 @@ export const editPost = async(post: {
data: "Karma points not enough to create a post."
}

if (post.status=="closed")
return{
error: true,
data: "Post has already been closed."
}
if (post.status == "closed")
return {
error: true,
data: "Post has already been closed."
}

let editPost= await prisma.post.update({
where: { id: post.requestId },
let editPost = await prisma.post.update({
where: { id: post.id },
data: {
source: post.source,
destination: post.destination,
Expand All @@ -208,12 +207,12 @@ export const editPost = async(post: {
}));
return {
error: true,
data: "Some error occurred while creating the post"
data: "Some error occurred while updating the post"
}
}
}

export const deletePost = async(post: {
export const deletePost = async (post: {
requestId: string,
authorEmail: string,
status: string,
Expand All @@ -236,13 +235,13 @@ export const deletePost = async(post: {
data: "User does not exist."
}

if (post.status=="closed")
return{
error: true,
data: "Post has already been closed."
}
if (post.status == "closed")
return {
error: true,
data: "Post has already been closed."
}

let deletePost= await prisma.post.delete({
let deletePost = await prisma.post.delete({
where: { id: post.requestId },
})

Expand Down
57 changes: 56 additions & 1 deletion backend/tests/posts.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect } from "@jest/globals";
import { prismaMock } from "./_mockdb";
import { createPost, getAllPosts, getMyPosts, getPostDetails } from "../src/service/posts.service";
import { createPost, getAllPosts, getMyPosts, getPostDetails, editPost } from "../src/service/posts.service";
import { Post, User } from ".prisma/client";

const userWith10KarmaPoints: User = {
Expand Down Expand Up @@ -168,3 +168,58 @@ describe("Get post details", () => {
});
});
})

describe("Update post", () => {
it("should update post", () => {
prismaMock.user.findUnique.mockResolvedValue(userWith10KarmaPoints);
prismaMock.post.update.mockResolvedValue(post);

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: false,
data: post
});
});

it("should throw an error if new karma higher than user karma", () => {
prismaMock.user.findUnique.mockResolvedValue(userWith0KarmaPoints);

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: "Karma points not enough to create a post."
});

})

it("should catch any error occurred", () => {
prismaMock.user.findUnique.mockRejectedValue(new Error("Some error occurred"));

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: "Some error occurred while updating the post"
});
});
})

0 comments on commit 6423a45

Please sign in to comment.