Skip to content

Commit 847b487

Browse files
committed
Added unit test for functions
TODO: e2e testing and UI unit test
1 parent 36613d6 commit 847b487

File tree

8 files changed

+510
-159
lines changed

8 files changed

+510
-159
lines changed

package-lock.json

Lines changed: 339 additions & 67 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"dependencies": {
1717
"bootstrap": "^4.0.0-beta.2",
1818
"bootstrap-vue": "^1.4.1",
19+
"chai-as-promised": "^7.1.1",
20+
"github-username-regex": "^1.0.0",
21+
"karma-chrome-launcher": "^2.2.0",
1922
"vue": "^2.5.2",
2023
"vue-resource": "^1.3.5",
2124
"vue-router": "^3.0.1"
@@ -39,7 +42,7 @@
3942
"copy-webpack-plugin": "^4.0.1",
4043
"cross-env": "^5.0.1",
4144
"cross-spawn": "^5.0.1",
42-
"css-loader": "^0.28.0",
45+
"css-loader": "^0.28.9",
4346
"eslint": "^4.15.0",
4447
"eslint-config-standard": "^10.2.1",
4548
"eslint-friendly-formatter": "^3.0.0",
@@ -55,14 +58,15 @@
5558
"html-webpack-plugin": "^2.30.1",
5659
"inject-loader": "^3.0.0",
5760
"karma": "^1.4.1",
61+
"karma-chai-as-promised": "^0.1.2",
5862
"karma-coverage": "^1.1.1",
5963
"karma-mocha": "^1.3.0",
6064
"karma-phantomjs-launcher": "^1.0.2",
6165
"karma-phantomjs-shim": "^1.4.0",
6266
"karma-sinon-chai": "^1.3.1",
6367
"karma-sourcemap-loader": "^0.3.7",
6468
"karma-spec-reporter": "0.0.31",
65-
"karma-webpack": "^2.0.2",
69+
"karma-webpack": "^2.0.9",
6670
"mocha": "^3.2.0",
6771
"nightwatch": "^0.9.12",
6872
"node-notifier": "^5.1.2",
@@ -79,12 +83,13 @@
7983
"shelljs": "^0.7.6",
8084
"sinon": "^4.0.0",
8185
"sinon-chai": "^2.8.0",
86+
"style-loader": "^0.19.1",
8287
"uglifyjs-webpack-plugin": "^1.1.1",
8388
"url-loader": "^0.5.8",
8489
"vue-loader": "^13.3.0",
8590
"vue-style-loader": "^3.0.1",
8691
"vue-template-compiler": "^2.5.2",
87-
"webpack": "^3.6.0",
92+
"webpack": "^3.10.0",
8893
"webpack-bundle-analyzer": "^2.9.0",
8994
"webpack-dev-server": "^2.9.1",
9095
"webpack-merge": "^4.1.0"

src/components/GitFriend.vue

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<template>
2+
<div>
3+
<input type="text" id="username" v-model="username" placeholder="github username"/>
4+
<b-btn @click="findFriends" variant="info" class="m-1">
5+
Find Friends
6+
</b-btn>
7+
<br/>
8+
<br/>
9+
<div v-if="error" id="error">
10+
<div>{{errorMessage}}</div>
11+
</div>
12+
<div v-else-if="friends.length > 0" id="friend-list">
13+
<div v-for="friend in friends"
14+
:key="friend.login">
15+
{{friend.login}}
16+
</div>
17+
</div>
18+
19+
<div v-if="loading" id="loading">
20+
Searching Git Friends....
21+
</div>
22+
</div>
23+
</template>
24+
25+
<script>
26+
27+
import githubUsernameRegex from 'github-username-regex'
28+
export default {
29+
name: 'HelloWorld',
30+
data () {
31+
return {
32+
msg: 'Welcome to Your Vue.js App',
33+
username: '',
34+
error: false,
35+
errorMessage: '',
36+
loading: false,
37+
friends: [],
38+
githubURL: 'https://api.github.com/users/'
39+
}
40+
},
41+
methods: {
42+
// find a friend that u follow and follow u
43+
matchFriends (followers, following) {
44+
var followersHash = {}
45+
for (var i = 0; i < followers.length; i++) {
46+
followersHash[followers[i].login] = followers[i]
47+
}
48+
// use the hash table above to find out if u follow ur follower.
49+
var friends = following.filter(function (each) {
50+
if (followersHash[each.login]) {
51+
return true
52+
}
53+
}, followersHash)
54+
return friends
55+
},
56+
validUsername (username) {
57+
return githubUsernameRegex.test(username)
58+
},
59+
async checkUserExists (username) {
60+
try {
61+
let user = await this.$http.get(this.githubURL + this.username)
62+
if (user.data.login) {
63+
return true
64+
}
65+
return false
66+
} catch (error) {
67+
return false
68+
}
69+
},
70+
// main function to request api call and find out your git friends
71+
async findFriends () {
72+
this.error = false
73+
this.errorMessage = ''
74+
this.loading = true
75+
76+
// Check if username belongs to github
77+
if (!this.validUsername(this.username)) {
78+
this.error = true
79+
this.errorMessage = 'This username seems wrong. Try again'
80+
this.loading = false
81+
return
82+
}
83+
84+
// check if the user exists
85+
let userExists = await this.checkUserExists(this.username)
86+
if (!userExists) {
87+
this.error = true
88+
this.errorMessage = 'Sorry, this user does not exists. You should go and get that username.'
89+
this.loading = false
90+
return
91+
}
92+
93+
// Get followers and following
94+
const followingAppend = '/following'
95+
const followersAppend = '/followers'
96+
let following = await this.$http.get(this.githubURL + this.username + followingAppend)
97+
let followers = await this.$http.get(this.githubURL + this.username + followersAppend)
98+
99+
this.friends = this.matchFriends(followers.data, following.data)
100+
if (this.friends.length < 1) {
101+
this.error = true
102+
this.errorMessage = 'Sorry to hear that you have no friend. add me @soechun'
103+
}
104+
this.loading = false
105+
}
106+
}
107+
}
108+
</script>
109+
110+
<!-- Add "scoped" attribute to limit CSS to this component only -->
111+
<style scoped>
112+
h1, h2 {
113+
font-weight: normal;
114+
}
115+
ul {
116+
list-style-type: none;
117+
padding: 0;
118+
}
119+
li {
120+
display: inline-block;
121+
margin: 0 10px;
122+
}
123+
a {
124+
color: #42b983;
125+
}
126+
</style>

src/components/HelloWorld.vue

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/router/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Vue from 'vue'
22
import Router from 'vue-router'
3-
import HelloWorld from '@/components/HelloWorld'
3+
import HelloWorld from '@/components/GitFriend'
44

55
Vue.use(Router)
66

test/unit/karma.conf.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,19 @@ module.exports = function karmaConfig (config) {
1111
// 1. install corresponding karma launcher
1212
// http://karma-runner.github.io/0.13/config/browsers.html
1313
// 2. add it to the `browsers` array below.
14-
browsers: ['PhantomJS'],
15-
frameworks: ['mocha', 'sinon-chai', 'phantomjs-shim'],
14+
browsers: ['Chrome'],
15+
frameworks: ['mocha', 'sinon-chai','phantomjs-shim'],
16+
plugins: [
17+
require('karma-mocha'),
18+
require('karma-webpack'),
19+
require('karma-sourcemap-loader'),
20+
require('karma-sinon-chai'),
21+
require('karma-phantomjs-shim'),
22+
require('karma-spec-reporter'),
23+
require('karma-coverage'),
24+
require('karma-phantomjs-launcher'),
25+
require('karma-chrome-launcher')
26+
],
1627
reporters: ['spec', 'coverage'],
1728
files: ['./index.js'],
1829
preprocessors: {

test/unit/specs/GitFriend.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import GitFriend from '@/components/GitFriend'
2+
3+
describe('GitFriend.vue', () => {
4+
it('should return correct array that contains elements present on both side', () => {
5+
expect(GitFriend.methods.matchFriends(
6+
[{login: 1}, {login: 3}, {login: 2}],
7+
[{login: 3}, {login: 2}])).deep.equal([{login: 3}, {login: 2}])
8+
})
9+
10+
it('should validate the username is acceptable to github', () => {
11+
expect(GitFriend.methods.validUsername('soechun')).to.equal(true)
12+
expect(GitFriend.methods.validUsername('#')).to.equal(false)
13+
})
14+
15+
it('should check if the user exists', () => {
16+
GitFriend.methods.checkUserExists('soechun').then(function (result) {
17+
result.should.equal(true)
18+
})
19+
GitFriend.methods.checkUserExists('12312123123123123').then(function (result) {
20+
result.should.equal(false)
21+
})
22+
})
23+
})

test/unit/specs/HelloWorld.spec.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)