Skip to content

Commit

Permalink
Add unit tests for commit SHA
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebettridge authored and peterjgrainger committed Dec 22, 2021
1 parent d1ae555 commit 1a8eb16
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions __tests__/create-branch.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
import {createBranch} from '../src/create-branch'
import { createBranch } from '../src/create-branch'
import { readFileSync } from 'fs';
import { context } from '@actions/github';

describe('Create a branch based on the input', () => {
process.env.GITHUB_TOKEN = 'token'

let githubMock;
let contextMock;
let branch = 'release-v1';
let octokitMock;

beforeEach(()=> {
octokitMock = {
git: {
createRef: jest.fn()
},
repos: {
getBranch: jest.fn()
}
let sha = 'ffac537e6cbbf934b08745a378932722df287a53';
let contextMock = JSON.parse(readFileSync('__tests__/context.json', 'utf8'));
let githubMock = jest.fn();
let octokitMock = {
git: {
createRef: jest.fn()
},
repos: {
getBranch: jest.fn()
}
})

beforeEach(()=> {
githubMock = jest.fn().mockImplementation(() => {
return octokitMock;
})
})
};

beforeEach(() => {
contextMock = JSON.parse(readFileSync('__tests__/context.json', 'utf8'))
})

beforeEach(() => {
process.env.GITHUB_TOKEN = 'token'
})
jest.resetAllMocks();
githubMock.mockImplementation(() => octokitMock);
});

it('gets a branch', async () => {
octokitMock.repos.getBranch.mockRejectedValue(new HttpError())
Expand All @@ -43,10 +32,9 @@ describe('Create a branch based on the input', () => {
owner: 'peterjgrainger',
branch
})
})

});

it('Create new branch if not already there', async () => {
it('Creates a new branch if not already there', async () => {
octokitMock.repos.getBranch.mockRejectedValue(new HttpError())
await createBranch(githubMock, contextMock, branch)
expect(octokitMock.git.createRef).toHaveBeenCalledWith({
Expand All @@ -55,6 +43,15 @@ describe('Create a branch based on the input', () => {
})
});

it('Creates a new branch from a given commit SHA', async () => {
octokitMock.repos.getBranch.mockRejectedValue(new HttpError())
await createBranch(githubMock, contextMock, branch, sha)
expect(octokitMock.git.createRef).toHaveBeenCalledWith({
ref: 'refs/heads/release-v1',
sha: 'ffac537e6cbbf934b08745a378932722df287a53'
})
})

it('Replaces refs/heads in branch name', async () => {
octokitMock.repos.getBranch.mockRejectedValue(new HttpError())
await createBranch(githubMock, contextMock, `refs/heads/${branch}`)
Expand All @@ -64,15 +61,15 @@ describe('Create a branch based on the input', () => {
})
});

it('fails if github token isn\'t defined', async() => {
it('Fails if github token isn\'t defined', async () => {
delete process.env.GITHUB_TOKEN
expect.assertions(1);
try {
await createBranch(githubMock, contextMock, branch)
} catch (error) {
expect(error).toEqual(new ReferenceError('No token defined in the environment variables'))
}
})
});
});

class HttpError extends Error {
Expand Down

0 comments on commit 1a8eb16

Please sign in to comment.