Skip to content

Commit

Permalink
chore(tests): improve test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Benitte committed Apr 3, 2016
1 parent c24be7e commit 7e01887
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 50 deletions.
17 changes: 11 additions & 6 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "mozaik-ext-github",
"version": "1.1.0",
"version": "1.2.0",
"description": "Mozaik github widgets",
"repository": {
"type": "git",
Expand Down Expand Up @@ -47,16 +47,17 @@
"eslint": "2.2.0",
"eslint-plugin-react": "4.2.2",
"mockery": "1.4.1",
"nock": "7.7.2",
"nyc": "6.1.1",
"react": "^0.13.3",
"sinon": "1.17.3"
},
"peerDependencies": {
"mozaik": ">=1.0.11",
"mozaik": ">=1.1.0",
"react": "^0.13.3"
},
"scripts": {
"eslint": "eslint --ext .js --ext .jsx ./src/*",
"eslint": "eslint --ext .js --ext .jsx ./src/** ./test/**",
"test": "ava",
"test-cover": "nyc ava",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
Expand All @@ -68,14 +69,18 @@
]
},
"ava": {
"babel": "inherit",
"files": [
"test/**/*.test.js"
],
"tap": false,
"failFast": true,
"require": [
"babel-register"
]
],
"babel": "inherit"
},
"nyc": {
"extension": [
".js",
".jsx"
]
}
Expand Down
40 changes: 21 additions & 19 deletions src/client.js
Expand Up @@ -8,29 +8,30 @@ import config from './config';
/**
* @param {Mozaik} mozaik
*/
const client = function (mozaik) {
const client = mozaik => {

mozaik.loadApiConfig(config);

function buildApiRequest(path, params) {
let url = config.get('github.baseUrl');
let req = request.get(url + path);
const buildApiRequest = (path, params) => {
const url = config.get('github.baseUrl');
const req = request.get(`${url}${path}`);

mozaik.logger.info(chalk.yellow(`[github] calling ${ url + path } ${ JSON.stringify(params) }`));
const paramsDebug = params ? ` ${JSON.stringify(params)}` : '';
mozaik.logger.info(chalk.yellow(`[github] calling ${url}${path}${paramsDebug}`));

if (params) {
req.query(params);
}

if (config.get('github.token') !== '') {
req.set('Authorization', `token ${ config.get('github.token') }`);
req.set('Authorization', `token ${config.get('github.token')}`);
}

return req.promise();
}
};

function repositoryCommits(params, buffer) {
return buildApiRequest(`/repos/${ params.repository }/commits`, params)
const repositoryCommits = (params, buffer) => {
return buildApiRequest(`/repos/${params.repository}/commits`, params)
.then(res => {
buffer.commits = buffer.commits.concat(res.body);

Expand All @@ -44,31 +45,31 @@ const client = function (mozaik) {
}
})
;
}
};

const apiCalls = {
organization(params) {
return buildApiRequest(`/orgs/${ params.organization }`)
return buildApiRequest(`/orgs/${params.organization}`)
.then(res => res.body)
;
},

user(params) {
return buildApiRequest(`/users/${ params.user }`)
return buildApiRequest(`/users/${params.user}`)
.then(res => res.body)
;
},

pullRequests(params) {
return buildApiRequest(`/repos/${ params.repository }/pulls`)
return buildApiRequest(`/repos/${params.repository}/pulls`)
.then(res => res.body)
;
},

// Be warned that this API call can be heavy enough
// because it loads each branch details with an extra call
branches(params) {
return buildApiRequest(`/repos/${ params.repository }/branches`)
return buildApiRequest(`/repos/${params.repository}/branches`)
.then(res => {
return Promise.all(res.body.map(branch => {
return apiCalls.branch(_.extend({ branch: branch.name }, params));
Expand All @@ -78,13 +79,13 @@ const client = function (mozaik) {
},

branch(params) {
return buildApiRequest(`/repos/${ params.repository }/branches/${ params.branch }`)
return buildApiRequest(`/repos/${params.repository}/branches/${params.branch}`)
.then(res => res.body)
;
},

repositoryContributorsStats(params) {
return buildApiRequest(`/repos/${ params.repository }/stats/contributors`)
return buildApiRequest(`/repos/${params.repository}/stats/contributors`)
.then(res => res.body)
;
},
Expand All @@ -102,7 +103,7 @@ const client = function (mozaik) {
},

issues(params) {
return buildApiRequest(`/repos/${ params.repository }/issues`)
return buildApiRequest(`/repos/${params.repository}/issues`)
.then(res => res.body)
;
},
Expand All @@ -115,7 +116,7 @@ const client = function (mozaik) {
});

return Promise.all(params.labels.map(label => {
return buildApiRequest(`/repos/${ params.repository }/issues`, {
return buildApiRequest(`/repos/${params.repository}/issues`, {
labels: label.name,
state: 'open',
filter: 'all'
Expand All @@ -133,7 +134,7 @@ const client = function (mozaik) {
const url = 'https://status.github.com/api/last-message.json';
let req = request.get(url);

mozaik.logger.info(chalk.yellow(`[github] calling ${ url }`));
mozaik.logger.info(chalk.yellow(`[github] calling ${url}`));

return req.promise()
.then(res => res.body)
Expand All @@ -144,4 +145,5 @@ const client = function (mozaik) {
return apiCalls;
};


export default client;
6 changes: 3 additions & 3 deletions src/components/PullRequest.jsx
Expand Up @@ -3,14 +3,14 @@ import React, { Component, PropTypes } from 'react';

class PullRequest extends Component {
render() {
const { pullRequest } = this.props;
const { pullRequest: { title, user} } = this.props;

return (
<div className="list__item github__pull-request">
<span className="github__pull-request__avatar">
<img src={pullRequest.user.avatar_url} />
<img src={user.avatar_url} />
</span>
{pullRequest.title}
{title}
</div>
);
}
Expand Down
15 changes: 15 additions & 0 deletions test/chalk-mock.js
@@ -0,0 +1,15 @@
/**
* Used to remove chalk formatting to be able
* to make proper comparison in tests.
*/
const chalkMock = {
yellow(log) {
return log;
},
red(log) {
return log;
}
};


export default chalkMock;

0 comments on commit 7e01887

Please sign in to comment.