-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
84 lines (64 loc) · 2.55 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const gitIsBranchProtectedCli = require('.')
let branch = require('git-branch')
jest.mock('git-branch')
let options = {}
beforeEach(() => {
// mock global functions
process.exit = jest.fn()
global.console.error = jest.fn()
options = {
branches: 'master, main, develop',
silent: true
}
})
it('should throw if "branches" is not a string', async () => {
options.branches = 123
await expect(gitIsBranchProtectedCli(options)).rejects.toThrow()
options.branches = []
await expect(gitIsBranchProtectedCli(options)).rejects.toThrow()
options.branches = false
await expect(gitIsBranchProtectedCli(options)).rejects.toThrow()
})
it('should exit with 1 if currently on "master" and no branches are passed in', async () => {
branch.mockReturnValue(Promise.resolve('master'))
await gitIsBranchProtectedCli(options)
expect(process.exit).toHaveBeenCalledWith(1)
})
it('should exit with 1 if currently on "main" and no branches are passed in', async () => {
branch.mockReturnValue(Promise.resolve('main'))
await gitIsBranchProtectedCli(options)
expect(process.exit).toHaveBeenCalledWith(1)
})
it('should exit with 1 if currently on "develop" and no branches are passed in', async () => {
branch.mockReturnValue(Promise.resolve('develop'))
await gitIsBranchProtectedCli(options)
expect(process.exit).toHaveBeenCalledWith(1)
})
it('should exit with 0 if currently on "feature" and no branches are passed in', async () => {
branch.mockReturnValue(Promise.resolve('feature'))
await gitIsBranchProtectedCli(options)
expect(process.exit).toHaveBeenCalledWith(0)
})
it('should exit with 0 if currently on "develop" and only "master" is protected', async () => {
options.branches = 'master'
branch.mockReturnValue(Promise.resolve('develop'))
await gitIsBranchProtectedCli(options)
expect(process.exit).toHaveBeenCalledWith(0)
})
it('should exit with 0 if currently on "master" no branches are protected', async () => {
options.branches = ''
branch.mockReturnValue(Promise.resolve('master'))
await gitIsBranchProtectedCli(options)
expect(process.exit).toHaveBeenCalledWith(0)
})
it('should log when "silent" mode is disabled', async () => {
options.silent = false
branch.mockReturnValue(Promise.resolve('master'))
await gitIsBranchProtectedCli(options)
expect(global.console.error).toHaveBeenCalledWith('Branch is protected')
})
it('should NOT log when "silent" mode is active', async () => {
branch.mockReturnValue(Promise.resolve('master'))
await gitIsBranchProtectedCli(options)
expect(global.console.error).not.toHaveBeenCalled()
})