Skip to content

Commit

Permalink
test(command): some test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Cphayim committed Feb 26, 2022
1 parent ae4018b commit 02271c4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
48 changes: 48 additions & 0 deletions packages/command/src/__tests__/action.spec.ts
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!')
})
})
16 changes: 16 additions & 0 deletions packages/command/src/__tests__/index.spec.ts
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)
})
})

0 comments on commit 02271c4

Please sign in to comment.