Skip to content

Commit

Permalink
🐛 Execute default function to get default answer when -y flag is pass…
Browse files Browse the repository at this point in the history
…ed (#185)
  • Loading branch information
anku255 authored and kefranabg committed Nov 15, 2019
1 parent d96e310 commit 89cd82d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/ask-questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = async (projectInfos, useDefaultAnswers) => {
)

const answersContext = useDefaultAnswers
? utils.getDefaultAnswers(questions)
? await utils.getDefaultAnswers(questions)
: await inquirer.prompt(questions)

return {
Expand Down
19 changes: 11 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ const getPackageJson = async () => {
*
* @param {Object} question
*/
const getDefaultAnswer = (question, answersContext) => {
const getDefaultAnswer = async (question, answersContext) => {
if (question.when && !question.when(answersContext)) return undefined

switch (question.type) {
case 'input':
return question.default || ''
return typeof question.default === 'function'
? question.default(answersContext)
: question.default || ''
case 'checkbox':
return question.choices
.filter(choice => choice.checked)
Expand Down Expand Up @@ -110,13 +112,14 @@ const isProjectAvailableOnNpm = projectName => {
* @param {Array} questions
*/
const getDefaultAnswers = questions =>
questions.reduce(
(answersContext, question) => ({
questions.reduce(async (answersContextProm, question) => {
const answersContext = await answersContextProm

return {
...answersContext,
[question.name]: getDefaultAnswer(question, answersContext)
}),
{}
)
[question.name]: await getDefaultAnswer(question, answersContext)
}
}, Promise.resolve({}))

/**
* Clean social network username by removing the @ prefix and
Expand Down
28 changes: 14 additions & 14 deletions src/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,45 +120,45 @@ describe('utils', () => {
})

describe('getDefaultAnswer', () => {
it('should handle input prompts correctly', () => {
it('should handle input prompts correctly', async () => {
const question = { type: 'input', default: 'default' }
const result = getDefaultAnswer(question)
const result = await getDefaultAnswer(question)
expect(result).toEqual(question.default)
})

it('should handle choices prompts correctly', () => {
it('should handle choices prompts correctly', async () => {
const value = { name: 'name', value: 'value' }
const question = {
type: 'checkbox',
choices: [{ value, checked: true }, { checked: false }]
}
const result = getDefaultAnswer(question)
const result = await getDefaultAnswer(question)

expect(result).toEqual([value])
})

it('should return empty string for non-defaulted fields', () => {
it('should return empty string for non-defaulted fields', async () => {
const question = { type: 'input' }
const result = getDefaultAnswer(question)
const result = await getDefaultAnswer(question)

expect(result).toEqual('')
})

it('should return undefined for invalid types', () => {
it('should return undefined for invalid types', async () => {
const question = { type: 'invalid' }
const result = getDefaultAnswer(question)
const result = await getDefaultAnswer(question)

expect(result).toEqual(undefined)
})

it('should return undefined if when function is defined and return false', () => {
it('should return undefined if when function is defined and return false', async () => {
const answersContext = {}
const question = {
type: 'input',
when: ansewersContext => !isNil(ansewersContext.licenseUrl)
}

const result = getDefaultAnswer(question, answersContext)
const result = await getDefaultAnswer(question, answersContext)

expect(result).toEqual(undefined)
})
Expand All @@ -177,22 +177,22 @@ describe('utils', () => {
})
})

it('should return correct value if when function is defined and return true', () => {
it('should return correct value if when function is defined and return true', async () => {
const answersContext = { licenseUrl: 'licenseUrl' }
const question = {
type: 'input',
default: 'default',
when: ansewersContext => !isNil(ansewersContext.licenseUrl)
}

const result = getDefaultAnswer(question, answersContext)
const result = await getDefaultAnswer(question, answersContext)

expect(result).toEqual('default')
})
})

describe('getDefaultAnswers', () => {
it('should return default answers from questions', () => {
it('should return default answers from questions', async () => {
const questions = [
{
type: 'input',
Expand All @@ -206,7 +206,7 @@ describe('utils', () => {
}
]

const result = getDefaultAnswers(questions)
const result = await getDefaultAnswers(questions)

expect(result).toEqual({
questionOne: 'answer 1',
Expand Down

0 comments on commit 89cd82d

Please sign in to comment.