UNPKG

1.91 kBJavaScriptView Raw
1'use strict';
2var events = require('events');
3var _ = require('lodash');
4var inquirer = require('inquirer');
5var sinon = require('sinon');
6var Promise = require('pinkie-promise');
7
8function DummyPrompt(answers, q) {
9 this.answers = answers;
10 this.question = q;
11}
12
13DummyPrompt.prototype.run = function() {
14 var answer = this.answers[this.question.name];
15 var isSet;
16
17 switch (this.question.type) {
18 case 'list':
19 // List prompt accepts any answer value including null
20 isSet = answer !== undefined;
21 break;
22 case 'confirm':
23 // Ensure that we don't replace `false` with default `true`
24 isSet = answer || answer === false;
25 break;
26 default:
27 // Other prompts treat all falsy values to default
28 isSet = Boolean(answer);
29 }
30
31 if (!isSet) {
32 answer = this.question.default;
33
34 if (answer === undefined && this.question.type === 'confirm') {
35 answer = true;
36 }
37 }
38
39 return Promise.resolve(answer);
40};
41
42function TestAdapter(answers) {
43 answers = answers || {};
44 this.promptModule = inquirer.createPromptModule();
45
46 Object.keys(this.promptModule.prompts).forEach(function(promptName) {
47 this.promptModule.registerPrompt(promptName, DummyPrompt.bind(DummyPrompt, answers));
48 }, this);
49
50 this.diff = sinon.spy();
51 this.log = sinon.spy();
52 _.extend(this.log, events.EventEmitter.prototype);
53
54 // Make sure all log methods are defined
55 [
56 'write',
57 'writeln',
58 'ok',
59 'error',
60 'skip',
61 'force',
62 'create',
63 'invoke',
64 'conflict',
65 'identical',
66 'info',
67 'table'
68 ].forEach(function(methodName) {
69 this.log[methodName] = sinon.stub().returns(this.log);
70 }, this);
71}
72
73TestAdapter.prototype.prompt = function(questions, cb) {
74 var promise = this.promptModule(questions);
75 promise.then(cb || _.noop);
76
77 return promise;
78};
79
80module.exports = {
81 DummyPrompt: DummyPrompt,
82 TestAdapter: TestAdapter
83};