Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add unit tests for ReviewRequestService #569

Merged
merged 6 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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