Skip to content

Commit 7d1bcd7

Browse files
committed
Feat: change getConfig to class for future extension
1 parent c8cd15b commit 7d1bcd7

12 files changed

+241
-166
lines changed

__tests__/getConfig.js renamed to __tests__/Config.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import fs from 'fs-extra';
2-
import json from 'json-extra';
2+
import * as json from 'json-extra';
33
import os from 'os';
44
import path from 'path';
55

6-
import getConfig from '../lib/getConfig';
6+
import Config from '../lib/Config';
77

88
const cwd = process.cwd();
99
const homedir = os.homedir();
@@ -37,13 +37,13 @@ afterAll(() => {
3737
});
3838

3939
it('read config from a specific path', () => {
40-
expect(getConfig(fixtures)).toEqual(json.readToObjSync(path.join(fixtures, '.sgcrc')));
40+
expect(new Config(fixtures).config).toEqual(json.readToObjSync(path.join(fixtures, '.sgcrc')));
4141
});
4242

4343
it('read config from a .sgcrc_default', () => {
4444
const globalConfig = json.readToObjSync(path.join(cwd, '.sgcrc_default'));
4545

46-
expect(getConfig()).toEqual(globalConfig);
46+
expect(new Config().config).toEqual(globalConfig);
4747
});
4848

