Skip to content

Commit

Permalink
✅ Add missing test for cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck committed Jun 11, 2019
1 parent 20005b6 commit cdd7df6
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/cli.spec.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
const inquirer = require('inquirer')
const cli = require('./cli')
const projectInfos = require('./project-infos')
const readme = require('./readme')
const utils = require('./utils')
const questions = require('./questions')

const realAskQuestions = cli.askQuestions

inquirer.prompt = jest.fn(([question]) =>
Promise.resolve({ [question.name]: 'value' })
)

jest.mock('./questions', () => ({
askProjectName: jest.fn(() => ({ name: 'askProjectName' })),
askProjectVersion: jest.fn(() => ({ name: 'askProjectVersion' })),
askProjectDescription: jest.fn(() => ({ name: 'askProjectDescription' }))
}))

describe('cli', () => {
describe('mainProcess', () => {
const answersContext = { projectName: 'readme-md-generator' }

beforeAll(() => {
cli.askQuestions = jest.fn(() => Promise.resolve(answersContext))
})

afterAll(() => {
cli.askQuestions = realAskQuestions
})

it('should call main functions with correct args', async () => {
const template = 'default'
const projectInformations = { name: 'readme-md-generator' }
const readmeContent = 'content'
const answersContext = { projectName: 'readme-md-generator' }
projectInfos.getProjectInfos = jest.fn(() =>
Promise.resolve(projectInformations)
)
cli.askQuestions = jest.fn(() => Promise.resolve(answersContext))
readme.buildReadmeContent = jest.fn(() => Promise.resolve(readmeContent))
readme.writeReadme = jest.fn()
utils.showEndMessage = jest.fn()
Expand All @@ -33,4 +55,31 @@ describe('cli', () => {
expect(utils.showEndMessage).toHaveBeenCalledTimes(1)
})
})

describe('askQuestions', () => {
it('should call all builder functions exported by questions', async () => {
const projectInfos = { name: 'readme-md-generator' }

await cli.askQuestions(projectInfos)

expect(questions.askProjectName).toHaveBeenCalledTimes(1)
expect(questions.askProjectVersion).toHaveBeenCalledTimes(1)
expect(questions.askProjectDescription).toHaveBeenCalledTimes(1)
})

it('should return merged contexts', async () => {
const projectInfos = { name: 'readme-md-generator' }

const context = await cli.askQuestions(projectInfos)

expect(context).toEqual({
askProjectName: 'value',
askProjectVersion: 'value',
askProjectDescription: 'value',
isGithubRepos: undefined,
repositoryUrl: undefined,
projectPrerequisites: undefined
})
})
})
})

0 comments on commit cdd7df6

Please sign in to comment.