From 437d36cb0e1ed5a0f6ceb2a15357531777d53460 Mon Sep 17 00:00:00 2001 From: Kishore <42832651+kishore03109@users.noreply.github.com> Date: Mon, 4 Dec 2023 06:15:15 +0800 Subject: [PATCH] test(githubService): add tests --- src/fixtures/sessionData.ts | 19 +++ src/services/db/GitHubService.ts | 25 +-- src/services/db/RepoService.ts | 3 +- .../db/__tests__/GitHubService.spec.ts | 157 +++++++++++++++++- 4 files changed, 180 insertions(+), 24 deletions(-) diff --git a/src/fixtures/sessionData.ts b/src/fixtures/sessionData.ts index 661515caa..2e1c6e72d 100644 --- a/src/fixtures/sessionData.ts +++ b/src/fixtures/sessionData.ts @@ -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, @@ -25,6 +26,24 @@ export const mockCurrentCommitSha = "mockCurrentCommitSha" export const mockSiteName = "mockSiteName" export const mockGrowthBook = new GrowthBook() +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, diff --git a/src/services/db/GitHubService.ts b/src/services/db/GitHubService.ts index c5710b98a..0060eacb7 100644 --- a/src/services/db/GitHubService.ts +++ b/src/services/db/GitHubService.ts @@ -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, { @@ -300,13 +297,11 @@ export default class GitHubService { sha, fileName, directoryName, - branchName, }: { fileContent: string sha: string fileName: string directoryName: string | undefined - branchName: string } ): Promise { const { accessToken, siteName, isomerUserId: userId } = sessionData @@ -335,7 +330,7 @@ export default class GitHubService { const params = { message, content: encodedNewContent, - branch: branchName, + branch: STAGING_BRANCH, sha: fileSha, } @@ -690,17 +685,11 @@ export default class GitHubService { githubSessionData: GithubSessionData, oldPath: string, newPath: string, - message?: string, - isStaging = true + message?: string ): Promise { - 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" || diff --git a/src/services/db/RepoService.ts b/src/services/db/RepoService.ts index 1d48bbeda..9ffc5d60d 100644 --- a/src/services/db/RepoService.ts +++ b/src/services/db/RepoService.ts @@ -418,7 +418,6 @@ export default class RepoService extends GitHubService { sha, fileName, directoryName, - branchName: STAGING_BRANCH, }) } @@ -612,7 +611,7 @@ export default class RepoService extends GitHubService { { commitSha, branchName = BRANCH_REF, - }: { commitSha: string; branchName?: string } + }: { commitSha: string; branchName: string } ): Promise { const { siteName } = sessionData if ( diff --git a/src/services/db/__tests__/GitHubService.spec.ts b/src/services/db/__tests__/GitHubService.spec.ts index 03a1ced24..75866351a 100644 --- a/src/services/db/__tests__/GitHubService.spec.ts +++ b/src/services/db/__tests__/GitHubService.spec.ts @@ -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 @@ -531,7 +534,6 @@ describe("Github Service", () => { directoryName, fileContent: content, sha, - branchName: BRANCH_REF, }) ).resolves.toMatchObject({ newSha: sha, @@ -560,7 +562,6 @@ describe("Github Service", () => { directoryName, fileContent: content, sha, - branchName: BRANCH_REF, }) ).rejects.toThrowError(NotFoundError) expect(mockAxiosInstance.put).toHaveBeenCalledWith( @@ -596,7 +597,6 @@ describe("Github Service", () => { directoryName, fileContent: content, sha: "", - branchName: BRANCH_REF, }) ).resolves.toMatchObject({ newSha: sha, @@ -628,7 +628,6 @@ describe("Github Service", () => { directoryName, fileContent: content, sha: "", - branchName: BRANCH_REF, }) ).rejects.toThrowError(NotFoundError) expect(mockAxiosInstance.get).toHaveBeenCalledWith(endpoint, { @@ -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 }) + }) + }) })