Skip to content

Commit

Permalink
update unit tests based on refactor of util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMurray97 committed Oct 9, 2024
1 parent e91bc81 commit fd42d41
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 151 deletions.
12 changes: 9 additions & 3 deletions test/mock/data.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ export const MOCK_SHA_RESPONSE = {
object: { sha: 'ABC12345678' }
};

export const MOCK_INVALID_SHA_RESPONSE = {
httpStatusCode: 404,
object: ['Resource not found']
};

export const MOCK_POST_BRANCH = { ref: 'refs/heads/test-branch', sha: 'ABC12345678' };

export const MOCK_POST_BLOB = {
Expand All @@ -211,21 +216,22 @@ export const MOCK_POST_BLOB = {

export const MOCK_POST_TREE = {
base_tree: 'ABC12345678',
tree: [{ path: 'terraform/account-1.tf', mode: '100644', type: 'blob', sha: 'mockBlobSha' }]
tree: [{ path: 'terraform/account-1.tf', mode: '100644', type: 'blob', sha: 'ABC12345678' }]
};

export const MOCK_POST_COMMIT = {
message: 'commit message',
tree: 'mockTreeSha',
tree: 'ABC12345678',
parents: ['ABC12345678']
};

export const MOCK_POST_PR = {
title: 'PR Title',
body: 'PR Body',
head: 'new-feature',
head: 'test-branch',
base: 'main'
};

export const MOCK_PR_RESPONSE = {
branchName: 'new-feature',
prContent: 'some content',
Expand Down
12 changes: 11 additions & 1 deletion test/mock/text.mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
export const MOCK_REPO_URL = 'https://api.github.com/repos/test-repo';

export const MOCK_SHA = 'ABC12345678';
export const MOCK_BASE_SHA = 'ABC12345678';
export const MOCK_BLOB_SHA = 'ABC12345678';
export const MOCK_TREE_SHA = 'ABC12345678';
export const MOCK_COMMIT_SHA = 'ABC12345678';

export const MOCK_COMMIT_MESSAGE = 'commit message';
export const MOCK_BRANCH_NAME = 'test-branch';

export const MOCK_PATH = 'terraform/account-1.tf';

export const MOCK_PR_TITLE = 'PR Title';
export const MOCK_PR_BODY = 'PR Body';

157 changes: 10 additions & 147 deletions test/unit/api-sdk/github/github.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jest, beforeEach, afterEach, describe, test, expect } from '@jest/globals';
import { Github } from '../../../../src/api-sdk/github/github';
import { HttpRequest } from '../../../../src/http-request/index';
import { MOCK_REPO_URL, MOCK_SHA, MOCK_BRANCH_NAME } from '../../../mock/text.mock';
import { MOCK_REPO_URL, MOCK_BASE_SHA, MOCK_BLOB_SHA, MOCK_TREE_SHA, MOCK_COMMIT_SHA } from '../../../mock/text.mock';
import {
MOCK_REPO_FETCH_RESPONSE,
MOCK_MEMBER_FETCH_RESPONSE,
Expand Down Expand Up @@ -29,12 +29,6 @@ import {
MOCK_GET_RESPONSE,
MOCK_POST,
MOCK_POST_RESPONSE,
MOCK_SHA_RESPONSE,
MOCK_POST_BRANCH,
MOCK_POST_BLOB,
MOCK_POST_TREE,
MOCK_POST_COMMIT,
MOCK_POST_PR,
MOCK_PR_RESPONSE
} from '../../../mock/data.mock';
import { HttpResponse } from '../../../../src/http-request/type';
Expand Down Expand Up @@ -171,154 +165,23 @@ describe('Github sdk module test suites', () => {
expect(result).toEqual(MOCK_POST_RESPONSE);
});

test('Should retrieve the SHA of the main branch', async () => {
httpRequestMock.httpGet.mockResolvedValue(createMockHttpResponse(MOCK_SHA_RESPONSE));

const result = await github.getSha(MOCK_REPO_URL);

expect(httpRequestMock.httpGet).toHaveBeenCalledWith(`${MOCK_REPO_URL}/git/refs/heads/main`);
expect(result).toEqual(MOCK_SHA);
});

test('Should create a new branch successfully', async () => {
httpRequestMock.httpPost.mockResolvedValue(createMockHttpResponse({}));

const result = await github.createBranch(MOCK_REPO_URL, MOCK_BRANCH_NAME, MOCK_SHA);
expect(httpRequestMock.httpPost).toHaveBeenCalledWith(
`${MOCK_REPO_URL}/git/refs`,
JSON.stringify(MOCK_POST_BRANCH)
);
expect(result.httpStatusCode).toEqual(200);
});

test('should create a blob and return the SHA', async () => {
httpRequestMock.httpPost.mockResolvedValue(createMockHttpResponse({ sha: MOCK_SHA }));

const content = 'test content';
const result = await github.createBlob(MOCK_REPO_URL, content);

expect(httpRequestMock.httpPost).toHaveBeenCalledWith(`${MOCK_REPO_URL}/git/blobs`, JSON.stringify(MOCK_POST_BLOB));
expect(result).toEqual(MOCK_SHA);
});

test('should create a tree and return the SHA', async () => {
httpRequestMock.httpPost.mockResolvedValue(createMockHttpResponse({ sha: MOCK_SHA }));

const path = 'terraform/account-1.tf';
const blobSha = 'mockBlobSha';

const result = await github.createTree(MOCK_REPO_URL, MOCK_SHA, path, blobSha);

expect(httpRequestMock.httpPost).toHaveBeenCalledWith(`${MOCK_REPO_URL}/git/trees`, JSON.stringify(MOCK_POST_TREE));
expect(result).toEqual(MOCK_SHA);
});

test('should create a commit and return the SHA', async () => {
const commitSha = 'mockCommitSha';
httpRequestMock.httpPost.mockResolvedValue(createMockHttpResponse({ sha: commitSha }));

const message = 'commit message';
const treeSha = 'mockTreeSha';

const result = await github.createCommit(MOCK_REPO_URL, message, treeSha, MOCK_SHA);

expect(httpRequestMock.httpPost).toHaveBeenCalledWith(`${MOCK_REPO_URL}/git/commits`, JSON.stringify(MOCK_POST_COMMIT));
expect(result).toEqual(commitSha);
});

test('should update the branch reference with the commit SHA', async () => {
httpRequestMock.httpPost.mockResolvedValue(createMockHttpResponse({}));

const result = await github.updateBranchReferance(MOCK_REPO_URL, MOCK_BRANCH_NAME, MOCK_SHA);

expect(httpRequestMock.httpPost).toHaveBeenCalledWith(`${MOCK_REPO_URL}/git/refs/heads/${MOCK_BRANCH_NAME}`, JSON.stringify({ sha: MOCK_SHA }));
expect(result.httpStatusCode).toEqual(200);
});
test('should create a pull request and return the response', async () => {
httpRequestMock.httpPost.mockResolvedValue(createMockHttpResponse(MOCK_PR_RESPONSE));

const title = 'PR Title';
const body = 'PR Body';
const head = 'new-feature';
const base = 'main';

const result = await github.createPullRequest(MOCK_REPO_URL, title, body, head, base);

expect(httpRequestMock.httpPost).toHaveBeenCalledWith(`${MOCK_REPO_URL}/pulls`, JSON.stringify(MOCK_POST_PR));
expect(result).toEqual({
httpStatusCode: 200,
resource: MOCK_PR_RESPONSE
});
});

test('should successfully post a pull request and return the response', async () => {
httpRequestMock.httpGet.mockResolvedValue(createMockHttpResponse({ object: { sha: MOCK_SHA } }));
httpRequestMock.httpGet.mockResolvedValue(createMockHttpResponse({ object: { sha: MOCK_BASE_SHA } }));
httpRequestMock.httpPost
.mockResolvedValueOnce(createMockHttpResponse({})) // branch creation
.mockResolvedValueOnce(createMockHttpResponse({ sha: 'mockBlobSha' }))
.mockResolvedValueOnce(createMockHttpResponse({ sha: 'mockTreeSha' }))
.mockResolvedValueOnce(createMockHttpResponse({ sha: 'mockCommitSha' }))
.mockResolvedValueOnce(createMockHttpResponse({})) // branch update
.mockResolvedValueOnce(createMockHttpResponse({}))
.mockResolvedValueOnce(createMockHttpResponse({ sha: MOCK_BLOB_SHA }))
.mockResolvedValueOnce(createMockHttpResponse({ sha: MOCK_TREE_SHA }))
.mockResolvedValueOnce(createMockHttpResponse({ sha: MOCK_COMMIT_SHA }))
.mockResolvedValueOnce(createMockHttpResponse({}))
.mockResolvedValue(createMockHttpResponse(MOCK_PR_RESPONSE));

const result = await github.postPullRequest(MOCK_REPO_URL, MOCK_PR_RESPONSE);

expect(result).toEqual({
httpStatusCode: 200,
resource: MOCK_PR_RESPONSE
});
});

test('Should return the error when SHA resource does not exist in getSha', async () => {
httpRequestMock.httpGet.mockResolvedValue(createMockHttpResponse({}, 404));

const result = await github.getSha(MOCK_REPO_URL);

expect(httpRequestMock.httpGet).toHaveBeenCalledWith(`${MOCK_REPO_URL}/git/refs/heads/main`);
expect(result).toEqual({
httpStatusCode: 404,
errors: [{}],
});
});
test('Should return the error when blob SHA resource does not exist in createBlob', async () => {
httpRequestMock.httpPost.mockResolvedValue(createMockHttpResponse({}, 404));

const content = 'test content';
const result = await github.createBlob(MOCK_REPO_URL, content);

expect(httpRequestMock.httpPost).toHaveBeenCalledWith(`${MOCK_REPO_URL}/git/blobs`, JSON.stringify(MOCK_POST_BLOB));
expect(result).toEqual({
httpStatusCode: 404,
errors: [{}],
});
});

test('Should return the error when tree SHA resource does not exist in createTree', async () => {
httpRequestMock.httpPost.mockResolvedValue(createMockHttpResponse({}, 404));

const path = 'terraform/account-1.tf';
const blobSha = 'mockBlobSha';
const result = await github.createTree(MOCK_REPO_URL, MOCK_SHA, path, blobSha);
expect(result).toEqual({ httpStatusCode: 200, resource: MOCK_PR_RESPONSE });

expect(httpRequestMock.httpPost).toHaveBeenCalledWith(`${MOCK_REPO_URL}/git/trees`, JSON.stringify(MOCK_POST_TREE));
expect(result).toEqual({
httpStatusCode: 404,
errors: [{}],
});
});

test('Should return the error when commit SHA resource does not exist in createCommit', async () => {
httpRequestMock.httpPost.mockResolvedValue(createMockHttpResponse({}, 404));
expect(httpRequestMock.httpGet).toHaveBeenCalledTimes(1);

const message = 'commit message';
const treeSha = 'mockTreeSha';
const result = await github.createCommit(MOCK_REPO_URL, message, treeSha, MOCK_SHA);

expect(httpRequestMock.httpPost).toHaveBeenCalledWith(`${MOCK_REPO_URL}/git/commits`, JSON.stringify(MOCK_POST_COMMIT));
expect(result).toEqual({
httpStatusCode: 404,
errors: [{}],
});
expect(httpRequestMock.httpPost).toHaveBeenCalledTimes(6);
});

test('Should return an object with an error property', async () => {
Expand Down
94 changes: 94 additions & 0 deletions test/unit/api-sdk/github/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { jest, afterEach, describe, test, expect } from '@jest/globals';

import {
extractBaseShaHelper,
extractShaHelper,
getShaParams,
createBranchParams,
createBlobParams,
createTreeParams,
createCommitParams,
updateBranchReferenceParams,
createPullRequestParams
} from '../../../../src/api-sdk/github/utils';
import { MOCK_REPO_URL, MOCK_BASE_SHA, MOCK_BLOB_SHA, MOCK_TREE_SHA, MOCK_COMMIT_SHA, MOCK_BRANCH_NAME, MOCK_PATH, MOCK_COMMIT_MESSAGE, MOCK_PR_TITLE, MOCK_PR_BODY } from '../../../mock/text.mock';
import { MOCK_POST_BLOB, MOCK_INVALID_SHA_RESPONSE, MOCK_POST_BRANCH, MOCK_POST_TREE, MOCK_POST_COMMIT, MOCK_POST_PR } from '../../../mock/data.mock';

describe('Github utils test suites', () => {

afterEach(() => {
jest.restoreAllMocks();
});

test('extractBaseShaHelper should return sha if it exists', () => {
const mockBaseShaResponse = {
httpStatusCode: 200,
resource: { object: { sha: MOCK_BASE_SHA } }
};
const result = extractBaseShaHelper(mockBaseShaResponse);
expect(result).toBe(MOCK_BASE_SHA);
});

test('extractBaseShaHelper should return response if resource does not exist', () => {
const result = extractBaseShaHelper(MOCK_INVALID_SHA_RESPONSE);
expect(result).toEqual(MOCK_INVALID_SHA_RESPONSE);
});

test('extractShaHelper should return sha if it exists', () => {
const mockShaResponse = {
httpStatusCode: 200,
resource: { sha: MOCK_BLOB_SHA }
};
const result = extractShaHelper(mockShaResponse);
expect(result).toBe(MOCK_BLOB_SHA);
});

test('extractShaHelper should return response if sha does not exist', () => {
const result = extractShaHelper(MOCK_INVALID_SHA_RESPONSE);
expect(result).toEqual(MOCK_INVALID_SHA_RESPONSE);
});

test('getShaParams should return the correct sha URL', () => {
const result = getShaParams(MOCK_REPO_URL);
expect(result).toBe(`${MOCK_REPO_URL}/git/refs/heads/main`);
});

test('createBranchParams should return the correct branch URL and body', () => {
const { branchUrl, branchBody } = createBranchParams(MOCK_REPO_URL, MOCK_BRANCH_NAME, MOCK_BASE_SHA);

expect(branchUrl).toBe(`${MOCK_REPO_URL}/git/refs`);
expect(branchBody).toEqual(MOCK_POST_BRANCH);
});

test('createBlobParams should return the correct blob URL and body', () => {
const { blobUrl, blobBody } = createBlobParams(MOCK_REPO_URL, 'test content');

expect(blobUrl).toBe(`${MOCK_REPO_URL}/git/blobs`);
expect(blobBody).toEqual(MOCK_POST_BLOB);
});

test('createTreeParams should return the correct tree URL and body', () => {
const { treeUrl, treeBody } = createTreeParams(MOCK_REPO_URL, MOCK_BASE_SHA, MOCK_PATH, MOCK_BLOB_SHA);

expect(treeUrl).toBe(`${MOCK_REPO_URL}/git/trees`);
expect(treeBody).toEqual(MOCK_POST_TREE);
});

test('createCommitParams should return the correct commit URL and body', () => {
const { commitUrl, commitBody } = createCommitParams(MOCK_REPO_URL, MOCK_COMMIT_MESSAGE, MOCK_TREE_SHA, MOCK_BASE_SHA);
expect(commitUrl).toBe(`${MOCK_REPO_URL}/git/commits`);
expect(commitBody).toEqual(MOCK_POST_COMMIT);
});

test('updateBranchReferenceParams should return the correct ref URL and body', () => {
const { refUrl, refBody } = updateBranchReferenceParams(MOCK_REPO_URL, MOCK_BRANCH_NAME, MOCK_COMMIT_SHA);
expect(refUrl).toBe(`${MOCK_REPO_URL}/git/refs/heads/${MOCK_BRANCH_NAME}`);
expect(refBody).toEqual({ sha: MOCK_COMMIT_SHA });
});

test('createPullRequestParams should return the correct PR URL and body', () => {
const { prUrl, prPostbody } = createPullRequestParams(MOCK_REPO_URL, MOCK_PR_TITLE, MOCK_PR_BODY, MOCK_BRANCH_NAME, 'main');
expect(prUrl).toBe(`${MOCK_REPO_URL}/pulls`);
expect(prPostbody).toEqual(MOCK_POST_PR);
});
});

0 comments on commit fd42d41

Please sign in to comment.