Skip to content

Commit

Permalink
🔊 Add logs in writeReadme
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck committed Jun 12, 2019
1 parent 0038201 commit 772d536
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
13 changes: 11 additions & 2 deletions src/readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ const README_PATH = 'README.md'
*
* @param {string} readmeContent
*/
const writeReadme = async readmeContent =>
promisify(fs.writeFile)(README_PATH, readmeContent)
const writeReadme = async readmeContent => {
const spinner = ora('Creating README').start()

try {
await promisify(fs.writeFile)(README_PATH, readmeContent)
spinner.succeed('README created')
} catch (err) {
spinner.fail('README creation fail')
throw err
}
}

/**
* Get README template content from the given templatePath
Expand Down
53 changes: 43 additions & 10 deletions src/readme.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,50 @@ jest.mock('ora')
const { writeReadme, buildReadmeContent, README_PATH } = require('./readme')

describe('readme', () => {
const succeed = jest.fn()
const fail = jest.fn()

ora.mockReturnValue({
start: () => ({
succeed,
fail
})
})

afterEach(() => {
jest.clearAllMocks()
})

describe('writeReadme', () => {
it('should call ora with correct parameters in success case', async () => {
const readmeContent = 'content'
fs.writeFile = jest.fn((path, content, cb) => cb(null, 'done'))

await writeReadme(readmeContent)

expect(ora).toHaveBeenCalledTimes(1)
expect(ora).toHaveBeenCalledWith('Creating README')
expect(succeed).toHaveBeenCalledTimes(1)
expect(succeed).toHaveBeenCalledWith('README created')
})

it('should call ora with correct parameters in fail case', async () => {
const readmeContent = 'content'
fs.writeFile = jest.fn(() => {
throw new Error('error')
})

try {
await writeReadme(readmeContent)
// eslint-disable-next-line no-empty
} catch (err) {}

expect(ora).toHaveBeenCalledTimes(1)
expect(ora).toHaveBeenCalledWith('Creating README')
expect(fail).toHaveBeenCalledTimes(1)
expect(fail).toHaveBeenCalledWith('README creation fail')
})

it('should call writeFile with correct parameters', async () => {
const readmeContent = 'content'
fs.writeFile = jest.fn((path, content, cb) => cb(null, 'done'))
Expand All @@ -20,16 +63,6 @@ describe('readme', () => {
})

describe('buildReadmeContent', () => {
const succeed = jest.fn()
const fail = jest.fn()

ora.mockReturnValue({
start: () => ({
succeed,
fail
})
})

const templateName = 'default'
const context = {
isGithubRepos: true,
Expand Down

0 comments on commit 772d536

Please sign in to comment.