diff --git a/lib/ask.js b/lib/ask.js index f2c3c9ef60..cdecba5226 100644 --- a/lib/ask.js +++ b/lib/ask.js @@ -32,11 +32,9 @@ module.exports = function ask (prompts, data, done) { */ function prompt (data, key, prompt, done) { - // skip prompts whose when condition is not met - if (prompt.when && !evaluate(prompt.when, data)) { + if (prompt.when && !(typeof prompt.when === 'function' ? prompt.when(data) : evaluate(prompt.when, data))) { return done() } - var promptDefault = prompt.default if (typeof prompt.default === 'function') { promptDefault = function () { diff --git a/test/e2e/mock-metadata-repo-js/meta.js b/test/e2e/mock-metadata-repo-js/meta.js index 8da267a811..fcb01ab8e8 100644 --- a/test/e2e/mock-metadata-repo-js/meta.js +++ b/test/e2e/mock-metadata-repo-js/meta.js @@ -12,6 +12,19 @@ module.exports = { validate: function (input) { return input === 'custom' ? 'can not input `custom`' : true } + }, + q1: { + type: 'list', + required: true, + label: 'Which choice?', + choices: ['a1', 'a2'] + }, + q2: { + when: function (answers) { + return answers.q1 === 'a1' + }, + label: 'Which choice', + type: 'confirm' } }, helpers: { diff --git a/test/e2e/test.js b/test/e2e/test.js index 704808ecfa..2698b82c02 100644 --- a/test/e2e/test.js +++ b/test/e2e/test.js @@ -11,6 +11,7 @@ const extend = Object.assign || require('util')._extend const generate = require('../../lib/generate') const metadata = require('../../lib/options') const { isLocalPath, getTemplatePath } = require('../../lib/local-path') +const ask = require('../../lib/ask') const MOCK_META_JSON_PATH = path.resolve('./test/e2e/mock-meta-json') const MOCK_TEMPLATE_REPO_PATH = path.resolve('./test/e2e/mock-template-repo') @@ -225,7 +226,23 @@ describe('vue-cli', () => { expect(getTemplatePath('../template')).to.equal(path.join(__dirname, '/../../../template')) }) - it.only('points out the file in the error', done => { + it('"when" set function', () => { + var meta = require(path.join(MOCK_METADATA_REPO_JS_PATH, 'meta.js')) + + var collectQa1 = extend({}, answers, { q1: 'a1', q2: 'aq2' }) + monkeyPatchInquirer(collectQa1) + var data = {} + ask(meta.prompts, data, function () { + expect(data).to.have.property('q2') + }) + var data2 = {} + var collectQa2 = extend({}, answers, { q1: 'a2', q2: 'aq2' }) + monkeyPatchInquirer(collectQa2) + ask(meta.prompts, data2, function () { + expect(data2).to.not.have.property('q2') + }) + }) + it('points out the file in the error', done => { monkeyPatchInquirer(answers) generate('test', MOCK_ERROR, MOCK_TEMPLATE_BUILD_PATH, err => { expect(err.message).to.match(/^\[readme\.md\] Parse error/)