Event 'readystatechange' is now triggered

Close #102
This commit is contained in:
Mickael Jeanroy 2015-04-07 08:36:12 +02:00 committed by Gregg Van Hove
parent eecce5403a
commit d2762c59f0
4 changed files with 51 additions and 28 deletions

View File

@ -12,6 +12,15 @@ describe('EventBus', function() {
expect(callback).toHaveBeenCalled(); expect(callback).toHaveBeenCalled();
}); });
it('calls an event listener with additional arguments', function() {
var callback = jasmine.createSpy('callback');
this.bus.addEventListener('foo', callback);
this.bus.trigger('foo', 'bar');
expect(callback).toHaveBeenCalledWith('bar');
});
it('only triggers callbacks for the specified event', function() { it('only triggers callbacks for the specified event', function() {
var fooCallback = jasmine.createSpy('foo'), var fooCallback = jasmine.createSpy('foo'),
barCallback = jasmine.createSpy('bar'); barCallback = jasmine.createSpy('bar');

View File

@ -101,74 +101,73 @@ describe('FakeRequest', function() {
describe('managing readyState', function() { describe('managing readyState', function() {
beforeEach(function() { beforeEach(function() {
this.request = new this.FakeRequest(); this.request = new this.FakeRequest();
this.request.onreadystatechange = jasmine.createSpy('onreadystatechange');
}); });
it('has an initial ready state of 0 (uninitialized)', function() { it('has an initial ready state of 0 (uninitialized)', function() {
expect(this.request.readyState).toBe(0); expect(this.request.readyState).toBe(0);
expect(this.request.onreadystatechange).not.toHaveBeenCalled(); expect(this.fakeEventBus.trigger).not.toHaveBeenCalled();
}); });
it('has a ready state of 1 (open) when opened', function() { it('has a ready state of 1 (open) when opened', function() {
this.request.open(); this.request.open();
expect(this.request.readyState).toBe(1); expect(this.request.readyState).toBe(1);
expect(this.request.onreadystatechange).toHaveBeenCalled(); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
}); });
it('has a ready state of 0 (uninitialized) when aborted', function() { it('has a ready state of 0 (uninitialized) when aborted', function() {
this.request.open(); this.request.open();
this.request.onreadystatechange.calls.reset(); this.fakeEventBus.trigger.calls.reset();
this.request.abort(); this.request.abort();
expect(this.request.readyState).toBe(0); expect(this.request.readyState).toBe(0);
expect(this.request.onreadystatechange).toHaveBeenCalled(); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
}); });
it('has a ready state of 2 (sent) when sent', function() { it('has a ready state of 2 (sent) when sent', function() {
this.request.open(); this.request.open();
this.request.onreadystatechange.calls.reset(); this.fakeEventBus.trigger.calls.reset();
this.request.send(); this.request.send();
expect(this.request.readyState).toBe(2); expect(this.request.readyState).toBe(2);
expect(this.request.onreadystatechange).toHaveBeenCalled(); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
}); });
it('has a ready state of 4 (loaded) when timed out', function() { it('has a ready state of 4 (loaded) when timed out', function() {
this.request.open(); this.request.open();
this.request.send(); this.request.send();
this.request.onreadystatechange.calls.reset(); this.fakeEventBus.trigger.calls.reset();
jasmine.clock().install(); jasmine.clock().install();
this.request.responseTimeout(); this.request.responseTimeout();
jasmine.clock().uninstall(); jasmine.clock().uninstall();
expect(this.request.readyState).toBe(4); expect(this.request.readyState).toBe(4);
expect(this.request.onreadystatechange).toHaveBeenCalledWith('timeout'); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange', 'timeout');
}); });
it('has a ready state of 4 (loaded) when network erroring', function() { it('has a ready state of 4 (loaded) when network erroring', function() {
this.request.open(); this.request.open();
this.request.send(); this.request.send();
this.request.onreadystatechange.calls.reset(); this.fakeEventBus.trigger.calls.reset();
this.request.responseError(); this.request.responseError();
expect(this.request.readyState).toBe(4); expect(this.request.readyState).toBe(4);
expect(this.request.onreadystatechange).toHaveBeenCalled(); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
}); });
it('has a ready state of 4 (loaded) when responding', function() { it('has a ready state of 4 (loaded) when responding', function() {
this.request.open(); this.request.open();
this.request.send(); this.request.send();
this.request.onreadystatechange.calls.reset(); this.fakeEventBus.trigger.calls.reset();
this.request.respondWith({}); this.request.respondWith({});
expect(this.request.readyState).toBe(4); expect(this.request.readyState).toBe(4);
expect(this.request.onreadystatechange).toHaveBeenCalled(); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
}); });
it('throws an error when timing out a request that has completed', function() { it('throws an error when timing out a request that has completed', function() {
@ -208,6 +207,7 @@ describe('FakeRequest', function() {
it('registers on-style callback with the event bus', function() { it('registers on-style callback with the event bus', function() {
this.request = new this.FakeRequest(); this.request = new this.FakeRequest();
expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('readystatechange', jasmine.any(Function));
expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('loadstart', jasmine.any(Function)); expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('loadstart', jasmine.any(Function));
expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('progress', jasmine.any(Function)); expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('progress', jasmine.any(Function));
expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('abort', jasmine.any(Function)); expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('abort', jasmine.any(Function));
@ -216,6 +216,7 @@ describe('FakeRequest', function() {
expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('timeout', jasmine.any(Function)); expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('timeout', jasmine.any(Function));
expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('loadend', jasmine.any(Function)); expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('loadend', jasmine.any(Function));
this.request.onreadystatechange = jasmine.createSpy('readystatechange');
this.request.onloadstart = jasmine.createSpy('loadstart'); this.request.onloadstart = jasmine.createSpy('loadstart');
this.request.onprogress = jasmine.createSpy('progress'); this.request.onprogress = jasmine.createSpy('progress');
this.request.onabort = jasmine.createSpy('abort'); this.request.onabort = jasmine.createSpy('abort');
@ -233,12 +234,12 @@ describe('FakeRequest', function() {
expect(this.request['on' + eventName]).toHaveBeenCalled(); expect(this.request['on' + eventName]).toHaveBeenCalled();
} }
}); });
it('delegates addEventListener to the eventBus', function() { it('delegates addEventListener to the eventBus', function() {
this.request = new this.FakeRequest(); this.request = new this.FakeRequest();
this.request.addEventListener('foo', 'bar'); this.request.addEventListener('foo', 'bar');
expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('foo', 'bar'); expect(this.fakeEventBus.addEventListener).toHaveBeenCalledWith('foo', 'bar');
}); });
@ -258,14 +259,18 @@ describe('FakeRequest', function() {
it('should not trigger any events to start', function() { it('should not trigger any events to start', function() {
this.request.open(); this.request.open();
expect(this.fakeEventBus.trigger).not.toHaveBeenCalled(); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
}); });
it('should trigger loadstart when sent', function() { it('should trigger loadstart when sent', function() {
this.request.open(); this.request.open();
this.fakeEventBus.trigger.calls.reset();
this.request.send(); this.request.send();
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('loadstart'); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('loadstart');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('progress'); expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('progress');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('abort'); expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('abort');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('error'); expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('error');
@ -282,6 +287,7 @@ describe('FakeRequest', function() {
this.request.abort(); this.request.abort();
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('loadstart'); expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('loadstart');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('progress'); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('progress');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('abort'); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('abort');
@ -300,6 +306,7 @@ describe('FakeRequest', function() {
this.request.responseError(); this.request.responseError();
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('loadstart'); expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('loadstart');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('progress'); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('progress');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('abort'); expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('abort');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('error'); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('error');
@ -319,6 +326,7 @@ describe('FakeRequest', function() {
jasmine.clock().uninstall(); jasmine.clock().uninstall();
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('loadstart'); expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('loadstart');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange', 'timeout');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('progress'); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('progress');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('abort'); expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('abort');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('error'); expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('error');
@ -336,6 +344,7 @@ describe('FakeRequest', function() {
this.request.respondWith({ status: 200 }); this.request.respondWith({ status: 200 });
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('loadstart'); expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('loadstart');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('progress'); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('progress');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('abort'); expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('abort');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('error'); expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('error');

View File

@ -22,6 +22,10 @@ getJasmineRequireObj().AjaxEventBus = function() {
return -1; return -1;
} }
function slice(array, start) {
return Array.prototype.slice.call(array, start);
}
EventBus.prototype.addEventListener = function(event, callback) { EventBus.prototype.addEventListener = function(event, callback) {
ensureEvent(this.eventList, event).push(callback); ensureEvent(this.eventList, event).push(callback);
}; };
@ -35,11 +39,13 @@ getJasmineRequireObj().AjaxEventBus = function() {
}; };
EventBus.prototype.trigger = function(event) { EventBus.prototype.trigger = function(event) {
// Arguments specified after event name need to be propagated
var args = slice(arguments, 1);
var eventListeners = this.eventList[event]; var eventListeners = this.eventList[event];
if(eventListeners){ if(eventListeners){
for(var i = 0; i < eventListeners.length; i++){ for(var i = 0; i < eventListeners.length; i++){
eventListeners[i](); eventListeners[i].apply(this, args);
} }
} }
}; };

View File

@ -21,12 +21,13 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
function wrapProgressEvent(xhr, eventName) { function wrapProgressEvent(xhr, eventName) {
return function() { return function() {
if (xhr[eventName]) { if (xhr[eventName]) {
xhr[eventName](); xhr[eventName].apply(xhr, arguments);
} }
}; };
} }
function initializeEvents(xhr) { function initializeEvents(xhr) {
xhr.eventBus.addEventListener('readystatechange', wrapProgressEvent(xhr, 'onreadystatechange'));
xhr.eventBus.addEventListener('loadstart', wrapProgressEvent(xhr, 'onloadstart')); xhr.eventBus.addEventListener('loadstart', wrapProgressEvent(xhr, 'onloadstart'));
xhr.eventBus.addEventListener('load', wrapProgressEvent(xhr, 'onload')); xhr.eventBus.addEventListener('load', wrapProgressEvent(xhr, 'onload'));
xhr.eventBus.addEventListener('loadend', wrapProgressEvent(xhr, 'onloadend')); xhr.eventBus.addEventListener('loadend', wrapProgressEvent(xhr, 'onloadend'));
@ -115,7 +116,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.username = arguments[3]; this.username = arguments[3];
this.password = arguments[4]; this.password = arguments[4];
this.readyState = 1; this.readyState = 1;
this.onreadystatechange(); this.eventBus.trigger('readystatechange');
}, },
setRequestHeader: function(header, value) { setRequestHeader: function(header, value) {
@ -134,7 +135,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.readyState = 0; this.readyState = 0;
this.status = 0; this.status = 0;
this.statusText = "abort"; this.statusText = "abort";
this.onreadystatechange(); this.eventBus.trigger('readystatechange');
this.eventBus.trigger('progress'); this.eventBus.trigger('progress');
this.eventBus.trigger('abort'); this.eventBus.trigger('abort');
this.eventBus.trigger('loadend'); this.eventBus.trigger('loadend');
@ -149,9 +150,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
onload: null, onload: null,
ontimeout: null, ontimeout: null,
onloadend: null, onloadend: null,
onreadystatechange: null,
onreadystatechange: function(isTimeout) {
},
addEventListener: function() { addEventListener: function() {
this.eventBus.addEventListener.apply(this.eventBus, arguments); this.eventBus.addEventListener.apply(this.eventBus, arguments);
@ -167,7 +166,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.params = data; this.params = data;
this.readyState = 2; this.readyState = 2;
this.eventBus.trigger('loadstart'); this.eventBus.trigger('loadstart');
this.onreadystatechange(); this.eventBus.trigger('readystatechange');
var stub = stubTracker.findStub(this.url, data, this.method); var stub = stubTracker.findStub(this.url, data, this.method);
if (stub) { if (stub) {
@ -255,7 +254,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.response = this.responseValue(); this.response = this.responseValue();
} }
this.onreadystatechange(); this.eventBus.trigger('readystatechange');
this.eventBus.trigger('progress'); this.eventBus.trigger('progress');
this.eventBus.trigger('load'); this.eventBus.trigger('load');
this.eventBus.trigger('loadend'); this.eventBus.trigger('loadend');
@ -267,7 +266,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
} }
this.readyState = 4; this.readyState = 4;
jasmine.clock().tick(30000); jasmine.clock().tick(30000);
this.onreadystatechange('timeout'); this.eventBus.trigger('readystatechange', 'timeout');
this.eventBus.trigger('progress'); this.eventBus.trigger('progress');
this.eventBus.trigger('timeout'); this.eventBus.trigger('timeout');
this.eventBus.trigger('loadend'); this.eventBus.trigger('loadend');
@ -278,7 +277,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
throw new Error("FakeXMLHttpRequest already completed"); throw new Error("FakeXMLHttpRequest already completed");
} }
this.readyState = 4; this.readyState = 4;
this.onreadystatechange(); this.eventBus.trigger('readystatechange');
this.eventBus.trigger('progress'); this.eventBus.trigger('progress');
this.eventBus.trigger('error'); this.eventBus.trigger('error');
this.eventBus.trigger('loadend'); this.eventBus.trigger('loadend');