Skip to content

Commit 18c7c15

Browse files
Merge pull request #3 from makeshift-array/unit-test
Unit test
2 parents 258b311 + a4d2e93 commit 18c7c15

File tree

18 files changed

+1174
-15
lines changed

18 files changed

+1174
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
npm-debug.log
55
*.sublime-project
66
*.sublime-workspace
7+
*.DS_Store

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,16 @@ A list of features in this kit.
5252
* Font Support
5353
* Supported but no tooling, only copies from one folder to another.
5454
* Unit Test Support
55-
* TODO: [Ava](https://www.npmjs.com/package/ava), Maybe?
55+
* Karma test suite ([karma](https://www.npmjs.com/package/karma))
56+
* [karma-phantomjs-launcher](https://www.npmjs.com/package/karma-phantomjs-launcher)
57+
* Jasmin framework ([jasmin](https://www.npmjs.com/package/jasmine))
58+
* [jasmine-ajax](https://github.com/jasmine/jasmine-ajax)
59+
* [jasmine-jquery](https://www.npmjs.com/package/jasmine-jquery)
60+
* Test coverage
61+
* [karma-spec-reporter](https://www.npmjs.com/package/karma-spec-reporter)
62+
* [karma-coverage](https://www.npmjs.com/package/karma-coverage)
63+
* [istanbul](https://www.npmjs.com/package/istanbul)
64+
* [isparta-loader](https://www.npmjs.com/package/isparta-loader)
5665
* Utility Support
5766
* Conditional Pipe Control ([gulp-if](https://www.npmjs.com/package/gulp-if))
5867
* Pipe Cleaner ([gulp-plumber](https://www.npmjs.com/package/gulp-plumber))

__tests__/Github-test.js

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import 'babel-polyfill'
2+
import $ from 'jquery'
3+
import 'jasmine-jquery'
4+
import 'jasmine-ajax'
5+
import { ok200, notFound404 } from './__helpers__/GithubResponses'
6+
import { GitHub } from '../src/js/modules/Github'
7+
8+
jasmine.getFixtures().fixturesPath = 'base/src/modules'
9+
10+
describe('Github.js - instantiation', () => {
11+
let github = null
12+
13+
beforeEach(() => {
14+
github = new GitHub('jfusco')
15+
})
16+
17+
afterEach(() => {
18+
github = null
19+
})
20+
21+
it('exists in DOM', () => {
22+
loadFixtures('github.html')
23+
24+
expect($('.github')).toBeInDOM()
25+
})
26+
27+
it('should error if no username is passed in', () => {
28+
// Test undefined
29+
expect((() => {
30+
new GitHub()
31+
})
32+
).toThrowError(Error)
33+
34+
// Test empty string
35+
expect((() => {
36+
new GitHub('')
37+
})
38+
).toThrowError(Error)
39+
})
40+
41+
it('should set this.el to jQuery object', () => {
42+
expect(github.el instanceof $).toBe(true)
43+
})
44+
45+
it('should set a username', () => {
46+
expect(github.username).toBe('jfusco')
47+
})
48+
})
49+
50+
describe('Github.js - ajax', () => {
51+
const github = new GitHub('DennisMartinez')
52+
53+
let request
54+
55+
beforeEach(done => {
56+
jasmine.Ajax.install()
57+
58+
github.getUserData()
59+
60+
request = jasmine.Ajax.requests.mostRecent()
61+
request.respondWith(ok200)
62+
63+
done()
64+
});
65+
66+
afterEach(() => {
67+
jasmine.Ajax.uninstall();
68+
})
69+
70+
it('sends the request to the right end point', () => {
71+
expect(request.url).toBe('https://api.github.com/users/DennisMartinez')
72+
});
73+
74+
it('uses the correct method', () => {
75+
expect(request.method).toBe('GET')
76+
});
77+
78+
it('should return the correct data', () => {
79+
const data = JSON.parse(request.responseText)
80+
81+
expect(typeof data).toBe('object')
82+
expect(request.status).toBe(200)
83+
expect(data.name).toBe('Dennis Martinez')
84+
expect(data.company).toBe('Verndale')
85+
})
86+
})
87+
88+
describe('Github.js - render', () => {
89+
let github = null,
90+
gitHubAvatar,
91+
gitHubInfo
92+
93+
const data = JSON.parse(ok200.responseText)
94+
95+
beforeEach(() => {
96+
loadFixtures('github.html')
97+
98+
github = new GitHub('DennisMartinez')
99+
github.render(data)
100+
101+
gitHubAvatar = $('.github-avatar')
102+
gitHubInfo = $('.github-info')
103+
})
104+
105+
afterEach(() => {
106+
github = null
107+
gitHubAvatar = null
108+
gitHubInfo = null
109+
})
110+
111+
describe('avatar', () => {
112+
it('should exist', () => {
113+
expect(gitHubAvatar).toBeInDOM()
114+
})
115+
116+
it('should render avatar attributes', () => {
117+
const $avatar = gitHubAvatar.find('img')
118+
119+
expect($avatar.attr('src')).toBe(data.avatar_url)
120+
expect($avatar.attr('alt')).toBe(data.name)
121+
})
122+
})
123+
124+
describe('info', () => {
125+
it('should exist', () => {
126+
expect(gitHubInfo).toBeInDOM()
127+
})
128+
129+
it('should render user info', () => {
130+
const $gitHubLink = gitHubInfo.find('a')
131+
const $company = gitHubInfo.find('li:nth-child(2)')
132+
const $email = gitHubInfo.find('li:nth-child(3)')
133+
const $bio = gitHubInfo.find('p')
134+
135+
expect($gitHubLink.attr('href')).toBe(data.html_url)
136+
expect($gitHubLink.text()).toBe(data.html_url)
137+
138+
expect($company.text()).toBe(`Company: ${data.company}`)
139+
expect($email.text()).toBe(`Email: ${data.email}`)
140+
expect($bio.text()).toBe(data.bio)
141+
})
142+
})
143+
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// 200
2+
export const ok200 = {
3+
status: 200,
4+
responseText: JSON.stringify({
5+
avatar_url: "https://avatars1.githubusercontent.com/u/1375616?v=3",
6+
bio: "mek a d weeb fun",
7+
blog: null,
8+
company: "Verndale",
9+
created_at: "2012-01-24T16:19:15Z",
10+
11+
events_url: "https://api.github.com/users/DennisMartinez/events{/privacy}",
12+
followers: 13,
13+
followers_url: "https://api.github.com/users/DennisMartinez/followers",
14+
following: 15,
15+
following_url: "https://api.github.com/users/DennisMartinez/following{/other_user}",
16+
gists_url: "https://api.github.com/users/DennisMartinez/gists{/gist_id}",
17+
gravatar_id: "",
18+
hireable: true,
19+
html_url: "https://github.com/DennisMartinez",
20+
id: 1375616,
21+
location: "CO",
22+
login: "DennisMartinez",
23+
name: "Dennis Martinez",
24+
organizations_url: "https://api.github.com/users/DennisMartinez/orgs",
25+
public_gists: 20,
26+
public_repos: 10,
27+
received_events_url: "https://api.github.com/users/DennisMartinez/received_events",
28+
repos_url: "https://api.github.com/users/DennisMartinez/repos",
29+
site_admin: false,
30+
starred_url: "https://api.github.com/users/DennisMartinez/starred{/owner}{/repo}",
31+
subscriptions_url: "https://api.github.com/users/DennisMartinez/subscriptions",
32+
type: "User",
33+
updated_at: "2017-03-25T16:42:02Z",
34+
url: "https://api.github.com/users/DennisMartinez"
35+
})
36+
}
37+
38+
// 404
39+
export const notFound404 = {
40+
status: 404,
41+
responseText: JSON.stringify({
42+
documentation_url: "https://developer.github.com/v3",
43+
message: "Not Found"
44+
})
45+
}

0 commit comments

Comments
 (0)