Skip to content

Commit

Permalink
tests: add unit tests for ReviewRequestService (#569)
Browse files Browse the repository at this point in the history
* chore: remove TODO comment as it is already done

* fix: should get the count of unread comments instead

* tests: add ReviewRequestService unit tests

* chore: move tests back to identity services

* chore: remove unused variable

* chore: adjust naming of test to be clearer on the situation
  • Loading branch information
dcshzj authored and alexanderleegs committed Mar 16, 2023
1 parent b0076b0 commit 77d780d
Show file tree
Hide file tree
Showing 7 changed files with 1,285 additions and 15 deletions.
59 changes: 47 additions & 12 deletions src/fixtures/identity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Attributes } from "sequelize/types"

import { User, SiteMember } from "@database/models"
import { Author } from "@root/types/github"

import { mockIsomerUserId } from "./sessionData"

Expand All @@ -18,18 +19,24 @@ export const mockHeaders = {
}
export const mockSiteName = "hello world"
export const mockUserId = "some user id"
export const mockSiteId = "16"

export const mockBearerTokenHeaders = {
headers: {
Authorization: `Bearer ${process.env.POSTMAN_API_KEY}`,
},
}

const mockCollaboratorContributor1: Attributes<User> & {
export const MOCK_IDENTITY_EMAIL_ONE = "[email protected]"
export const MOCK_IDENTITY_EMAIL_TWO = "[email protected]"
export const MOCK_IDENTITY_EMAIL_THREE = "[email protected]"
export const MOCK_IDENTITY_EMAIL_FOUR = "[email protected]"

export const mockCollaboratorContributor1: Attributes<User> & {
SiteMember: Attributes<SiteMember>
} = {
id: 1,
email: "[email protected]",
email: MOCK_IDENTITY_EMAIL_ONE,
githubId: "test1",
contactNumber: "12331231",
lastLoggedIn: new Date("2022-07-30T07:41:09.661Z"),
Expand All @@ -38,19 +45,19 @@ const mockCollaboratorContributor1: Attributes<User> & {
deletedAt: undefined,
SiteMember: {
userId: 1,
siteId: "16",
siteId: mockSiteId,
role: "CONTRIBUTOR",
createdAt: new Date("2022-07-29T03:50:49.145Z"),
updatedAt: new Date("2022-07-29T03:50:49.145Z"),
},
sites: [],
}

const mockCollaboratorAdmin1: Attributes<User> & {
export const mockCollaboratorAdmin1: Attributes<User> & {
SiteMember: Attributes<SiteMember>
} = {
id: 2,
email: "[email protected]",
email: MOCK_IDENTITY_EMAIL_TWO,
githubId: "test2",
contactNumber: "12331232",
lastLoggedIn: new Date("2022-07-30T07:41:09.661Z"),
Expand All @@ -59,18 +66,18 @@ const mockCollaboratorAdmin1: Attributes<User> & {
deletedAt: undefined,
SiteMember: {
userId: 2,
siteId: "16",
siteId: mockSiteId,
role: "ADMIN",
createdAt: new Date("2022-07-29T03:50:49.145Z"),
updatedAt: new Date("2022-07-29T03:50:49.145Z"),
},
sites: [],
}
const mockCollaboratorAdmin2: Attributes<User> & {
export const mockCollaboratorAdmin2: Attributes<User> & {
SiteMember: Attributes<SiteMember>
} = {
id: 3,
email: "[email protected]",
email: MOCK_IDENTITY_EMAIL_THREE,
githubId: "test3",
contactNumber: "12331233",
lastLoggedIn: new Date("2022-06-30T07:41:09.661Z"),
Expand All @@ -79,18 +86,18 @@ const mockCollaboratorAdmin2: Attributes<User> & {
deletedAt: undefined,
SiteMember: {
userId: 3,
siteId: "16",
siteId: mockSiteId,
role: "ADMIN",
createdAt: new Date("2022-07-29T03:50:49.145Z"),
updatedAt: new Date("2022-07-29T03:50:49.145Z"),
},
sites: [],
}
const mockCollaboratorContributor2: Attributes<User> & {
export const mockCollaboratorContributor2: Attributes<User> & {
SiteMember: Attributes<SiteMember>
} = {
id: 4,
email: "[email protected]",
email: MOCK_IDENTITY_EMAIL_FOUR,
githubId: "test4",
contactNumber: "12331234",
lastLoggedIn: new Date("2022-07-30T07:41:09.661Z"),
Expand All @@ -99,7 +106,7 @@ const mockCollaboratorContributor2: Attributes<User> & {
deletedAt: undefined,
SiteMember: {
userId: 4,
siteId: "16",
siteId: mockSiteId,
role: "CONTRIBUTOR",
createdAt: new Date("2022-07-29T03:50:49.145Z"),
updatedAt: new Date("2022-07-29T03:50:49.145Z"),
Expand Down Expand Up @@ -143,6 +150,7 @@ export const mockSiteOrmResponseWithNoCollaborators = {

export const MOCK_COMMIT_MESSAGE_ONE = "Update file: Example.md"
export const MOCK_COMMIT_FILENAME_ONE = "Example.md"
export const MOCK_COMMIT_FILEPATH_ONE = "test/path/one/"
export const MOCK_GITHUB_NAME_ONE = "testuser"
export const MOCK_GITHUB_EMAIL_ADDRESS_ONE = "[email protected]"
export const MOCK_GITHUB_DATE_ONE = "2022-09-22T04:07:53Z"
Expand All @@ -154,6 +162,7 @@ export const MOCK_COMMIT_MESSAGE_OBJECT_ONE = {

export const MOCK_COMMIT_MESSAGE_TWO = "Update file: Test.md"
export const MOCK_COMMIT_FILENAME_TWO = "Test.md"
export const MOCK_COMMIT_FILEPATH_TWO = "test/path/two/"
export const MOCK_GITHUB_NAME_TWO = "testuser2"
export const MOCK_GITHUB_EMAIL_ADDRESS_TWO = "[email protected]"
export const MOCK_GITHUB_DATE_TWO = "2022-09-28T06:25:14Z"
Expand All @@ -163,4 +172,30 @@ export const MOCK_COMMIT_MESSAGE_OBJECT_TWO = {
userId: mockIsomerUserId,
}

export const MOCK_GITHUB_COMMIT_AUTHOR_ONE: Author = {
name: MOCK_GITHUB_NAME_ONE,
email: MOCK_GITHUB_EMAIL_ADDRESS_ONE,
date: MOCK_GITHUB_DATE_ONE,
}

export const MOCK_GITHUB_COMMIT_AUTHOR_TWO: Author = {
name: MOCK_GITHUB_NAME_TWO,
email: MOCK_GITHUB_EMAIL_ADDRESS_TWO,
date: MOCK_GITHUB_DATE_TWO,
}

export const MOCK_GITHUB_COMMENT_ONE = "test comment 1"
export const MOCK_GITHUB_COMMENT_DATA_ONE = {
userId: mockIsomerUserId,
message: MOCK_GITHUB_COMMENT_ONE,
createdAt: MOCK_GITHUB_DATE_ONE,
}

export const MOCK_GITHUB_COMMENT_TWO = "test comment 2"
export const MOCK_GITHUB_COMMENT_DATA_TWO = {
userId: mockIsomerUserId,
message: MOCK_GITHUB_COMMENT_TWO,
createdAt: MOCK_GITHUB_DATE_TWO,
}

export const MOCK_COMMON_ACCESS_TOKEN_GITHUB_NAME = "isomergithub1"
85 changes: 85 additions & 0 deletions src/fixtures/review.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Attributes } from "sequelize/types"

import { ReviewRequestStatus } from "@root/constants"
import { ReviewRequest, ReviewRequestView } from "@root/database/models"
import { Commit } from "@root/types/github"

import {
mockCollaboratorAdmin1,
mockCollaboratorAdmin2,
mockCollaboratorContributor1,
MOCK_COMMIT_FILEPATH_TWO,
MOCK_COMMIT_MESSAGE_OBJECT_ONE,
MOCK_COMMIT_MESSAGE_OBJECT_TWO,
MOCK_GITHUB_COMMIT_AUTHOR_ONE,
MOCK_GITHUB_COMMIT_AUTHOR_TWO,
MOCK_GITHUB_DATE_ONE,
} from "./identity"

export const MOCK_PULL_REQUEST_FILE_FILENAME_ONE = "file1.txt"
export const MOCK_PULL_REQUEST_FILE_CONTENTSURL_ONE =
"https://api.github.com/repos/octocat/Hello-World/contents/file1.txt?ref=6dcb09b5b57875f334f61aebed695e2e4193db5e"
export const MOCK_PULL_REQUEST_FILE_FILENAME_TWO = "file2.txt"
export const MOCK_PULL_REQUEST_FILE_CONTENTSURL_TWO =
"https://api.github.com/repos/octocat/Hello-World/contents/file2.txt?ref=bbcd538c8e72b8c175046e27cc8f907076331401"

export const MOCK_PULL_REQUEST_FILECHANGEINFO_ONE = {
filename: MOCK_PULL_REQUEST_FILE_FILENAME_ONE,
contents_url: MOCK_PULL_REQUEST_FILE_CONTENTSURL_ONE,
}
export const MOCK_PULL_REQUEST_FILECHANGEINFO_TWO = {
filename: `${MOCK_COMMIT_FILEPATH_TWO}/${MOCK_PULL_REQUEST_FILE_FILENAME_TWO}`,
contents_url: MOCK_PULL_REQUEST_FILE_CONTENTSURL_TWO,
}

export const MOCK_PULL_REQUEST_COMMIT_SHA_ONE =
"6dcb09b5b57875f334f61aebed695e2e4193db5e"
export const MOCK_PULL_REQUEST_COMMIT_SHA_TWO =
"bbcd538c8e72b8c175046e27cc8f907076331401"

export const MOCK_PULL_REQUEST_COMMIT_ONE: Commit = {
sha: MOCK_PULL_REQUEST_COMMIT_SHA_ONE,
url: "fakeUrl",
commit: {
url: "fakeUrl",
author: MOCK_GITHUB_COMMIT_AUTHOR_ONE,
message: JSON.stringify(MOCK_COMMIT_MESSAGE_OBJECT_ONE),
},
}
export const MOCK_PULL_REQUEST_COMMIT_TWO: Commit = {
sha: MOCK_PULL_REQUEST_COMMIT_SHA_TWO,
url: "fakeUrl",
commit: {
url: "fakeUrl",
author: MOCK_GITHUB_COMMIT_AUTHOR_TWO,
message: JSON.stringify(MOCK_COMMIT_MESSAGE_OBJECT_TWO),
},
}

export const MOCK_PULL_REQUEST_ONE = {
title: "Pull Request 1",
body: "Pull Request 1 Description",
changed_files: 3,
created_at: MOCK_GITHUB_DATE_ONE,
}

export const MOCK_REVIEW_REQUEST_ONE: Attributes<ReviewRequest> = {
id: 1,
site: {
name: "Test Site 1",
},
requestor: mockCollaboratorContributor1,
reviewMeta: {
id: 1,
pullRequestNumber: 1,
reviewLink: "fakeUrl",
},
reviewStatus: ReviewRequestStatus.Open,
reviewers: [mockCollaboratorAdmin1, mockCollaboratorAdmin2],
}

export const MOCK_REVIEW_REQUEST_VIEW_ONE: Attributes<ReviewRequestView> = {
id: 1,
reviewRequestId: 1,
lastViewedAt: new Date("2022-09-23T00:00:00Z"),
}
4 changes: 2 additions & 2 deletions src/services/review/ReviewRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ export default class ReviewRequestService {
const countNewComments = await Promise.all(
allComments.map(async (value) => value.isRead)
).then((arr) => {
const readComments = arr.filter((isRead) => !!isRead)
return readComments.length
const unreadComments = arr.filter((isRead) => !isRead)
return unreadComments.length
})

return {
Expand Down
Loading

0 comments on commit 77d780d

Please sign in to comment.