-
Notifications
You must be signed in to change notification settings - Fork 579
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
1 parent
634b748
commit d6c3582
Showing
5 changed files
with
237 additions
and
124 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
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
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
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 |
---|---|---|
@@ -1,67 +1,41 @@ | ||
'use strict'; | ||
|
||
module.exports = class Helper { | ||
constructor(command) { | ||
this.command = command; | ||
this.inquirer = command.inquirer; | ||
this.originPrompt = this.inquirer.prompt; | ||
this.KEY_UP = '\u001b[A'; | ||
this.KEY_DOWN = '\u001b[B'; | ||
this.KEY_LEFT = '\u001b[D'; | ||
this.KEY_RIGHT = '\u001b[C'; | ||
this.KEY_ENTER = '\n'; | ||
this.KEY_SPACE = ' '; | ||
const Command = require('../lib/command'); | ||
|
||
class TestCommand extends Command { | ||
|
||
constructor() { | ||
super(); | ||
this.showPrompt = false; | ||
} | ||
|
||
mockPrompt(arr) { | ||
this.promptAction = arr; | ||
} | ||
|
||
/** | ||
* send keys after `inquirer.prompt` trigger | ||
* | ||
* @param {Array} actions - each item will be sent after once call, if item is array, then send sub item after a tick | ||
* | ||
* @example | ||
* mock([ helper.KEY_DOWN + helper.KEY_DOWN, [ 'test', 'this is xxx', 'TZ' ]]); | ||
*/ | ||
mock(actions) { | ||
this.inquirer.prompt = opts => { | ||
const result = this.originPrompt.call(this.inquirer, opts); | ||
const key = actions.shift() || '\n'; | ||
if (key) { | ||
if (Array.isArray(opts) && !Array.isArray(key)) { | ||
throw new Error(`prompt multiple question, but mock with only one key \`${key}\`, should provide array`); | ||
} else { | ||
this.sendKey(key); | ||
async beforePromptSubmit() { | ||
this.prompt.once('run', async () => { | ||
const value = this.promptAction.shift(); | ||
if (value) { | ||
for (const flag of value) { | ||
if (Array.isArray(flag)) { | ||
await this.prompt.keypress.apply(this.prompt, flag); | ||
} else if (typeof flag === 'string') { | ||
try { | ||
for (const key of flag.split('')) { | ||
await this.prompt.keypress(key); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
} | ||
} | ||
return result; | ||
}; | ||
} | ||
|
||
/** | ||
* restore prompt to origin fn | ||
*/ | ||
restore() { | ||
this.inquirer.prompt = this.originPrompt; | ||
await this.prompt.submit(); | ||
this.prompt.close(); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* send key to process.stdin | ||
* | ||
* @param {String/Array} arr - key list, send one by one after a tick | ||
* @return {Promise} after all sent | ||
*/ | ||
sendKey(arr) { | ||
if (Array.isArray(arr)) { | ||
return arr.reduce((promise, key) => { | ||
return promise.then(() => this.sendKey(key)); | ||
}, Promise.resolve()); | ||
} else { | ||
const key = arr; | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
process.stdin.emit('data', key + '\n'); | ||
resolve(key); | ||
}, 10); | ||
}); | ||
} | ||
} | ||
}; | ||
exports.TestCommand = TestCommand; |
Oops, something went wrong.