forked from vrn-deco/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { jest } from '@jest/globals' | ||
import { Command } from 'commander' | ||
import { Action, runAction } from '../action.js' | ||
|
||
type Name = string | ||
type HelloArguments = [Name] | ||
type HelloOptions = { welcome: boolean } | ||
|
||
const initFn = jest.fn() | ||
const execFn = jest.fn((name: Name, welcome = false) => { | ||
return `Hello, ${name}!${welcome ? ' Welcome!' : ''}` | ||
}) | ||
const clearFn = jest.fn() | ||
|
||
class HelloAction extends Action<HelloArguments, HelloOptions> { | ||
async initialize() { | ||
initFn() | ||
} | ||
async execute() { | ||
execFn(this.arguments[0], this.options.welcome) | ||
} | ||
async clear() { | ||
clearFn() | ||
} | ||
} | ||
|
||
describe('@vrn-deco/cli-command -> action.ts', () => { | ||
test('No environment variables will throw an exception', async () => { | ||
expect(runAction(HelloAction)('Hoyoe', { welcome: false }, new Command())).rejects.toThrow( | ||
'command sub package can be invoked', | ||
) | ||
}) | ||
|
||
test('Can run action', async () => { | ||
process.env.VRN_CLI_NAME = 'vrn-cli' | ||
process.env.VRN_CLI_PACKAGE_NAME = '@vrn-deco/cli' | ||
process.env.VRN_CLI_VERSION = '1.0.0' | ||
process.env.VRN_CLI_HOME_PATH = '/home/vrn' | ||
await expect(runAction(HelloAction)('Hoyoe', { welcome: false }, new Command())).resolves.not.toThrow() | ||
expect(initFn).toBeCalled() | ||
expect(execFn).toBeCalledWith('Hoyoe', false) | ||
expect(execFn).toReturnWith('Hello, Hoyoe!') | ||
expect(clearFn).toBeCalled() | ||
|
||
await expect(runAction(HelloAction)('Hoyoe', { welcome: true }, new Command())).resolves.not.toThrow() | ||
expect(execFn).toReturnWith('Hello, Hoyoe! Welcome!') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Command, prompt, registerCommands } from '../index.js' | ||
|
||
describe('@vrn-deco/cli-command -> index.ts', () => { | ||
test('Correct export', () => { | ||
expect(Command).toBeDefined() | ||
expect(prompt).toBeDefined() | ||
expect(registerCommands).toBeDefined() | ||
}) | ||
|
||
test('can register children to parent command', () => { | ||
const parent = new Command('parent') | ||
const child1 = new Command('child1') | ||
const child2 = new Command('child2') | ||
expect(registerCommands(parent, [child1, child2])).toBe(parent) | ||
}) | ||
}) |