Skip to content

Commit

Permalink
Add repo fetch in github library
Browse files Browse the repository at this point in the history
Also add support for promises in test library
  • Loading branch information
leobenkel committed May 27, 2017
1 parent 7465f5f commit c54d8c4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
20 changes: 20 additions & 0 deletions scripts/githubAPI/githubConnector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
define(['jquery', 'lodash'], function($) {
var Github = function() {
var rootURL = 'https://api.github.com';
var organizationName = 'The-Brains';

this.getRepos = function() {
return $.getJSON(`${rootURL}/orgs/The-Brains/repos`);
};

this.getReposNames = function() {
return this.getRepos().then(function(data) {
return _.map(data, function(repo) {
return _.replace(repo.full_name, `${organizationName}/`, '');
});
})
}
};

return Github;
});
3 changes: 2 additions & 1 deletion scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ requirejs.config({
baseUrl: 'scripts/lib',
paths: {
chai: 'chai',
jquery: 'https://code.jquery.com/jquery-3.2.1.slim.min',
jquery: 'https://code.jquery.com/jquery-3.2.1.min',
lodash: 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min',
seedRandom: 'https://cdnjs.cloudflare.com/ajax/libs/seedrandom/2.4.3/seedrandom.min',
},
Expand All @@ -18,6 +18,7 @@ define(function() {
// Start loading the main app file. Put all of
// your application logic in there.
requirejs([
'scripts/test/githubAPI-githubConnector-test.js',
'scripts/test/util-find-get-param-test.js',
]);
});
22 changes: 22 additions & 0 deletions scripts/test/githubAPI-githubConnector-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
define(
['chai', './test-wrapper.js', '../githubAPI/githubConnector.js'],
function(chai, testWrapper, GithubConnector) {
var expect = chai.expect;
var mainName = 'githubAPI-githubConnector';

testWrapper.execTest(mainName, 'should return correct GET values', function() {
var github = new GithubConnector();
return github.getReposNames()
.then(function(data) {
expect(data).to.include('Ideas');
expect(data).to.include('ChaosGame');
expect(data).to.include('Wolfensheep');
expect(data).to.include('the-brains.github.io');
expect(data).to.include('database-storage');
expect(data).to.include('GithubDB');
expect(data).to.include('TestSuite');
return Promise.resolve();
});
});
}
);
42 changes: 31 additions & 11 deletions scripts/test/test-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ define([
this.grepSearch = FindGetParam('grep');
this.failOnly = !!$.parseJSON(FindGetParam('failOnly'));

var isPromise = function(thing) {
return thing && thing.then && thing.catch;
}

this.execTest = function(mainName, testName, testFn) {
var myself = this;
var validTest = true;
Expand All @@ -36,17 +40,7 @@ define([
return;
}

setTimeout(function() {
var startTime = new Date();
var succeed = null;
var errorMsg = null;
try {
testFn();
succeed = true;
} catch (error) {
succeed = false;
errorMsg = error;
}
var completeTest = function(startTime, succeed, errorMsg) {
var timeSpent = new Date() - startTime;

if ((myself.failOnly && !succeed) || !myself.failOnly) {
Expand All @@ -63,6 +57,32 @@ define([
myself.updateCounters(succeed);
myself.renderTest(succeed, mainName, testName, timeSpent, errorMsg);
}
}

setTimeout(function() {
var startTime = new Date();
var succeed = null;
var errorMsg = null;
try {
var returnedThing = testFn();

if (isPromise(returnedThing)) {
returnedThing.then(function() {
completeTest(startTime, true, null);
})
.catch(function(error) {
completeTest(startTime, false, error);
});
return;
}

succeed = true;
} catch (error) {
succeed = false;
errorMsg = error;
}

completeTest(startTime, succeed, errorMsg);
}, 0);
}

Expand Down

0 comments on commit c54d8c4

Please sign in to comment.