Skip to content

Commit

Permalink
test(githubService): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kishore03109 committed Dec 6, 2023
1 parent 3ac9df0 commit 437d36c
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 24 deletions.
19 changes: 19 additions & 0 deletions src/fixtures/sessionData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import GithubSessionData from "@root/classes/GithubSessionData"
import UserSessionData from "@root/classes/UserSessionData"
import UserWithSiteSessionData from "@root/classes/UserWithSiteSessionData"
import { FeatureFlags } from "@root/types/featureFlags"
import { RawGitTreeEntry } from "@root/types/github"

import {
MOCK_USER_EMAIL_ONE,
Expand All @@ -25,6 +26,24 @@ export const mockCurrentCommitSha = "mockCurrentCommitSha"
export const mockSiteName = "mockSiteName"
export const mockGrowthBook = new GrowthBook<FeatureFlags>()

export const gitTree: RawGitTreeEntry[] = [
{
path: "directory/file1.txt",
type: "tree",
sha: "fake-sha-1",
mode: "100644",
url: "fake-url-1",
},
{
path: "directory/file2.txt",
type: "file",
sha: "fake-sha-2",
mode: "100644",
url: "fake-url-2",
size: 100,
},
]

export const mockGithubState = {
treeSha: mockTreeSha,
currentCommitSha: mockCurrentCommitSha,
Expand Down
25 changes: 7 additions & 18 deletions src/services/db/GitHubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,14 @@ export default class GitHubService {

async readDirectory(
sessionData: UserWithSiteSessionData,
{
directoryName,
branchName = STAGING_BRANCH,
}: { directoryName: any; branchName?: string }
{ directoryName }: { directoryName: any }
) {
const { accessToken } = sessionData
const { siteName } = sessionData
const endpoint = this.getFolderPath({ siteName, directoryName })

const params = {
ref: branchName,
ref: STAGING_BRANCH,
}

const resp = await this.axiosInstance.get(endpoint, {
Expand All @@ -300,13 +297,11 @@ export default class GitHubService {
sha,
fileName,
directoryName,
branchName,
}: {
fileContent: string
sha: string
fileName: string
directoryName: string | undefined
branchName: string
}
): Promise<GitCommitResult> {
const { accessToken, siteName, isomerUserId: userId } = sessionData
Expand Down Expand Up @@ -335,7 +330,7 @@ export default class GitHubService {
const params = {
message,
content: encodedNewContent,
branch: branchName,
branch: STAGING_BRANCH,
sha: fileSha,
}

Expand Down Expand Up @@ -690,17 +685,11 @@ export default class GitHubService {
githubSessionData: GithubSessionData,
oldPath: string,
newPath: string,
message?: string,
isStaging = true
message?: string
): Promise<GitCommitResult> {
const gitTree = await this.getTree(
sessionData,
githubSessionData,
{
isRecursive: true,
},
!!isStaging
)
const gitTree = await this.getTree(sessionData, githubSessionData, {
isRecursive: true,
})
const newGitTree: any[] = []
const isMovingDirectory =
gitTree.find((item: any) => item.path === oldPath)?.type === "tree" ||
Expand Down
3 changes: 1 addition & 2 deletions src/services/db/RepoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ export default class RepoService extends GitHubService {
sha,
fileName,
directoryName,
branchName: STAGING_BRANCH,
})
}

Expand Down Expand Up @@ -612,7 +611,7 @@ export default class RepoService extends GitHubService {
{
commitSha,
branchName = BRANCH_REF,
}: { commitSha: string; branchName?: string }
}: { commitSha: string; branchName: string }
): Promise<void> {
const { siteName } = sessionData
if (
Expand Down
157 changes: 153 additions & 4 deletions src/services/db/__tests__/GitHubService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ import {
mockCurrentCommitSha,
mockGithubSessionData,
mockIsomerUserId,
gitTree,
} from "@fixtures/sessionData"
import { STAGING_BRANCH, STAGING_LITE_BRANCH } from "@root/constants"
import { indexHtmlContent } from "@root/fixtures/markdown-fixtures"
import { collectionYmlContent } from "@root/fixtures/yaml-fixtures"
import { RawGitTreeEntry } from "@root/types/github"
import GitHubService from "@services/db/GitHubService"

// using es6 gives some error
Expand Down Expand Up @@ -531,7 +534,6 @@ describe("Github Service", () => {
directoryName,
fileContent: content,
sha,
branchName: BRANCH_REF,
})
).resolves.toMatchObject({
newSha: sha,
Expand Down Expand Up @@ -560,7 +562,6 @@ describe("Github Service", () => {
directoryName,
fileContent: content,
sha,
branchName: BRANCH_REF,
})
).rejects.toThrowError(NotFoundError)
expect(mockAxiosInstance.put).toHaveBeenCalledWith(
Expand Down Expand Up @@ -596,7 +597,6 @@ describe("Github Service", () => {
directoryName,
fileContent: content,
sha: "",
branchName: BRANCH_REF,
})
).resolves.toMatchObject({
newSha: sha,
Expand Down Expand Up @@ -628,7 +628,6 @@ describe("Github Service", () => {
directoryName,
fileContent: content,
sha: "",
branchName: BRANCH_REF,
})
).rejects.toThrowError(NotFoundError)
expect(mockAxiosInstance.get).toHaveBeenCalledWith(endpoint, {
Expand Down Expand Up @@ -985,4 +984,154 @@ describe("Github Service", () => {
expect(resp.isOk()).toEqual(true)
})
})

describe("deleteDirectory", () => {
const message = JSON.stringify({
message: `Delete directory: ${directoryName}`,
directoryName,
userId,
})
const params = {
message,
branch: BRANCH_REF,
sha: treeSha,
force: true,
}

const endpoint = `${siteName}/git/refs/heads/${STAGING_BRANCH}`
const stagingLiteEndpoint = `${siteName}/git/refs/heads/${STAGING_LITE_BRANCH}`

it("should delete a directory correctly", async () => {
// Arrange
const getTreeSpy = jest.spyOn(service, "getTree")
getTreeSpy.mockResolvedValueOnce(gitTree)
mockAxiosInstance.get.mockImplementation(() =>
Promise.resolve({ data: gitTree })
)
const updateTreeSpy = jest.spyOn(service, "updateTree")
updateTreeSpy.mockResolvedValueOnce(treeSha)
// Act
await service.deleteDirectory(sessionData, {
directoryName,
message,
githubSessionData: mockGithubSessionData,
})

// Assert
expect(mockAxiosInstance.patch).toHaveBeenCalledWith(
endpoint,
{
force: true,
sha: treeSha,
},
{
headers: { Authorization: `token ${accessToken}` },
}
)
})

it("should throw the correct error if directory cannot be found", async () => {
// Arrange
const getTreeSpy = jest.spyOn(service, "getTree")
getTreeSpy.mockResolvedValueOnce(gitTree)
const updateTreeSpy = jest.spyOn(service, "updateTree")
updateTreeSpy.mockResolvedValueOnce(treeSha)
mockAxiosInstance.patch.mockImplementation(() => {
const err = {
response: {
status: 404,
},
isAxiosError: true,
}
throw err
})

// Act
await expect(
service.deleteDirectory(sessionData, {
directoryName,
message,
githubSessionData: mockGithubSessionData,
})
).rejects.toStrictEqual({ isAxiosError: true, response: { status: 404 } })

// Assert
expect(mockAxiosInstance.patch).toHaveBeenCalledWith(
endpoint,
{
force: true,
sha: "mockTreeSha",
},
{
headers: authHeader.headers,
}
)
})
})

describe("renameSinglePath", () => {
it("should rename a file correctly", async () => {
// Arrange

const oldPath = "old/path.txt"
const newPath = "new/path.txt"
const message = "Renaming file"

const newGitTree: RawGitTreeEntry[] = [
{
path: oldPath,
type: "file",
sha: "new-sha2",
mode: "100644",
url: "",
},
]

const resolvedTree = [
{
path: newPath,
type: "file",
sha: "new-sha2",
mode: "100644",
url: "",
},
{
path: oldPath,
type: "file",
sha: null,
mode: "100644",
url: "",
},
]
const newCommitSha = "new-commit-sha"
jest.spyOn(service, "getTree").mockResolvedValueOnce(newGitTree)
jest.spyOn(service, "updateTree").mockResolvedValueOnce(newCommitSha)
jest.spyOn(service, "updateRepoState").mockResolvedValueOnce()

// Act
const result = await service.renameSinglePath(
sessionData,
mockGithubSessionData,
oldPath,
newPath,
message
)

// Assert
expect(service.getTree).toHaveBeenCalledWith(
sessionData,
mockGithubSessionData,
{ isRecursive: true }
)
expect(service.updateTree).toHaveBeenCalledWith(
sessionData,
mockGithubSessionData,
{ gitTree: resolvedTree, message }
)
expect(service.updateRepoState).toHaveBeenCalledWith(sessionData, {
commitSha: newCommitSha,
})
expect(result).toEqual({ newSha: newCommitSha })
})
})
})

0 comments on commit 437d36c

Please sign in to comment.