Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Url shortner
beforeEach(function () {
this.callback = sinon.spy();
// Stubbing the ajax method
sinon.stub($, 'ajax', function (options) {
// Creating a deffered object
var dfd = $.Deferred();
// assigns success callback to done.
if(options.success) dfd.done(options.success({status_code: 200, data: {url: "bit.ly/aaaa"}}));
// assigns error callback to fail.
if(options.error) dfd.fail(options.error);
dfd.success = dfd.done;
dfd.error = dfd.fail;
// returning the deferred object so that we can chain it.
return dfd;
});
});
afterEach(function () {
$.ajax.restore();
});
(function(exports, $){
exports.shorten = Url.shorten;
}(typeof exports === 'undefined'? this['mymodule']={}: exports, $));
var assert = require('assert'),
sinon = require('sinon'),
$ = require('jquery'),
url = require('url');
if(typeof(exports) !== "undefined"){
var $ = require('jquery');
}
it("return false when url is not passed", function () {
assert.equal(false,url.shorten(""));
assert.equal(false,url.shorten());
assert.equal(false,url.shorten(null));
});
it("call the ajax once", function (done) {
// This will execute the method assigned to Deferred.done
url.shorten("http://google.com", this.callback).resolve();
// sinon will check whether the ajax method is called Once
assert($.ajax.calledOnce);
done();
});
it("yeild success", function (done) {
url.shorten("http://google.com", this.callback).resolve();
// sinon will check whether the success method is called Once
assert(this.callback.withArgs(0,"bit.ly/aaaa").calledOnce);
done();
});
it("yields error", function (done) {
// This will execute the method assigned to Deferred.fail
url.shorten("http://google.com", this.callback).reject();
// sinon will check whether the error method is called Once
assert(this.callback.withArgs(-1, "ajaxFailed").calledOnce);
done();
});
var Url = {
shorten: function (url,callback) {
if(["",null,undefined].indexOf(url) >= 0) return false;
var api_url = "http://api.bit.ly/v3/shorten";
var params = {
format: 'json',
longUrl: url,
login: 'revathskumar',
apiKey: ''
};
return $.ajax({
type: 'GET',
url: api_url ,
data: params,
dataType: 'json',
success: function(data, status_param) {
var status = data.status_code;
console.log(status);
if(status == 200) {
callback(status, data.data.url);
} else {
callback(status, data.status_txt);
}
},
error: function (request, status, error) {
callback(-1, "ajaxFailed");
}
});
}
};
if(typeof(exports) !== "undefined"){
var $ = require('jquery');
}
var Url = {
shorten: function (url,callback) {
if(["",null,undefined].indexOf(url) >= 0) return false;
var api_url = "http://api.bit.ly/v3/shorten";
var params = {
format: 'json',
longUrl: url,
login: 'revathskumar',
apiKey: ''
};
return $.ajax({
type: 'GET',
url: api_url ,
data: params,
dataType: 'json',
success: function(data, status_param) {
var status = data.status_code;
console.log(status);
if(status == 200) {
callback(status, data.data.url);
} else {
console.log(status);
callback(status, data.status_txt);
}
},
error: function (request, status, error) {
callback(-1, "ajaxFailed");
}
});
}
};
(function(exports, $){
exports.shorten = Url.shorten;
}(typeof exports === 'undefined'? this['mymodule']={}: exports, $));
var assert = require('assert'),
sinon = require('sinon'),
$ = require('jquery'),
url = require('../js/backbone/lib/url');
describe('Url', function(){
describe(".shorten", function(){
beforeEach(function () {
this.callback = sinon.spy();
sinon.stub($, 'ajax', function (options) {
var dfd = $.Deferred();
if(options.success) dfd.done(options.success({status_code: 200, data: {url: "bit.ly/aaaa"}}));
if(options.error) dfd.fail(options.error);
dfd.success = dfd.done;
dfd.error = dfd.fail;
return dfd;
});
});
afterEach(function () {
$.ajax.restore();
});
it("return false when url is not passed", function () {
assert.equal(false,url.shorten(""));
assert.equal(false,url.shorten());
assert.equal(false,url.shorten(null));
});
it("call the ajax once", function (done) {
url.shorten("http://google.com", this.callback).resolve();
assert($.ajax.calledOnce);
done();
});
it("yeild success", function (done) {
url.shorten("http://google.com", this.callback).resolve();
assert(this.callback.withArgs(0,"bit.ly/aaaa").calledOnce);
done();
});
it("yields error", function (done) {
url.shorten("http://google.com", this.callback).reject();
assert(this.callback.withArgs(-1, "ajaxFailed").calledOnce);
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment