Skip to content

Commit 1a8eb16

Browse files
lukebettridgepeterjgrainger
authored andcommitted
Add unit tests for commit SHA
1 parent d1ae555 commit 1a8eb16

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

__tests__/create-branch.test.ts

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,27 @@
1-
import {createBranch} from '../src/create-branch'
1+
import { createBranch } from '../src/create-branch'
22
import { readFileSync } from 'fs';
33
import { context } from '@actions/github';
44

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

7-
let githubMock;
8-
let contextMock;
98
let branch = 'release-v1';
10-
let octokitMock;
11-
12-
beforeEach(()=> {
13-
octokitMock = {
14-
git: {
15-
createRef: jest.fn()
16-
},
17-
repos: {
18-
getBranch: jest.fn()
19-
}
9+
let sha = 'ffac537e6cbbf934b08745a378932722df287a53';
10+
let contextMock = JSON.parse(readFileSync('__tests__/context.json', 'utf8'));
11+
let githubMock = jest.fn();
12+
let octokitMock = {
13+
git: {
14+
createRef: jest.fn()
15+
},
16+
repos: {
17+
getBranch: jest.fn()
2018
}
21-
})
22-
23-
beforeEach(()=> {
24-
githubMock = jest.fn().mockImplementation(() => {
25-
return octokitMock;
26-
})
27-
})
19+
};
2820

2921
beforeEach(() => {
30-
contextMock = JSON.parse(readFileSync('__tests__/context.json', 'utf8'))
31-
})
32-
33-
beforeEach(() => {
34-
process.env.GITHUB_TOKEN = 'token'
35-
})
22+
jest.resetAllMocks();
23+
githubMock.mockImplementation(() => octokitMock);
24+
});
3625

3726
it('gets a branch', async () => {
3827
octokitMock.repos.getBranch.mockRejectedValue(new HttpError())
@@ -43,10 +32,9 @@ describe('Create a branch based on the input', () => {
4332
owner: 'peterjgrainger',
4433
branch
4534
})
46-
})
47-
35+
});
4836

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

46+
it('Creates a new branch from a given commit SHA', async () => {
47+
octokitMock.repos.getBranch.mockRejectedValue(new HttpError())
48+
await createBranch(githubMock, contextMock, branch, sha)
49+
expect(octokitMock.git.createRef).toHaveBeenCalledWith({
50+
ref: 'refs/heads/release-v1',
51+
sha: 'ffac537e6cbbf934b08745a378932722df287a53'
52+
})
53+
})
54+
5855
it('Replaces refs/heads in branch name', async () => {
5956
octokitMock.repos.getBranch.mockRejectedValue(new HttpError())
6057
await createBranch(githubMock, contextMock, `refs/heads/${branch}`)
@@ -64,15 +61,15 @@ describe('Create a branch based on the input', () => {
6461
})
6562
});
6663

67-
it('fails if github token isn\'t defined', async() => {
64+
it('Fails if github token isn\'t defined', async () => {
6865
delete process.env.GITHUB_TOKEN
6966
expect.assertions(1);
7067
try {
7168
await createBranch(githubMock, contextMock, branch)
7269
} catch (error) {
7370
expect(error).toEqual(new ReferenceError('No token defined in the environment variables'))
7471
}
75-
})
72+
});
7673
});
7774

7875
class HttpError extends Error {

0 commit comments

Comments
 (0)