Skip to content

Commit 0e81ff4

Browse files
authored
Chore: Convert hubdb commands to TypeScript (#1372)
1 parent a251c54 commit 0e81ff4

23 files changed

+830
-253
lines changed

commands/__tests__/hubdb.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import yargs, { Argv } from 'yargs';
2+
import * as clear from '../hubdb/clear';
3+
import * as create from '../hubdb/create';
4+
import * as deleteCommand from '../hubdb/delete';
5+
import * as fetch from '../hubdb/fetch';
6+
7+
jest.mock('yargs');
8+
jest.mock('../hubdb/clear');
9+
jest.mock('../hubdb/create');
10+
jest.mock('../hubdb/delete');
11+
jest.mock('../hubdb/fetch');
12+
jest.mock('../../lib/commonOpts');
13+
14+
const commandSpy = jest
15+
.spyOn(yargs as Argv, 'command')
16+
.mockReturnValue(yargs as Argv);
17+
const demandCommandSpy = jest
18+
.spyOn(yargs as Argv, 'demandCommand')
19+
.mockReturnValue(yargs as Argv);
20+
21+
// Import this last so mocks apply
22+
import * as hubdbCommands from '../hubdb';
23+
24+
describe('commands/hubdb', () => {
25+
describe('command', () => {
26+
it('should have the correct command structure', () => {
27+
expect(hubdbCommands.command).toEqual('hubdb');
28+
});
29+
});
30+
31+
describe('describe', () => {
32+
it('should provide a description', () => {
33+
expect(hubdbCommands.describe).toBeDefined();
34+
});
35+
});
36+
37+
describe('builder', () => {
38+
beforeEach(() => {
39+
commandSpy.mockClear();
40+
demandCommandSpy.mockClear();
41+
});
42+
43+
const subcommands = [clear, create, deleteCommand, fetch];
44+
45+
it('should demand the command takes one positional argument', () => {
46+
hubdbCommands.builder(yargs as Argv);
47+
48+
expect(demandCommandSpy).toHaveBeenCalledTimes(1);
49+
expect(demandCommandSpy).toHaveBeenCalledWith(1, '');
50+
});
51+
52+
it('should add the correct number of sub commands', () => {
53+
hubdbCommands.builder(yargs as Argv);
54+
expect(commandSpy).toHaveBeenCalledTimes(subcommands.length);
55+
});
56+
57+
it.each(subcommands)('should attach the %s subcommand', module => {
58+
hubdbCommands.builder(yargs as Argv);
59+
expect(commandSpy).toHaveBeenCalledWith(module);
60+
});
61+
});
62+
});

commands/__tests__/secret.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import yargs, { Argv } from 'yargs';
2+
import * as addSecret from '../secret/addSecret';
3+
import * as deleteSecret from '../secret/deleteSecret';
4+
import * as listSecret from '../secret/listSecret';
5+
import * as updateSecret from '../secret/updateSecret';
6+
7+
jest.mock('yargs');
8+
jest.mock('../secret/addSecret');
9+
jest.mock('../secret/deleteSecret');
10+
jest.mock('../secret/listSecret');
11+
jest.mock('../secret/updateSecret');
12+
jest.mock('../../lib/commonOpts');
13+
14+
const commandSpy = jest
15+
.spyOn(yargs as Argv, 'command')
16+
.mockReturnValue(yargs as Argv);
17+
const demandCommandSpy = jest
18+
.spyOn(yargs as Argv, 'demandCommand')
19+
.mockReturnValue(yargs as Argv);
20+
21+
// Import this last so mocks apply
22+
import * as secretCommands from '../secret';
23+
24+
describe('commands/account', () => {
25+
describe('command', () => {
26+
it('should have the correct command structure', () => {
27+
expect(secretCommands.command).toEqual(['secret', 'secrets']);
28+
});
29+
});
30+
31+
describe('describe', () => {
32+
it('should provide a description', () => {
33+
expect(secretCommands.describe).toBeDefined();
34+
});
35+
});
36+
37+
describe('builder', () => {
38+
beforeEach(() => {
39+
commandSpy.mockClear();
40+
demandCommandSpy.mockClear();
41+
});
42+
43+
const subcommands = [addSecret, deleteSecret, listSecret, updateSecret];
44+
45+
it('should demand the command takes one positional argument', () => {
46+
secretCommands.builder(yargs as Argv);
47+
48+
expect(demandCommandSpy).toHaveBeenCalledTimes(1);
49+
expect(demandCommandSpy).toHaveBeenCalledWith(1, '');
50+
});
51+
52+
it('should add the correct number of sub commands', () => {
53+
secretCommands.builder(yargs as Argv);
54+
expect(commandSpy).toHaveBeenCalledTimes(subcommands.length);
55+
});
56+
57+
it.each(subcommands)('should attach the %s subcommand', module => {
58+
secretCommands.builder(yargs as Argv);
59+
expect(commandSpy).toHaveBeenCalledWith(module);
60+
});
61+
});
62+
});

commands/hubdb.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
// @ts-nocheck
2-
const { addGlobalOptions } = require('../lib/commonOpts');
3-
const createCommand = require('./hubdb/create');
4-
const fetchCommand = require('./hubdb/fetch');
5-
const deleteCommand = require('./hubdb/delete');
6-
const clearCommand = require('./hubdb/clear');
7-
const { i18n } = require('../lib/lang');
1+
import { Argv } from 'yargs';
2+
import { addGlobalOptions } from '../lib/commonOpts';
3+
import * as createCommand from './hubdb/create';
4+
import * as fetchCommand from './hubdb/fetch';
5+
import * as deleteCommand from './hubdb/delete';
6+
import * as clearCommand from './hubdb/clear';
7+
import { i18n } from '../lib/lang';
88

99
const i18nKey = 'commands.hubdb';
1010

11-
exports.command = 'hubdb';
12-
exports.describe = i18n(`${i18nKey}.describe`);
11+
export const command = 'hubdb';
12+
export const describe = i18n(`${i18nKey}.describe`);
1313

14-
exports.builder = yargs => {
14+
export function builder(yargs: Argv): Argv {
1515
addGlobalOptions(yargs);
1616

1717
yargs
@@ -22,4 +22,4 @@ exports.builder = yargs => {
2222
.demandCommand(1, '');
2323

2424
return yargs;
25-
};
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import yargs, { Argv } from 'yargs';
2+
import {
3+
addAccountOptions,
4+
addConfigOptions,
5+
addUseEnvironmentOptions,
6+
} from '../../../lib/commonOpts';
7+
8+
jest.mock('yargs');
9+
jest.mock('../../../lib/commonOpts');
10+
11+
// Import this last so mocks apply
12+
import * as hubdbClearCommand from '../clear';
13+
14+
describe('commands/account/clean', () => {
15+
const yargsMock = yargs as Argv;
16+
17+
describe('command', () => {
18+
it('should have the correct command structure', () => {
19+
expect(hubdbClearCommand.command).toEqual('clear [table-id]');
20+
});
21+
});
22+
23+
describe('describe', () => {
24+
it('should provide a description', () => {
25+
expect(hubdbClearCommand.describe).toBeDefined();
26+
});
27+
});
28+
29+
describe('builder', () => {
30+
it('should support the correct options', () => {
31+
hubdbClearCommand.builder(yargsMock);
32+
33+
expect(addAccountOptions).toHaveBeenCalledTimes(1);
34+
expect(addAccountOptions).toHaveBeenCalledWith(yargsMock);
35+
36+
expect(addConfigOptions).toHaveBeenCalledTimes(1);
37+
expect(addConfigOptions).toHaveBeenCalledWith(yargsMock);
38+
39+
expect(addUseEnvironmentOptions).toHaveBeenCalledTimes(1);
40+
expect(addUseEnvironmentOptions).toHaveBeenCalledWith(yargsMock);
41+
});
42+
});
43+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import yargs, { Argv } from 'yargs';
2+
import {
3+
addAccountOptions,
4+
addConfigOptions,
5+
addUseEnvironmentOptions,
6+
} from '../../../lib/commonOpts';
7+
8+
jest.mock('yargs');
9+
jest.mock('../../../lib/commonOpts');
10+
11+
// Import this last so mocks apply
12+
import * as hubdbCreateCommand from '../create';
13+
14+
describe('commands/account/clean', () => {
15+
const yargsMock = yargs as Argv;
16+
17+
describe('command', () => {
18+
it('should have the correct command structure', () => {
19+
expect(hubdbCreateCommand.command).toEqual('create');
20+
});
21+
});
22+
23+
describe('describe', () => {
24+
it('should provide a description', () => {
25+
expect(hubdbCreateCommand.describe).toBeDefined();
26+
});
27+
});
28+
29+
describe('builder', () => {
30+
it('should support the correct options', () => {
31+
hubdbCreateCommand.builder(yargsMock);
32+
33+
expect(addAccountOptions).toHaveBeenCalledTimes(1);
34+
expect(addAccountOptions).toHaveBeenCalledWith(yargsMock);
35+
36+
expect(addConfigOptions).toHaveBeenCalledTimes(1);
37+
expect(addConfigOptions).toHaveBeenCalledWith(yargsMock);
38+
39+
expect(addUseEnvironmentOptions).toHaveBeenCalledTimes(1);
40+
expect(addUseEnvironmentOptions).toHaveBeenCalledWith(yargsMock);
41+
});
42+
});
43+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import yargs, { Argv } from 'yargs';
2+
import {
3+
addAccountOptions,
4+
addConfigOptions,
5+
addUseEnvironmentOptions,
6+
} from '../../../lib/commonOpts';
7+
8+
jest.mock('yargs');
9+
jest.mock('../../../lib/commonOpts');
10+
11+
// Import this last so mocks apply
12+
import * as hubdbDeleteCommand from '../delete';
13+
14+
describe('commands/account/clean', () => {
15+
const yargsMock = yargs as Argv;
16+
17+
describe('command', () => {
18+
it('should have the correct command structure', () => {
19+
expect(hubdbDeleteCommand.command).toEqual('delete [table-id]');
20+
});
21+
});
22+
23+
describe('describe', () => {
24+
it('should provide a description', () => {
25+
expect(hubdbDeleteCommand.describe).toBeDefined();
26+
});
27+
});
28+
29+
describe('builder', () => {
30+
it('should support the correct options', () => {
31+
hubdbDeleteCommand.builder(yargsMock);
32+
33+
expect(addAccountOptions).toHaveBeenCalledTimes(1);
34+
expect(addAccountOptions).toHaveBeenCalledWith(yargsMock);
35+
36+
expect(addConfigOptions).toHaveBeenCalledTimes(1);
37+
expect(addConfigOptions).toHaveBeenCalledWith(yargsMock);
38+
39+
expect(addUseEnvironmentOptions).toHaveBeenCalledTimes(1);
40+
expect(addUseEnvironmentOptions).toHaveBeenCalledWith(yargsMock);
41+
});
42+
});
43+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import yargs, { Argv } from 'yargs';
2+
import {
3+
addAccountOptions,
4+
addConfigOptions,
5+
addUseEnvironmentOptions,
6+
} from '../../../lib/commonOpts';
7+
8+
jest.mock('yargs');
9+
jest.mock('../../../lib/commonOpts');
10+
11+
// Import this last so mocks apply
12+
import * as hubdbFetchCommand from '../fetch';
13+
14+
describe('commands/account/clean', () => {
15+
const yargsMock = yargs as Argv;
16+
17+
describe('command', () => {
18+
it('should have the correct command structure', () => {
19+
expect(hubdbFetchCommand.command).toEqual('fetch [table-id] [dest]');
20+
});
21+
});
22+
23+
describe('describe', () => {
24+
it('should provide a description', () => {
25+
expect(hubdbFetchCommand.describe).toBeDefined();
26+
});
27+
});
28+
29+
describe('builder', () => {
30+
it('should support the correct options', () => {
31+
hubdbFetchCommand.builder(yargsMock);
32+
33+
expect(addAccountOptions).toHaveBeenCalledTimes(1);
34+
expect(addAccountOptions).toHaveBeenCalledWith(yargsMock);
35+
36+
expect(addConfigOptions).toHaveBeenCalledTimes(1);
37+
expect(addConfigOptions).toHaveBeenCalledWith(yargsMock);
38+
39+
expect(addUseEnvironmentOptions).toHaveBeenCalledTimes(1);
40+
expect(addUseEnvironmentOptions).toHaveBeenCalledWith(yargsMock);
41+
});
42+
});
43+
});

0 commit comments

Comments
 (0)