Last active
December 14, 2015 10:19
-
-
Save revathskumar/5071581 to your computer and use it in GitHub Desktop.
Url shortner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(exports, $){ | |
exports.shorten = Url.shorten; | |
}(typeof exports === 'undefined'? this['mymodule']={}: exports, $)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert'), | |
sinon = require('sinon'), | |
$ = require('jquery'), | |
url = require('url'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(typeof(exports) !== "undefined"){ | |
var $ = require('jquery'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
}); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, $)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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