forked from wejendorp/angular-superagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
136 additions
and
128 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,114 @@ | ||
require('angular-superagent'); | ||
|
||
var assert = require('assert'); | ||
var chai = require('chai'); | ||
var expect = chai.expect; | ||
var assert = chai.assert; | ||
|
||
describe('Request', function() { | ||
var RequestProvider; | ||
|
||
// Initialize module | ||
beforeEach(module('ngSuperagent')); | ||
beforeEach(module(function(_RequestProvider_) { | ||
RequestProvider = _RequestProvider_; | ||
})); | ||
beforeEach(inject(function() {})); | ||
|
||
var server; | ||
var methods = ['get', 'head', 'del', 'put', 'post', 'patch']; | ||
|
||
before(function () { | ||
server = sinon.fakeServer.create(); | ||
server.fakeHTTPMethods = true; | ||
}); | ||
after(function () { server.restore(); }); | ||
|
||
beforeEach(function() { | ||
methods.forEach(function(m) { | ||
respond(m.toUpperCase(), '/api/v2', 200, {status: 'ok'}); | ||
}); | ||
function respond(method, path, status, body) { | ||
server.respondWith(method, path, JSON.stringify(body)); | ||
} | ||
}); | ||
|
||
|
||
it('should expose RequestProvider', function() { | ||
assert(RequestProvider); | ||
}); | ||
it('should expose Request service', function() { | ||
inject(function(Request) { | ||
assert(Request !== null); | ||
}); | ||
}); | ||
|
||
describe('when unconfigured', function() { | ||
// Module config step | ||
beforeEach(angular.mock.module(function(_RequestProvider_) { | ||
RequestProvider = _RequestProvider_; | ||
})); | ||
// Module run step | ||
beforeEach(angular.mock.inject(function(_Request_) { | ||
Request = _Request_; | ||
})); | ||
|
||
it('has correct defaults', function() { | ||
assert(RequestProvider.defaults.baseUrl === ''); | ||
assert(!RequestProvider.defaults.baseUrl); | ||
}); | ||
it('should expose RequestProvider', function() { | ||
assert(RequestProvider); | ||
}); | ||
it('should expose Request service', function() { | ||
assert(Request !== null); | ||
}); | ||
}); | ||
|
||
|
||
describe('when given a baseUrl', function() { | ||
beforeEach(function() { | ||
RequestProvider.defaults.baseUrl = '/api/v2'; | ||
}); | ||
beforeEach(inject(function(_Request_) { | ||
// Module config step | ||
beforeEach(angular.mock.module(function(RequestProvider) { | ||
RequestProvider.defaults.baseUrl = '/test'; | ||
})); | ||
// Module run step | ||
beforeEach(angular.mock.inject(function(_Request_) { | ||
Request = _Request_; | ||
})); | ||
|
||
methods.forEach(function(m) { | ||
it('#'+m + ' is redirected to the api', function() { | ||
it('all methods redirected to the api', function(done) { | ||
methods.forEach(function(m) { | ||
var cb = sinon.spy(); | ||
Request[m]('').end(cb); | ||
server.respond(); | ||
sinon.assert.calledOnce(cb); | ||
sinon.assert.calledWithMatch(cb, null, {body: {status: 'ok'}}); | ||
Request[m]('/random').end(function(err, res) { | ||
assert(!err); | ||
expect(res.status).to.eql(200); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
// describe('#promise', function() { | ||
// var cb; | ||
// function validate() { | ||
// server.respond(); | ||
// sinon.assert.calledOnce(cb); | ||
// } | ||
// beforeEach(function() { | ||
// cb = sinon.spy(); | ||
// }); | ||
|
||
// it('is a $q promise', function() { | ||
// var p = Request.get('/ping').promise(); | ||
// expect(p.then).to.be.a('function'); | ||
// expect(p.catch).to.be.a('function'); | ||
// expect(p.finally).to.be.a('function'); | ||
describe('#promise', function() { | ||
var cb; | ||
|
||
// p.finally(cb); | ||
// validate(); | ||
// }); | ||
// it('is resolved on HTTP 200 ok', function() { | ||
// Request.get('/ping').promise().then(cb); | ||
// validate(); | ||
// }); | ||
// it('is rejected on HTTP 404', function() { | ||
// Request.get('/notfound').promise().catch(cb); | ||
// validate(); | ||
// }); | ||
// it('is rejected on body.status !== "ok"', function() { | ||
// Request.get('/error').promise().catch(cb); | ||
// validate(); | ||
// }); | ||
// it('resolves promise with body.data', function() { | ||
// Request.get('/data').promise().then(cb); | ||
// // function(res) { | ||
// // // console.log(res.success); | ||
// // }); | ||
// // server.respond(); | ||
// validate(); | ||
// cb.should.have.been.calledWithMatch(testData); | ||
// }); | ||
// }); | ||
it('is a $q promise', function() { | ||
var p = Request.get('/ping').promise(); | ||
expect(p.then).to.be.a('function'); | ||
expect(p.catch).to.be.a('function'); | ||
expect(p.finally).to.be.a('function'); | ||
}); | ||
describe('#addResolve', function() { | ||
// Module config step | ||
beforeEach(angular.mock.module(function(RequestProvider) { | ||
RequestProvider.addResolver(function(res) { | ||
if(res.error || !res.body) return this.reject(res); | ||
if(res.body.status === 'ok') return this.resolve(res); | ||
return this.reject(res); | ||
}); | ||
})); | ||
// Module run step | ||
beforeEach(angular.mock.inject(function(_Request_) { | ||
Request = _Request_; | ||
})); | ||
it('is resolved on HTTP 200 ok', function(done) { | ||
Request.get('/test/ping').promise().then(function(res) { | ||
expect(res.status).to.eql(200); | ||
done(); | ||
}); | ||
}); | ||
it('is rejected on HTTP 404', function(done) { | ||
Request.get('/notfound').promise().catch(function(res) { | ||
expect(res.status).to.eql(404); | ||
done(); | ||
}); | ||
}); | ||
it('is rejected on HTTP 500', function(done) { | ||
Request.get('/error').promise().catch(function() { | ||
done(); | ||
}); | ||
}); | ||
it('is rejected on body.status !== "ok"', function(done) { | ||
Request.get('/test/error').promise().catch(function(res) { | ||
expect(res.body).not.to.eql({status: 'ok'}); | ||
done(); | ||
}); | ||
}); | ||
it('is rejected on CORS error', function(done) { | ||
Request.get('http://mojn.com').promise().then(function() { | ||
done(new Error('Not rejected')); | ||
}, function() { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |