2011-07-17 08:05:43 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
2011-02-15 06:12:45 +00:00
|
|
|
describe('$xhr.cache', function() {
|
2011-07-22 19:56:45 +00:00
|
|
|
var scope, $browser, $browserXhr, $xhrErr, cache, log;
|
2011-02-15 06:12:45 +00:00
|
|
|
|
2011-07-22 19:56:45 +00:00
|
|
|
beforeEach(function() {
|
2011-03-23 16:33:29 +00:00
|
|
|
scope = angular.scope(angularService, {'$xhr.error': $xhrErr = jasmine.createSpy('$xhr.error')});
|
2011-02-15 06:12:45 +00:00
|
|
|
$browser = scope.$service('$browser');
|
|
|
|
|
$browserXhr = $browser.xhr;
|
|
|
|
|
cache = scope.$service('$xhr.cache');
|
|
|
|
|
log = '';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
afterEach(function() {
|
2011-02-15 06:12:45 +00:00
|
|
|
dealoc(scope);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function callback(code, response) {
|
|
|
|
|
expect(code).toEqual(200);
|
|
|
|
|
log = log + toJson(response) + ';';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
it('should cache requests', function() {
|
2011-02-15 06:12:45 +00:00
|
|
|
$browserXhr.expectGET('/url').respond('first');
|
|
|
|
|
cache('GET', '/url', null, callback);
|
|
|
|
|
$browserXhr.flush();
|
|
|
|
|
|
|
|
|
|
$browserXhr.expectGET('/url').respond('ERROR');
|
|
|
|
|
cache('GET', '/url', null, callback);
|
|
|
|
|
$browser.defer.flush();
|
|
|
|
|
expect(log).toEqual('"first";"first";');
|
|
|
|
|
|
|
|
|
|
cache('GET', '/url', null, callback, false);
|
|
|
|
|
$browser.defer.flush();
|
|
|
|
|
expect(log).toEqual('"first";"first";"first";');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
it('should first return cache request, then return server request', function() {
|
2011-02-15 06:12:45 +00:00
|
|
|
$browserXhr.expectGET('/url').respond('first');
|
|
|
|
|
cache('GET', '/url', null, callback, true);
|
|
|
|
|
$browserXhr.flush();
|
|
|
|
|
|
|
|
|
|
$browserXhr.expectGET('/url').respond('ERROR');
|
|
|
|
|
cache('GET', '/url', null, callback, true);
|
|
|
|
|
$browser.defer.flush();
|
|
|
|
|
expect(log).toEqual('"first";"first";');
|
|
|
|
|
|
|
|
|
|
$browserXhr.flush();
|
|
|
|
|
expect(log).toEqual('"first";"first";"ERROR";');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
it('should serve requests from cache', function() {
|
2011-02-15 06:12:45 +00:00
|
|
|
cache.data.url = {value:'123'};
|
|
|
|
|
cache('GET', 'url', null, callback);
|
|
|
|
|
$browser.defer.flush();
|
|
|
|
|
expect(log).toEqual('"123";');
|
|
|
|
|
|
|
|
|
|
cache('GET', 'url', null, callback, false);
|
|
|
|
|
$browser.defer.flush();
|
|
|
|
|
expect(log).toEqual('"123";"123";');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
it('should keep track of in flight requests and request only once', function() {
|
2011-02-15 06:12:45 +00:00
|
|
|
scope.$service('$xhr.bulk').urls['/bulk'] = {
|
|
|
|
|
match:function(url){
|
|
|
|
|
return url == '/url';
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$browserXhr.expectPOST('/bulk', {
|
|
|
|
|
requests:[{method:'GET', url:'/url', data: null}]
|
|
|
|
|
}).respond([
|
|
|
|
|
{status:200, response:'123'}
|
|
|
|
|
]);
|
|
|
|
|
cache('GET', '/url', null, callback);
|
|
|
|
|
cache('GET', '/url', null, callback);
|
|
|
|
|
cache.delegate.flush();
|
|
|
|
|
$browserXhr.flush();
|
|
|
|
|
expect(log).toEqual('"123";"123";');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2011-10-07 18:27:49 +00:00
|
|
|
it('should clear cache on non GET', function() {
|
2011-02-15 06:12:45 +00:00
|
|
|
$browserXhr.expectPOST('abc', {}).respond({});
|
|
|
|
|
cache.data.url = {value:123};
|
|
|
|
|
cache('POST', 'abc', {});
|
|
|
|
|
expect(cache.data.url).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('should call callback asynchronously for both cache hit and cache miss', function() {
|
|
|
|
|
$browserXhr.expectGET('/url').respond('+');
|
|
|
|
|
cache('GET', '/url', null, callback);
|
|
|
|
|
expect(log).toEqual(''); //callback hasn't executed
|
|
|
|
|
|
|
|
|
|
$browserXhr.flush();
|
|
|
|
|
expect(log).toEqual('"+";'); //callback has executed
|
|
|
|
|
|
|
|
|
|
cache('GET', '/url', null, callback);
|
|
|
|
|
expect(log).toEqual('"+";'); //callback hasn't executed
|
|
|
|
|
|
|
|
|
|
$browser.defer.flush();
|
|
|
|
|
expect(log).toEqual('"+";"+";'); //callback has executed
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2011-03-30 16:35:59 +00:00
|
|
|
it('should call callback synchronously when sync flag is on', function() {
|
|
|
|
|
$browserXhr.expectGET('/url').respond('+');
|
|
|
|
|
cache('GET', '/url', null, callback, false, true);
|
|
|
|
|
expect(log).toEqual(''); //callback hasn't executed
|
|
|
|
|
|
|
|
|
|
$browserXhr.flush();
|
|
|
|
|
expect(log).toEqual('"+";'); //callback has executed
|
|
|
|
|
|
|
|
|
|
cache('GET', '/url', null, callback, false, true);
|
|
|
|
|
expect(log).toEqual('"+";"+";'); //callback has executed
|
|
|
|
|
|
|
|
|
|
$browser.defer.flush();
|
|
|
|
|
expect(log).toEqual('"+";"+";'); //callback was not called again any more
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2011-02-15 06:12:45 +00:00
|
|
|
it('should call eval after callbacks for both cache hit and cache miss execute', function() {
|
2011-08-10 20:15:43 +00:00
|
|
|
var flushSpy = this.spyOn(scope, '$digest').andCallThrough();
|
2011-02-15 06:12:45 +00:00
|
|
|
|
|
|
|
|
$browserXhr.expectGET('/url').respond('+');
|
|
|
|
|
cache('GET', '/url', null, callback);
|
2011-03-23 16:33:29 +00:00
|
|
|
expect(flushSpy).not.toHaveBeenCalled();
|
2011-02-15 06:12:45 +00:00
|
|
|
|
|
|
|
|
$browserXhr.flush();
|
2011-03-23 16:33:29 +00:00
|
|
|
expect(flushSpy).toHaveBeenCalled();
|
2011-02-15 06:12:45 +00:00
|
|
|
|
2011-03-23 16:33:29 +00:00
|
|
|
flushSpy.reset(); //reset the spy
|
2011-02-15 06:12:45 +00:00
|
|
|
|
|
|
|
|
cache('GET', '/url', null, callback);
|
2011-03-23 16:33:29 +00:00
|
|
|
expect(flushSpy).not.toHaveBeenCalled();
|
2011-02-15 06:12:45 +00:00
|
|
|
|
|
|
|
|
$browser.defer.flush();
|
2011-03-23 16:33:29 +00:00
|
|
|
expect(flushSpy).toHaveBeenCalled();
|
2011-02-15 06:12:45 +00:00
|
|
|
});
|
2011-07-22 19:56:45 +00:00
|
|
|
|
|
|
|
|
it('should call the error callback on error if provided', function() {
|
|
|
|
|
var errorSpy = jasmine.createSpy('error'),
|
|
|
|
|
successSpy = jasmine.createSpy('success');
|
|
|
|
|
|
|
|
|
|
$browserXhr.expectGET('/url').respond(500, 'error');
|
|
|
|
|
|
|
|
|
|
cache('GET', '/url', null, successSpy, errorSpy, false, true);
|
|
|
|
|
$browserXhr.flush();
|
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(500, 'error');
|
|
|
|
|
expect(successSpy).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
|
|
errorSpy.reset();
|
|
|
|
|
cache('GET', '/url', successSpy, errorSpy, false, true);
|
|
|
|
|
$browserXhr.flush();
|
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(500, 'error');
|
|
|
|
|
expect(successSpy).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should call the $xhr.error on error if error callback not provided', function() {
|
|
|
|
|
var errorSpy = jasmine.createSpy('error'),
|
|
|
|
|
successSpy = jasmine.createSpy('success');
|
|
|
|
|
|
|
|
|
|
$browserXhr.expectGET('/url').respond(500, 'error');
|
|
|
|
|
cache('GET', '/url', null, successSpy, false, true);
|
|
|
|
|
$browserXhr.flush();
|
|
|
|
|
|
|
|
|
|
expect(successSpy).not.toHaveBeenCalled();
|
|
|
|
|
expect($xhrErr).toHaveBeenCalledWith(
|
|
|
|
|
{method: 'GET', url: '/url', data: null, success: successSpy},
|
|
|
|
|
{status: 500, body: 'error'});
|
|
|
|
|
});
|
2011-02-15 06:12:45 +00:00
|
|
|
});
|