Skip to content

Commit

Permalink
✅ Add tests for readme file
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck committed Jun 9, 2019
1 parent 3a9d847 commit 0e18c2e
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
at: .
- run:
name: 'Run unit tests'
command: npm run test
command: npm run test:ci

workflows:
version: 2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1 align="center">Welcome to readme-md-generator 👋</h1>
<p>
<img src="https://img.shields.io/badge/version-0.1.0-blue.svg?cacheSeconds=2592000" />
<img src="https://img.shields.io/jsdelivr/npm/hm/readme-md-generator.svg">
<!-- <img src="https://img.shields.io/jsdelivr/npm/hm/readme-md-generator.svg"> -->
<a href="https://codecov.io/gh/kefranabg/readme-md-generator">
<img src="https://codecov.io/gh/kefranabg/readme-md-generator/branch/master/graph/badge.svg" />
</a>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"scripts": {
"lint": "eslint src/**",
"prettier:check": "prettier \"**/*.{js,md,json}\" --list-different",
"test": "jest --coverage && codecov"
"test": "jest",
"test:ci": "jest --coverage && codecov"
},
"repository": {
"type": "git",
Expand Down
14 changes: 8 additions & 6 deletions src/readme.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
const ejs = require('ejs')
const path = require('path')
const ora = require('ora')
const util = require('util')
const { promisify } = require('util')
const getYear = require('date-fns/get_year')
const readFile = util.promisify(require('fs').readFile)
const writeFile = util.promisify(require('fs').writeFile)
const fs = require('fs')

const README_PATH = 'README.md'

/**
* Create readme file from the given readmeContent
*
* @param {string} readmeContent
*/
const writeReadme = async readmeContent =>
await writeFile('README.md', readmeContent)
promisify(fs.writeFile)(README_PATH, readmeContent)

/**
* Get README template content from the given templatePath
Expand All @@ -23,7 +24,7 @@ const getReadmeTemplate = async templatePath => {
const spinner = ora('Loading README template').start()

try {
const template = await readFile(templatePath, 'utf8')
const template = await promisify(fs.readFile)(templatePath, 'utf8')
spinner.succeed('README template loaded')
return template
} catch (err) {
Expand Down Expand Up @@ -55,5 +56,6 @@ const buildReadmeContent = async (context, templateName) => {

module.exports = {
writeReadme,
buildReadmeContent
buildReadmeContent,
README_PATH
}
149 changes: 149 additions & 0 deletions src/readme.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
const fs = require('fs')
const ora = require('ora')

jest.mock('ora')

const { writeReadme, buildReadmeContent, README_PATH } = require('./readme')

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

await writeReadme(readmeContent)

expect(fs.writeFile).toHaveBeenCalledTimes(1)
expect(fs.writeFile.mock.calls[0][0]).toBe(README_PATH)
expect(fs.writeFile.mock.calls[0][1]).toBe(readmeContent)
})
})

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

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

const templateName = 'default'
const context = {
isGithubRepos: true,
repositoryUrl: 'https://github.com/kefranabg/readme-md-generator',
projectPrerequisites: ['npm >=5.5.0', 'node >=9.3.0'],
projectName: 'readme-md-generator',
projectVersion: '0.1.3',
projectDescription:
'Generates beautiful README files from git config & package.json infos',
projectDocumentationUrl:
'https://github.com/kefranabg/readme-md-generator#readme',
authorName: 'Franck Abgrall',
authorGithubUsername: 'kefranabg',
authorTwitterUsername: '',
licenseName: 'MIT',
licenseUrl:
'https://github.com/kefranabg/readme-md-generator/blob/master/LICENSE',
contributingUrl: 'https://github.com/kefranabg/readme-md-generator/issues'
}

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

it('should call ora with correct parameters in success case', async () => {
await buildReadmeContent(context, templateName)

expect(ora).toHaveBeenCalledTimes(1)
expect(ora).toHaveBeenCalledWith('Loading README template')
expect(succeed).toHaveBeenCalledTimes(1)
expect(succeed).toHaveBeenCalledWith('README template loaded')
})

it('should return readme template content', async () => {
const result = await buildReadmeContent(context, templateName)

expect(result)
.toEqual(`<h1 align="center">Welcome to readme-md-generator 👋</h1>
<p>
<img src="https://img.shields.io/badge/version-0.1.3-blue.svg?cacheSeconds=2592000" />
<a href="https://github.com/kefranabg/readme-md-generator#readme">
<img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" target="_blank" />
</a>
<a href="https://github.com/kefranabg/readme-md-generator/graphs/commit-activity">
<img alt="Maintenance" src="https://img.shields.io/badge/Maintained%3F-yes-green.svg" target="_blank" />
</a>
<a href="https://github.com/kefranabg/readme-md-generator/blob/master/LICENSE">
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" target="_blank" />
</a>
</p>
> Generates beautiful README files from git config &amp; package.json infos
## Prerequisites
- npm &gt;=5.5.0
- node &gt;=9.3.0
## Install
\`\`\`sh
npm i
\`\`\`
## Usage
\`\`\`sh
npm run start
\`\`\`
## Run tests
\`\`\`sh
npm run test
\`\`\`
## Contributing
Contributions, issues and feature requests are welcome. Feel free to check [issues page](https://github.com/kefranabg/readme-md-generator/issues) if you want to contribute.
## Author
👤 **Franck Abgrall**
* Github 👉 [@kefranabg](https://github.com/kefranabg)
## Show your support
Please ⭐️ this repository if you like it.
## License
Copyright © 2019 [Franck Abgrall](https://github.com/kefranabg).
📜 This project is [MIT](https://github.com/kefranabg/readme-md-generator/blob/master/LICENSE) licensed.
***
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_`)
})

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

try {
await buildReadmeContent(context, templateName)
// eslint-disable-next-line no-empty
} catch (err) {}

expect(ora).toHaveBeenCalledTimes(1)
expect(ora).toHaveBeenCalledWith('Loading README template')
expect(fail).toHaveBeenCalledTimes(1)
expect(fail).toHaveBeenCalledWith('README template loading fail')
})
})
})

0 comments on commit 0e18c2e

Please sign in to comment.