Permalink
Browse files

Add Dashboard.GitHubDataSource and corresponding tests

  • Loading branch information...
pangratz committed Jul 22, 2012
1 parent 6c2ec96 commit 26d869bb0a7fd0ae646fc4eab88ffc0a419d74b6
Showing with 107 additions and 0 deletions.
  1. +32 −0 app/lib/github_data_source.js
  2. +75 −0 app/tests/github_data_source_test.js
@@ -0,0 +1,32 @@
require('dashboard/core');
Dashboard.GitHubDataSource = Ember.Object.extend({
PREFIX: 'https://api.github.com',
ajax: function(url, target, callback) {
Ember.$.ajax({
url: this.PREFIX + url,
dataType: 'jsonp',
context: this,
success: function(data) {
this._ajaxSuccess(target, callback, data);
}
});
},
_ajaxSuccess: function(target, callback, response) {
this._updateLimits(response);
Ember.tryInvoke(target, callback, [response.data]);
},
_updateLimits: function(response) {
if (response.meta) {
this.set('remaining', response.meta['X-RateLimit-Remaining']);
this.set('limit', response.meta['X-RateLimit-Limit']);
}
},
watchedRepositories: function(username, target, callback) {
this.ajax('/users/%@/watched'.fmt(username), target, callback);
}
});
@@ -0,0 +1,75 @@
require('dashboard/github_data_source');
var get = Ember.get;
var dataSource;
var setupDataSource = {
setup: function() {
dataSource = Dashboard.GitHubDataSource.create();
},
teardowm: function() {
dataSource.destroy();
}
};
module("Dashboard.GitHubDataSource", setupDataSource);
test("is defined", function() {
ok(Dashboard.GitHubDataSource, "is defined");
});
test("_ajaxSuccess invokes callback on target", function() {
var callbackCalled, callbackThis, callbackResponse;
var response = {
meta: {
'X-RateLimit-Remaining': 42,
'X-RateLimit-Limit': 100
},
data: {
a: 1,
b: 'hello'
}
};
var target = {
callback: function(response) {
callbackCalled = true;
callbackThis = this;
callbackResponse = response;
}
};
dataSource._ajaxSuccess(target, 'callback', response);
equal(get(dataSource, 'remaining'), 42, "remaining has been read from repsonse.meta and set on the dataSource");
equal(get(dataSource, 'limit'), 100, "limit has been read from repsonse.meta and set on the dataSource");
ok(callbackCalled, 'callback has been called');
deepEqual(callbackThis, target, "this inside called callback is set to target");
deepEqual(callbackResponse, {a: 1, b: 'hello'}, "data of response is passed to callback");
});
module("Dashboard.GitHubDataSource#watchedRepositories", setupDataSource);
test("invokes ajax", function() {
var ajaxCalled, ajaxParams;
var ajaxUrl, ajaxTarget, ajaxCallback;
dataSource.reopen({
ajax: function(url, target, callback) {
ajaxCalled = true;
ajaxParams = arguments;
ajaxUrl = url;
ajaxTarget = target;
ajaxCallback = callback;
}
});
var target = {};
dataSource.watchedRepositories('buster', target, 'callback');
ok(ajaxCalled, 'ajax has been called');
deepEqual(ajaxUrl, '/users/buster/watched', "the passed url is correct");
deepEqual(ajaxTarget, target, "the target has been passed");
deepEqual(ajaxCallback, 'callback', "the callback has been passed");
});

0 comments on commit 26d869b

Please sign in to comment.