1
- import { createBranch } from '../src/create-branch'
1
+ import { createBranch } from '../src/create-branch'
2
2
import { readFileSync } from 'fs' ;
3
3
import { context } from '@actions/github' ;
4
4
5
5
describe ( 'Create a branch based on the input' , ( ) => {
6
+ process . env . GITHUB_TOKEN = 'token'
6
7
7
- let githubMock ;
8
- let contextMock ;
9
8
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 ( )
20
18
}
21
- } )
22
-
23
- beforeEach ( ( ) => {
24
- githubMock = jest . fn ( ) . mockImplementation ( ( ) => {
25
- return octokitMock ;
26
- } )
27
- } )
19
+ } ;
28
20
29
21
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
+ } ) ;
36
25
37
26
it ( 'gets a branch' , async ( ) => {
38
27
octokitMock . repos . getBranch . mockRejectedValue ( new HttpError ( ) )
@@ -43,10 +32,9 @@ describe('Create a branch based on the input', () => {
43
32
owner : 'peterjgrainger' ,
44
33
branch
45
34
} )
46
- } )
47
-
35
+ } ) ;
48
36
49
- it ( 'Create new branch if not already there' , async ( ) => {
37
+ it ( 'Creates a new branch if not already there' , async ( ) => {
50
38
octokitMock . repos . getBranch . mockRejectedValue ( new HttpError ( ) )
51
39
await createBranch ( githubMock , contextMock , branch )
52
40
expect ( octokitMock . git . createRef ) . toHaveBeenCalledWith ( {
@@ -55,6 +43,15 @@ describe('Create a branch based on the input', () => {
55
43
} )
56
44
} ) ;
57
45
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
+
58
55
it ( 'Replaces refs/heads in branch name' , async ( ) => {
59
56
octokitMock . repos . getBranch . mockRejectedValue ( new HttpError ( ) )
60
57
await createBranch ( githubMock , contextMock , `refs/heads/${ branch } ` )
@@ -64,15 +61,15 @@ describe('Create a branch based on the input', () => {
64
61
} )
65
62
} ) ;
66
63
67
- it ( 'fails if github token isn\'t defined' , async ( ) => {
64
+ it ( 'Fails if github token isn\'t defined' , async ( ) => {
68
65
delete process . env . GITHUB_TOKEN
69
66
expect . assertions ( 1 ) ;
70
67
try {
71
68
await createBranch ( githubMock , contextMock , branch )
72
69
} catch ( error ) {
73
70
expect ( error ) . toEqual ( new ReferenceError ( 'No token defined in the environment variables' ) )
74
71
}
75
- } )
72
+ } ) ;
76
73
} ) ;
77
74
78
75
class HttpError extends Error {
0 commit comments