4949
it('read config from package.json', () => {
@@ -56,7 +56,7 @@ it('read config from package.json', () => {
5656
fs.renameSync(path.join(cwd, 'package.json'), path.join(cwd, `package.json.${randomString}-${datetime}.back`));
5757
fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(packageJson));
5858

59-
const config = getConfig();
59+
const { config } = new Config();
6060

6161
// revert local package
6262
fs.removeSync(path.join(cwd, 'package.json'));
@@ -69,29 +69,29 @@ it('read global config', () => {
6969
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
7070

7171
fs.writeFileSync(path.join(homedir, '.sgcrc'), JSON.stringify(sgcrc));
72-
expect(getConfig()).toEqual(sgcrc);
72+
expect(new Config().config).toEqual(sgcrc);
7373
fs.removeSync(path.join(homedir, '.sgcrc'));
7474
});
7575

7676
it('read local config from `sgc.config.js`', () => {
7777
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
7878

7979
fs.writeFileSync(path.join(cwd, 'sgc.config.js'), `module.exports = (${JSON.stringify(sgcrc)})`);
80-
expect(getConfig()).toEqual(sgcrc);
80+
expect(new Config().config).toEqual(sgcrc);
8181
fs.removeSync(path.join(cwd, 'sgc.config.js'));
8282
});
8383

8484
it('read global config from `sgc.config.js`', () => {
8585
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
8686

8787
fs.writeFileSync(path.join(homedir, 'sgc.config.js'), `module.exports = (${JSON.stringify(sgcrc)})`);
88-
expect(getConfig()).toEqual(sgcrc);
88+
expect(new Config().config).toEqual(sgcrc);
8989
fs.removeSync(path.join(homedir, 'sgc.config.js'));
9090
});
9191

9292
it('read a .sgcrc_default from a deep nested cwd', () => {
9393
const deepCwd = path.join(fixtures, 'very', 'deep', 'directory');
9494
const fixturesConfig = json.readToObjSync(path.join(fixtures, '.sgcrc'));
9595

96-
expect(getConfig(deepCwd)).toEqual(fixturesConfig);
96+
expect(new Config(deepCwd).config).toEqual(fixturesConfig);
9797
});

__tests__/cli.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import cli from '../lib/cli';
77
import sgcPrompt from '../lib/helpers/sgcPrompt';
88
import retryCommit from '../lib/helpers/retryCommit';
99
import promptOrInitialCommit from '../lib/helpers/promptOrInitialCommit';
10-
import getConfig from '../lib/getConfig';
10+
import Config from '../lib/Config';
1111

1212
import pkg from '../package.json';
1313

@@ -17,13 +17,11 @@ jest.mock('git-commit-count');
1717
jest.mock('../lib/helpers/sgcPrompt');
1818
jest.mock('../lib/helpers/retryCommit');
1919
jest.mock('../lib/helpers/promptOrInitialCommit');
20-
jest.mock('../lib/getConfig');
2120

2221
beforeEach(() => {
2322
sgcPrompt.mockReset();
2423
retryCommit.mockReset();
2524
promptOrInitialCommit.mockReset();
26-
getConfig.mockReset();
2725
console.log = jest.fn();
2826
console.error = jest.fn();
2927
});
@@ -61,7 +59,7 @@ it('should prompt init', async () => {
6159
isGit.mockReturnValue(true);
6260
isAdded.mockReturnValue(true);
6361
commitCount.mockReturnValue(0);
64-
getConfig.mockReturnValue({ initialCommit: { isEnabled: true } });
62+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({ initialCommit: { isEnabled: true } });
6563
await cli();
6664

6765
expect(promptOrInitialCommit).toBeCalledTimes(1);
@@ -71,7 +69,7 @@ it('should not prompt init', async () => {
7169
isGit.mockReturnValue(true);
7270
isAdded.mockReturnValue(true);
7371
commitCount.mockReturnValue(0);
74-
getConfig.mockReturnValue({ initialCommit: { isEnabled: false } });
72+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({ initialCommit: { isEnabled: false } });
7573
await cli();
7674

7775
expect(promptOrInitialCommit).not.toBeCalled();

__tests__/helper/commitMeetsRules.spec.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import getConfig from '../../lib/getConfig';
1+
import Config from '../../lib/Config';
22
import commitMeetsRules from '../../lib/helpers/commitMeetsRules';
33

4-
jest.mock('../../lib/getConfig', jest.fn);
5-
6-
const getConfigMock = getConfig as jest.MockedFunction<typeof getConfig>;
7-
84
describe('commitMeetsRules', () => {
95
it('should have one of the types', () => {
10-
getConfigMock.mockReturnValue({
6+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
117
types: [
128
{ type: 'Chore' },
139
],
@@ -18,8 +14,8 @@ describe('commitMeetsRules', () => {
1814
expect(commitMeetsRules('Chore : true')).toBe(false);
1915
});
2016

21-
it('should have one of the types', () => {
22-
getConfigMock.mockReturnValue({
17+
it('should have one of the types 2', () => {
18+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
2319
lowercaseTypes: true,
2420
types: [
2521
{ type: 'Chore' },
@@ -34,7 +30,7 @@ describe('commitMeetsRules', () => {
3430
});
3531

3632
it('should have one of the types with different delimiter', () => {
37-
getConfigMock.mockReturnValue({
33+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
3834
delimiter: ' -',
3935
types: [
4036
{ type: 'Chore' },
@@ -49,7 +45,7 @@ describe('commitMeetsRules', () => {
4945
});
5046

5147
it('should not have scope', () => {
52-
getConfigMock.mockReturnValue({
48+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
5349
scope: false,
5450
types: [
5551
{ type: 'Feat' },
@@ -67,7 +63,7 @@ describe('commitMeetsRules', () => {
6763
});
6864

6965
it('should have optional scope', () => {
70-
getConfigMock.mockReturnValue({
66+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
7167
scope: true,
7268
types: [
7369
{ type: 'Feat' },
@@ -84,7 +80,7 @@ describe('commitMeetsRules', () => {
8480
});
8581

8682
it('should have optional scope with scopespace', () => {
87-
getConfigMock.mockReturnValue({
83+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
8884
scope: true,
8985
addScopeSpace: true,
9086
types: [
@@ -102,7 +98,7 @@ describe('commitMeetsRules', () => {
10298
});
10399

104100
it('should have dot ending', () => {
105-
getConfigMock.mockReturnValue({
101+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
106102
rules: {
107103
endWithDot: true,
108104
},
@@ -118,7 +114,7 @@ describe('commitMeetsRules', () => {
118114
});
119115

120116
it('should have no dot ending', () => {
121-
getConfigMock.mockReturnValue({
117+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
122118
rules: {
123119
endWithDot: false,
124120
},
@@ -134,7 +130,7 @@ describe('commitMeetsRules', () => {
134130
});
135131

136132
it('should have correct length', () => {
137-
getConfigMock.mockReturnValue({
133+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
138134
rules: {
139135
maxChar: 10,
140136
minChar: 8,
@@ -152,7 +148,7 @@ describe('commitMeetsRules', () => {
152148
});
153149

154150
it('should have no length', () => {
155-
getConfigMock.mockReturnValue({
151+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
156152
types: [
157153
{ type: 'Feat' },
158154
],
@@ -166,7 +162,7 @@ describe('commitMeetsRules', () => {
166162
});
167163

168164
it('should have body', () => {
169-
getConfigMock.mockReturnValue({
165+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
170166
body: true,
171167
types: [
172168
{ type: 'Feat' },
@@ -179,7 +175,7 @@ describe('commitMeetsRules', () => {
179175
});
180176

181177
it('should have initial commit', () => {
182-
getConfigMock.mockReturnValue({
178+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
183179
initialCommit: {
184180
isEnabled: true,
185181
message: 'initial commit',
@@ -189,7 +185,7 @@ describe('commitMeetsRules', () => {
189185
expect(commitMeetsRules('initial commit')).toBe(true);
190186
expect(commitMeetsRules('Initial commit')).toBe(false);
191187

192-
getConfigMock.mockReturnValue({
188+
jest.spyOn(Config.prototype, 'config', 'get').mockReturnValue({
193189
initialCommit: {
194190
isEnabled: false,
195191
message: 'initial commit',

0 commit comments

Comments
 (0)