|
| 1 | +import { jest, afterEach, describe, test, expect } from '@jest/globals'; |
| 2 | + |
| 3 | +import { |
| 4 | + extractBaseShaHelper, |
| 5 | + extractShaHelper, |
| 6 | + getShaParams, |
| 7 | + createBranchParams, |
| 8 | + createBlobParams, |
| 9 | + createTreeParams, |
| 10 | + createCommitParams, |
| 11 | + updateBranchReferenceParams, |
| 12 | + createPullRequestParams |
| 13 | +} from '../../../../src/api-sdk/github/utils'; |
| 14 | +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'; |
| 15 | +import { MOCK_POST_BLOB, MOCK_INVALID_SHA_RESPONSE, MOCK_POST_BRANCH, MOCK_POST_TREE, MOCK_POST_COMMIT, MOCK_POST_PR, MOCK_API_ERROR } from '../../../mock/data.mock'; |
| 16 | + |
| 17 | +describe('Github utils test suites', () => { |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + jest.restoreAllMocks(); |
| 21 | + }); |
| 22 | + |
| 23 | + test('extractBaseShaHelper should return sha if it exists', () => { |
| 24 | + const mockBaseShaResponse = { |
| 25 | + httpStatusCode: 200, |
| 26 | + resource: { object: { sha: MOCK_BASE_SHA } } |
| 27 | + }; |
| 28 | + const result = extractBaseShaHelper(mockBaseShaResponse); |
| 29 | + expect(result).toBe(MOCK_BASE_SHA); |
| 30 | + }); |
| 31 | + |
| 32 | + test('extractBaseShaHelper should return response if resource does not exist', () => { |
| 33 | + jest.spyOn(global, 'Error').mockImplementationOnce(() => MOCK_API_ERROR); |
| 34 | + |
| 35 | + expect(() => extractBaseShaHelper(MOCK_INVALID_SHA_RESPONSE)).toThrowError(MOCK_API_ERROR); |
| 36 | + }); |
| 37 | + |
| 38 | + test('extractShaHelper should return sha if it exists', () => { |
| 39 | + const mockShaResponse = { |
| 40 | + httpStatusCode: 200, |
| 41 | + resource: { sha: MOCK_BLOB_SHA } |
| 42 | + }; |
| 43 | + const result = extractShaHelper(mockShaResponse); |
| 44 | + expect(result).toBe(MOCK_BLOB_SHA); |
| 45 | + }); |
| 46 | + |
| 47 | + test('extractShaHelper should return response if sha does not exist', () => { |
| 48 | + jest.spyOn(global, 'Error').mockImplementationOnce(() => MOCK_API_ERROR); |
| 49 | + |
| 50 | + expect(() => extractShaHelper(MOCK_INVALID_SHA_RESPONSE)).toThrowError(MOCK_API_ERROR); |
| 51 | + }); |
| 52 | + |
| 53 | + test('getShaParams should return the correct sha URL', () => { |
| 54 | + const result = getShaParams(MOCK_REPO_URL); |
| 55 | + expect(result).toBe(`${MOCK_REPO_URL}/git/refs/heads/main`); |
| 56 | + }); |
| 57 | + |
| 58 | + test('createBranchParams should return the correct branch URL and body', () => { |
| 59 | + const { branchUrl, branchBody } = createBranchParams(MOCK_REPO_URL, MOCK_BRANCH_NAME, MOCK_BASE_SHA); |
| 60 | + |
| 61 | + expect(branchUrl).toBe(`${MOCK_REPO_URL}/git/refs`); |
| 62 | + expect(branchBody).toEqual(MOCK_POST_BRANCH); |
| 63 | + }); |
| 64 | + |
| 65 | + test('createBlobParams should return the correct blob URL and body', () => { |
| 66 | + const { blobUrl, blobBody } = createBlobParams(MOCK_REPO_URL, 'test content'); |
| 67 | + |
| 68 | + expect(blobUrl).toBe(`${MOCK_REPO_URL}/git/blobs`); |
| 69 | + expect(blobBody).toEqual(MOCK_POST_BLOB); |
| 70 | + }); |
| 71 | + |
| 72 | + test('createTreeParams should return the correct tree URL and body', () => { |
| 73 | + const { treeUrl, treeBody } = createTreeParams(MOCK_REPO_URL, MOCK_BASE_SHA, MOCK_PATH, MOCK_BLOB_SHA); |
| 74 | + |
| 75 | + expect(treeUrl).toBe(`${MOCK_REPO_URL}/git/trees`); |
| 76 | + expect(treeBody).toEqual(MOCK_POST_TREE); |
| 77 | + }); |
| 78 | + |
| 79 | + test('createCommitParams should return the correct commit URL and body', () => { |
| 80 | + const { commitUrl, commitBody } = createCommitParams(MOCK_REPO_URL, MOCK_COMMIT_MESSAGE, MOCK_TREE_SHA, MOCK_BASE_SHA); |
| 81 | + expect(commitUrl).toBe(`${MOCK_REPO_URL}/git/commits`); |
| 82 | + expect(commitBody).toEqual(MOCK_POST_COMMIT); |
| 83 | + }); |
| 84 | + |
| 85 | + test('updateBranchReferenceParams should return the correct ref URL and body', () => { |
| 86 | + const { refUrl, refBody } = updateBranchReferenceParams(MOCK_REPO_URL, MOCK_BRANCH_NAME, MOCK_COMMIT_SHA); |
| 87 | + expect(refUrl).toBe(`${MOCK_REPO_URL}/git/refs/heads/${MOCK_BRANCH_NAME}`); |
| 88 | + expect(refBody).toEqual({ sha: MOCK_COMMIT_SHA }); |
| 89 | + }); |
| 90 | + |
| 91 | + test('createPullRequestParams should return the correct PR URL and body', () => { |
| 92 | + const { prUrl, prPostbody } = createPullRequestParams(MOCK_REPO_URL, MOCK_PR_TITLE, MOCK_PR_BODY, MOCK_BRANCH_NAME); |
| 93 | + expect(prUrl).toBe(`${MOCK_REPO_URL}/pulls`); |
| 94 | + expect(prPostbody).toEqual(MOCK_POST_PR); |
| 95 | + }); |
| 96 | +}); |
0 commit comments