1
1
import { createBranch } from '../src/create-branch'
2
2
import { readFileSync } from 'fs' ;
3
+ import { context } from '@actions/github' ;
3
4
4
5
describe ( 'Create a branch based on the input' , ( ) => {
5
6
@@ -12,6 +13,9 @@ describe('Create a branch based on the input', () => {
12
13
octokitMock = {
13
14
git : {
14
15
createRef : jest . fn ( )
16
+ } ,
17
+ repos : {
18
+ getBranch : jest . fn ( )
15
19
}
16
20
}
17
21
} )
@@ -30,8 +34,20 @@ describe('Create a branch based on the input', () => {
30
34
process . env . GITHUB_TOKEN = 'token'
31
35
} )
32
36
37
+ it ( 'gets a branch' , async ( ) => {
38
+ octokitMock . repos . getBranch . mockRejectedValue ( new HttpError ( ) )
39
+ process . env . GITHUB_REPOSITORY = 'peterjgrainger/test-action-changelog-reminder'
40
+ await createBranch ( githubMock , context , branch )
41
+ expect ( octokitMock . repos . getBranch ) . toHaveBeenCalledWith ( {
42
+ repo : 'test-action-changelog-reminder' ,
43
+ owner : 'peterjgrainger' ,
44
+ branch
45
+ } )
46
+ } )
47
+
33
48
34
- it ( 'Create new branch' , async ( ) => {
49
+ it ( 'Create new branch if not already there' , async ( ) => {
50
+ octokitMock . repos . getBranch . mockRejectedValue ( new HttpError ( ) )
35
51
await createBranch ( githubMock , contextMock , branch )
36
52
expect ( octokitMock . git . createRef ) . toHaveBeenCalledWith ( {
37
53
ref : 'refs/heads/release-v1' ,
@@ -48,4 +64,18 @@ describe('Create a branch based on the input', () => {
48
64
expect ( error ) . toEqual ( new ReferenceError ( 'No token defined in the environment variables' ) )
49
65
}
50
66
} )
67
+
68
+ it ( 'fails if branch already exists' , async ( ) => {
69
+ try {
70
+ await createBranch ( githubMock , contextMock , branch )
71
+ throw Error ( 'should error' )
72
+ } catch ( error ) {
73
+ expect ( error ) . toEqual ( new ReferenceError ( 'Branch already exists' ) )
74
+ }
75
+ } )
51
76
} ) ;
77
+
78
+ class HttpError extends Error {
79
+ name = 'HttpError'
80
+ status = 404
81
+ }
0 commit comments