Merge branch 'mjeanroy-readystatechange'

Closes #103
This commit is contained in:
Gregg Van Hove 2015-04-09 10:24:01 -07:00
commit 3212c5f04a
4 changed files with 51 additions and 28 deletions

View File

@ -12,6 +12,15 @@ describe('EventBus', function() {
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() {
var fooCallback = jasmine.createSpy('foo'),
barCallback = jasmine.createSpy('bar');

View File

@ -101,74 +101,73 @@ describe('FakeRequest', function() {
describe('managing readyState', function() {
beforeEach(function() {
this.request = new this.FakeRequest();
this.request.onreadystatechange = jasmine.createSpy('onreadystatechange');
});
it('has an initial ready state of 0 (uninitialized)', function() {
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() {
this.request.open();
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() {
this.request.open();
this.request.onreadystatechange.calls.reset();
this.fakeEventBus.trigger.calls.reset();
this.request.abort();
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() {
this.request.open();
this.request.onreadystatechange.calls.reset();
this.fakeEventBus.trigger.calls.reset();
this.request.send();
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() {
this.request.open();
this.request.send();
this.request.onreadystatechange.calls.reset();
this.fakeEventBus.trigger.calls.reset();
jasmine.clock().install();
this.request.responseTimeout();
jasmine.clock().uninstall();
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() {
this.request.open();
this.request.send();
this.request.onreadystatechange.calls.reset();
this.fakeEventBus.trigger.calls.reset();
this.request.responseError();
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() {
this.request.open();
this.request.send();
this.request.onreadystatechange.calls.reset();
this.fakeEventBus.trigger.calls.reset();
this.request.respondWith({});
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() {
@ -208,6 +207,7 @@ describe('FakeRequest', function() {
it('registers on-style callback with the event bus', function() {
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('progress', 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('loadend', jasmine.any(Function));
this.request.onreadystatechange = jasmine.createSpy('readystatechange');
this.request.onloadstart = jasmine.createSpy('loadstart');
this.request.onprogress = jasmine.createSpy('progress');
this.request.onabort = jasmine.createSpy('abort');
@ -258,14 +259,18 @@ describe('FakeRequest', function() {
it('should not trigger any events to start', function() {
this.request.open();
expect(this.fakeEventBus.trigger).not.toHaveBeenCalled();
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
});
it('should trigger loadstart when sent', function() {
this.request.open();
this.fakeEventBus.trigger.calls.reset();
this.request.send();
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('abort');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('error');
@ -282,6 +287,7 @@ describe('FakeRequest', function() {
this.request.abort();
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('loadstart');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('progress');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('abort');
@ -300,6 +306,7 @@ describe('FakeRequest', function() {
this.request.responseError();
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('loadstart');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('progress');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('abort');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('error');
@ -319,6 +326,7 @@ describe('FakeRequest', function() {
jasmine.clock().uninstall();
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('loadstart');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange', 'timeout');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('progress');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('abort');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('error');
@ -336,6 +344,7 @@ describe('FakeRequest', function() {
this.request.respondWith({ status: 200 });
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('loadstart');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('progress');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('abort');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('error');

View File

@ -22,6 +22,10 @@ getJasmineRequireObj().AjaxEventBus = function() {
return -1;
}
function slice(array, start) {
return Array.prototype.slice.call(array, start);
}
EventBus.prototype.addEventListener = function(event, callback) {
ensureEvent(this.eventList, event).push(callback);
};
@ -35,11 +39,13 @@ getJasmineRequireObj().AjaxEventBus = function() {
};
EventBus.prototype.trigger = function(event) {
// Arguments specified after event name need to be propagated
var args = slice(arguments, 1);
var eventListeners = this.eventList[event];
if(eventListeners){
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) {
return function() {
if (xhr[eventName]) {
xhr[eventName]();
xhr[eventName].apply(xhr, arguments);
}
};
}
function initializeEvents(xhr) {
xhr.eventBus.addEventListener('readystatechange', wrapProgressEvent(xhr, 'onreadystatechange'));
xhr.eventBus.addEventListener('loadstart', wrapProgressEvent(xhr, 'onloadstart'));
xhr.eventBus.addEventListener('load', wrapProgressEvent(xhr, 'onload'));
xhr.eventBus.addEventListener('loadend', wrapProgressEvent(xhr, 'onloadend'));
@ -115,7 +116,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.username = arguments[3];
this.password = arguments[4];
this.readyState = 1;
this.onreadystatechange();
this.eventBus.trigger('readystatechange');
},
setRequestHeader: function(header, value) {
@ -134,7 +135,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.readyState = 0;
this.status = 0;
this.statusText = "abort";
this.onreadystatechange();
this.eventBus.trigger('readystatechange');
this.eventBus.trigger('progress');
this.eventBus.trigger('abort');
this.eventBus.trigger('loadend');
@ -149,9 +150,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
onload: null,
ontimeout: null,
onloadend: null,
onreadystatechange: function(isTimeout) {
},
onreadystatechange: null,
addEventListener: function() {
this.eventBus.addEventListener.apply(this.eventBus, arguments);
@ -167,7 +166,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.params = data;
this.readyState = 2;
this.eventBus.trigger('loadstart');
this.onreadystatechange();
this.eventBus.trigger('readystatechange');
var stub = stubTracker.findStub(this.url, data, this.method);
if (stub) {
@ -255,7 +254,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.response = this.responseValue();
}
this.onreadystatechange();
this.eventBus.trigger('readystatechange');
this.eventBus.trigger('progress');
this.eventBus.trigger('load');
this.eventBus.trigger('loadend');
@ -267,7 +266,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
}
this.readyState = 4;
jasmine.clock().tick(30000);
this.onreadystatechange('timeout');
this.eventBus.trigger('readystatechange', 'timeout');
this.eventBus.trigger('progress');
this.eventBus.trigger('timeout');
this.eventBus.trigger('loadend');
@ -278,7 +277,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
throw new Error("FakeXMLHttpRequest already completed");
}
this.readyState = 4;
this.onreadystatechange();
this.eventBus.trigger('readystatechange');
this.eventBus.trigger('progress');
this.eventBus.trigger('error');
this.eventBus.trigger('loadend');