/
getConfiguration.spec.js
148 lines (120 loc) 路 4.08 KB
/
getConfiguration.spec.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import Conf from 'conf'
import { pathExistsSync } from 'path-exists'
import { CONFIG, EMOJI_COMMIT_FORMATS } from '@constants/configuration'
import getConfiguration from '@utils/configurationVault/getConfiguration'
jest.mock('conf', () => jest.fn().mockReturnValue({
store: { from: 'local-cli' },
set: jest.fn()
}))
describe('getConfiguration', () => {
describe('setup', () => {
it('should create the `config` object', () => {
expect(Conf).toHaveBeenCalledWith({
projectName: 'gitmoji',
schema: {
[CONFIG.AUTO_ADD]: { type: 'boolean' },
[CONFIG.EMOJI_FORMAT]: {
enum: Object.values(EMOJI_COMMIT_FORMATS)
},
[CONFIG.SCOPE_PROMPT]: { type: 'boolean' },
[CONFIG.GITMOJIS_URL]: { type: 'string', format: 'url' }
}
})
})
})
describe('get', () => {
describe('when package.json exists', () => {
beforeEach(() => {
jest.resetModules()
})
beforeAll(() => {
pathExistsSync.mockImplementation((path) => {
return path.includes('package.json')
})
})
describe('when `gitmoji` key is defined', () => {
beforeAll(() => {
jest.doMock('../../../package.json', () => ({
gitmoji: { from: 'package.json' }
}))
})
it('should return package.json configuration', () => {
const configuration = getConfiguration()
expect(configuration.get('from')).toEqual('package.json')
})
})
describe('when `gitmoji` key is not defined', () => {
beforeAll(() => {
jest.doMock('../../../package.json', () => ({
gitmoji: undefined
}))
})
it('should return local configuration', () => {
const configuration = getConfiguration()
expect(configuration.get('from')).toEqual('local-cli')
})
})
})
describe('when .gitmojirc exists', () => {
beforeEach(() => {
jest.resetModules()
})
beforeAll(() => {
pathExistsSync.mockImplementation((path) => {
return path.includes('.gitmojirc.json')
})
})
describe('when file contains a valid json', () => {
beforeAll(() => {
jest.doMock('../../../.gitmojirc.json', () => ({
from: 'rc'
}), { virtual: true })
})
it('should return .gitmojirc.json configuration', () => {
const configuration = getConfiguration()
expect(configuration.get('from')).toEqual('rc')
})
})
describe('when file is empty', () => {
beforeAll(() => {
jest.doMock('../../../.gitmojirc.json', () => undefined, { virtual: true })
})
it('should return .gitmojirc.json configuration', () => {
const configuration = getConfiguration()
expect(configuration.get('from')).toEqual('local-cli')
})
})
})
describe('when package.json and .gitmojirc are not available', () => {
beforeAll(() => {
pathExistsSync.mockReturnValue(false)
})
it('should return local configuration', () => {
const configuration = getConfiguration()
expect(configuration.get('from')).toEqual('local-cli')
})
})
describe('when no configuration is available', () => {
beforeAll(() => {
pathExistsSync.mockReturnValue(false)
Conf().store = undefined
})
it('should return the default configuration', () => {
const configuration = getConfiguration()
expect(configuration.get('autoAdd')).toEqual(false)
expect(configuration.get('emojiFormat')).toEqual('code')
expect(configuration.get('scopePrompt')).toEqual(false)
expect(configuration.get('gitmojisUrl')).toEqual(
'https://gitmoji.dev/api/gitmojis'
)
})
})
})
describe('set', () => {
it('should call Conf.set with the key and value', () => {
const configuration = getConfiguration()
configuration.set('key', 'value')
expect(Conf().set).toHaveBeenCalledWith('key', 'value')
})
})
})