-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update unit tests based on refactor of util functions
- Loading branch information
1 parent
e91bc81
commit fd42d41
Showing
4 changed files
with
124 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |