Creator & PromptModuleAPI tests

This commit is contained in:
Evan You
2018-01-03 15:30:26 -05:00
parent 25e1d2f394
commit d69b7d50c7
4 changed files with 168 additions and 0 deletions

View File

@@ -47,6 +47,15 @@ exports.clearConsole = title => {
}
}
// make all logs except error noop during tests
if (process.env.VUE_CLI_TEST) {
Object.keys(exports).forEach(key => {
if (key !== 'error') {
exports[key] = () => {}
}
})
}
exports.hasYarn = (() => {
if (process.env.VUE_CLI_TEST) {
return true

View File

@@ -0,0 +1,47 @@
let pendingAssertions
exports.prompt = prompts => {
if (!pendingAssertions) {
throw new Error(`inquirer was mocked and used without pending assertions: ${prompts}`)
}
const answers = {}
let skipped = 0
prompts.forEach((prompt, i) => {
if (prompt.when && !prompt.when(answers)) {
skipped++
return
}
const a = pendingAssertions[i - skipped]
if (a.message) {
expect(prompt.message).toContain(a.message)
}
if (a.choices) {
expect(prompt.choices.length).toBe(a.choices.length)
a.choices.forEach((c, i) => {
expect(prompt.choices[i].name).toContain(a.choices[i])
})
}
if (a.choose != null) {
expect(prompt.type).toBe('list')
answers[prompt.name] = prompt.choices[a.choose].value
}
if (a.check != null) {
expect(prompt.type).toBe('checkbox')
answers[prompt.name] = a.check.map(i => prompt.choices[i].value)
}
if (a.confirm != null) {
expect(prompt.type).toBe('confirm')
answers[prompt.name] = a.confirm
}
})
expect(prompts.length).toBe(pendingAssertions.length + skipped)
pendingAssertions = null
return Promise.resolve(answers)
}
exports.expectPrompts = assertions => {
pendingAssertions = assertions
}

View File

@@ -0,0 +1,112 @@
jest.mock('fs')
jest.mock('inquirer')
const Creator = require('../Creator')
const { defaults } = require('../options')
const { expectPrompts } = require('inquirer') // from mock
it('default', async () => {
const creator = new Creator('test', '/', [])
expectPrompts([
{
message: 'project creation mode',
choices: [
'Zero-config',
'Manually select'
],
choose: 0
}
])
const options = await creator.promptAndResolveOptions()
expect(options).toEqual(defaults)
})
it('manual + PromptModuleAPI', async () => {
const creator = new Creator('test', '/', [
api => {
api.injectFeature({
name: 'Foo',
value: 'foo'
})
api.injectFeature({
name: 'Bar',
value: 'bar'
})
api.injectPrompt({
name: 'customFoo',
message: 'customFoo',
when: answers => answers.features.includes('foo'),
type: 'confirm'
})
api.injectPrompt({
name: 'customBar',
message: 'customBar',
when: answers => answers.features.includes('bar'),
type: 'list',
choices: []
})
api.injectOptionForPrompt('customBar', {
name: 'barChoice',
value: 'barChoice'
})
api.onPromptComplete((answers, options) => {
if (answers.features.includes('bar')) {
options.plugins.bar = {}
}
if (answers.customBar === 'barChoice') {
options.plugins.barChoice = {}
}
})
}
])
expectPrompts([
{ choose: 1 },
{
message: 'Check the features',
choices: ['Foo', 'Bar'],
check: [1]
},
{
message: 'customBar',
choices: ['barChoice'],
choose: 0
},
{
message: 'package manager',
choices: ['Yarn', 'NPM'],
choose: 0
},
{
message: 'Save the preferences',
confirm: true
}
])
const options = await creator.promptAndResolveOptions()
const expectedOptions = {
packageManager: 'yarn',
plugins: {
bar: {},
barChoice: {}
}
}
expect(options).toEqual(expectedOptions)
// should be saved now
expectPrompts([
{
choices: [
'Use previously saved',
'Zero-config',
'Manually'
],
choose: 0
}
])
const newCreator = new Creator('test', '/', [])
const newOptions = await newCreator.promptAndResolveOptions()
expect(newOptions).toEqual(expectedOptions)
})