Skip to content

Add tests and refactor #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "badge-generator",
"description": "Online tool to help you quickly generate tailor-made badges/shields for your repo docs and learn to work with badges",
"main": "main.js",
"scripts": {
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/MichaelCurrin/badge-generator.git"
},
"author": "Michael Currin",
"license": "MIT",
"bugs": {
"url": "https://github.com/MichaelCurrin/badge-generator/issues"
},
"homepage": "https://github.com/MichaelCurrin/badge-generator#readme",
"devDependencies": {
"jest": "^26.0.1"
}
}
Empty file.
34 changes: 34 additions & 0 deletions src/__tests__/license.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { License } = require('../components/license.js')

test('MIT license badge title displays correctly', () => {
var license = new License('MIT')
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I made License its own object.

This is actually not that important to me - I had the app working fine as is for license.


expect(license.title()).toBe('License: MIT')
})

test('MIT license badge image is correct', () => {
var license = new License('MIT')

expect(license.img()).toBe('https://img.shields.io/badge/License-MIT-blue')
})

test('MIT license badge relative target is correct', () => {
var license = new License('MIT')

expect(license.REL_TARGET).toBe('#license')
})

test('MIT license full badge with relative target displays correctly', () => {
var license = new License('MIT')

var badge = '[![License: MIT](https://img.shields.io/badge/License-MIT-blue)](#license)';
expect(license.markdown()).toBe(badge)
})

test('MIT license full badge with FQDN target displays correctly', () => {
var target = 'https://github.com/my-user/my-repo-name/blob/master/LICENSE';
var license = new License('MIT', target)

var badge = '[![License: MIT](https://img.shields.io/badge/License-MIT-blue)](https://github.com/my-user/my-repo-name/blob/master/LICENSE)';
expect(license.markdown()).toBe(badge)
})
6 changes: 6 additions & 0 deletions src/__tests__/made-with-badge.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test('Made with Bash badge is correct', () => {
var badge = new MadeWith('Bash')

// Use mix of shield and markdown links internally. And preset target.
var result = '[![Made with Bash](https://img.shields.io/badge/Made_with-Bash-blue.svg)](https://www.gnu.org/software/bash/)'
})
36 changes: 36 additions & 0 deletions src/__tests__/markdown-link.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { MarkdownLink } = require('../components/markdownLink.js')

test('An ID markdown link formats correctly', () => {
var tag = MarkdownLink('My link', '#target')
expect(tag.md()).toBe('[My link](#target)')
})

test('An absolute markdown link formats correctly', () => {
var tag = MarkdownLink('Absolute link', '/foo/bar.md')
expect(tag.md()).toBe('[Absolute link](/foo/bar.md)')
})

test('A markdown URL link formats correctly', () => {
var tag = MarkdownLink('Example link', 'https://example.com')
expect(tag.md()).toBe('[Example link](https://example.com)')
})

test('A markdown image tag formats correctly', () => {
var tag = MarkdownLink('Example image', 'https://example.com/image.png')
expect(tag.image()).toBe('![Example image](https://example.com/image.png)')
})

test('A markdown image tag with target URL formats correctly', () => {
var tag = MarkdownLink('Example image', 'https://example.com/image.png', 'https://foo.bar.com')
expect(tag.image()).toBe('[![Example image](https://example.com/image.png)](https://foo.bar.com)')
})

test('An ID markdown link with alt text formats correctly', () => {
var tag = MarkdownLink('My link', '#target')
expect(tag.md("My alt test for hover")).toBe('[My link](#target "My alt test for hover")')
})

test('A markdown image tag with alt text and target URL formats correctly', () => {
var tag = MarkdownLink('Example image', 'https://example.com/image.png', 'https://foo.bar.com')
expect(tag.image("My alt test for hover")).toBe('[![Example image](https://example.com/image.png "My alt test for hover")](https://foo.bar.com)')
})
78 changes: 78 additions & 0 deletions src/__tests__/repo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const { parseRepoUrl, Repo } = require('../components/repo.js')

test("A repo URL can be split correctly", () => {
var url = 'https://github.com/my-username/my-repo-name'

expect(parseRepoUrl(url)).toBe({
username: 'my-username',
repoName: 'my-repo-name'
})
})

test("Create a repo instance", () => {
var repo = new Repo('my-username', 'my-repo-name')

expect(repo.username).toBe('my-username')
expect(repo.repoName).toBe('my-repo-name')
})

test("Create a repo instance by key-value pairs", () => {
var repo = new Repo({ username: 'my-username', repoName: 'my-repo-name' })

expect(repo.username).toBe('my-username')
expect(repo.repoName).toBe('my-repo-name')
})

test('Get Github URL for a repo', () => {
var repo = new Repo('my-user', 'my-repo-name')

expect(repo.githubUrl()).toBe('https://github.com/my-user/my-repo-name')
})

test('Get repo-based Github Pages URL for a repo', () => {
var repo = new Repo('my-user', 'my-repo-name')

expect(repo.githubPagesUrl()).toBe('https://my-user.github.io/my-repo-name')
})

test('Get user-based Github Pages URL for a repo', () => {
var repo = new Repo('my-user')

expect(repo.githubPagesUrl()).toBe('https://my-user.github.io/')
})

test('Get correct full license URL for a repo', () => {
var repo = new Repo('my-user', 'my-repo-name')

expect(repo.licenseUrl()).toBe('https://github.com/my-user/my-repo-name/blob/master/LICENSE')
})

test('Get generate template URL for a repo', () => {
var repo = new Repo('my-user', 'my-repo-name')

expect(repo.template()).toBe('https://github.com/my-user/my-repo-name/template')
})

test('Get tags URL for a repo', () => {
var repo = new Repo('my-user', 'my-repo-name')

expect(repo.tags()).toBe('https://github.com/my-user/my-repo-name/tags')
})

test('Get releases URL for a repo', () => {
var repo = new Repo('my-user', 'my-repo-name')

expect(repo.releases()).toBe('https://github.com/my-user/my-repo-name/releases')
})

test('Get forks URL for a repo', () => {
var repo = new Repo('my-user', 'my-repo-name')

expect(repo.forks()).toBe('https://github.com/my-user/my-repo-name/network/members')
})

test('Get stars URL for a repo', () => {
var repo = new Repo('my-user', 'my-repo-name')

expect(repo.stars()).toBe('https://github.com/my-user/my-repo-name/stargazers')
})
32 changes: 32 additions & 0 deletions src/components/license.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class License {
DOMAIN = 'https://img.shields.io'
REL_TARGET = '#license'
DEFAULT_COLOR = 'blue'

constructor(type, target = null, color = null) {
this.type = type

this.target = target || this.REL_TARGET
this.color = color || this.DEFAULT_COLOR
}

title() {
return `License: ${this.type}`
}

img() {
return `${this.DOMAIN}/badge/License-${this.type}-${this.color}`
}

fileUrl() {
return ``
}

markdown() {
return `[![${this.title()}](${this.img()})](${this.target})`
}
}

module.exports = {
License,
}
19 changes: 19 additions & 0 deletions src/components/repo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Repo {
constructor(username, repoName) {
this.username = username
this.repoName = repoName
}

url() {
return `https://github.com/${this.username}/${this.repoName}`
}

licenseUrl() {
return `${this.url()}/blob/master/LICENSE`
}
}


module.exports = {
Repo
}