Merge pull request #152 from TerriaJS/andCallFunction

Add `RequestStub.andCallFunction.
This commit is contained in:
Gregg Van Hove 2016-08-15 10:20:41 -07:00 committed by GitHub
commit 33b9baa2df
4 changed files with 138 additions and 2 deletions

View File

@ -345,6 +345,11 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.eventBus.trigger('loadstart');
var stub = stubTracker.findStub(this.url, data, this.method);
this.dispatchStub(stub);
},
dispatchStub: function(stub) {
if (stub) {
if (stub.isReturn()) {
this.respondWith(stub);
@ -352,6 +357,8 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.responseError();
} else if (stub.isTimeout()) {
this.responseTimeout();
} else if (stub.isCallFunction()) {
this.responseCallFunction(stub);
}
}
},
@ -469,6 +476,12 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.eventBus.trigger('progress');
this.eventBus.trigger('error');
this.eventBus.trigger('loadend');
},
responseCallFunction: function(stub) {
stub.action = undefined;
stub.functionToCall(stub, this);
this.dispatchStub(stub);
}
});
@ -591,7 +604,8 @@ getJasmineRequireObj().AjaxParamParser = function() {
getJasmineRequireObj().AjaxRequestStub = function() {
var RETURN = 0,
ERROR = 1,
TIMEOUT = 2;
TIMEOUT = 2,
CALL = 3;
function RequestStub(url, stubData, method) {
var normalizeQuery = function(query) {
@ -641,6 +655,15 @@ getJasmineRequireObj().AjaxRequestStub = function() {
return this.action === TIMEOUT;
};
this.andCallFunction = function(functionToCall) {
this.action = CALL;
this.functionToCall = functionToCall;
};
this.isCallFunction = function() {
return this.action === CALL;
};
this.matches = function(fullUrl, data, method) {
var urlMatches = false;
fullUrl = fullUrl.toString();

View File

@ -714,4 +714,94 @@ describe('FakeRequest', function() {
expect(request.response).toEqual('foo');
});
describe('function response', function() {
it('can return a response', function() {
var request = new this.FakeRequest();
request.open();
request.send();
function f(stub, request) {
expect(stub.action).toBeUndefined();
stub.isReturn = function() { return true; };
stub.isError = function() { return false; };
stub.isTimeout = function() { return false; };
stub.isCallFunction = function() { return false; };
stub.responseText = 'foo';
}
request.responseCallFunction({ functionToCall: f });
expect(request.response).toEqual('foo');
});
it('can return an error', function() {
var request = new this.FakeRequest();
request.open();
request.send();
this.fakeEventBus.trigger.calls.reset();
function f(stub, request) {
expect(stub.action).toBeUndefined();
stub.isReturn = function() { return false; };
stub.isError = function() { return true; };
stub.isTimeout = function() { return false; };
stub.isCallFunction = function() { return false; };
}
request.responseCallFunction({ functionToCall: f });
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('error');
});
it('can return a timeout', function() {
var request = new this.FakeRequest();
request.open();
request.send();
this.fakeEventBus.trigger.calls.reset();
function f(stub, request) {
expect(stub.action).toBeUndefined();
stub.isReturn = function() { return false; };
stub.isError = function() { return false; };
stub.isTimeout = function() { return true; };
stub.isCallFunction = function() { return false; };
}
jasmine.clock().install();
request.responseCallFunction({ functionToCall: f });
jasmine.clock().uninstall();
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('timeout');
});
it('can chain to another function', function() {
var request = new this.FakeRequest();
request.open();
request.send();
this.fakeEventBus.trigger.calls.reset();
var calls = 0;
function f(stub, request) {
expect(stub.action).toBeUndefined();
stub.isReturn = function() { return false; };
stub.isError = function() { return false; };
stub.isTimeout = function() { return false; };
++calls;
if (calls > 1) {
stub.isCallFunction = function() { return false; };
} else {
stub.isCallFunction = function() { return true; };
}
}
request.responseCallFunction({ functionToCall: f });
expect(calls).toBe(2);
});
});
});

View File

@ -168,6 +168,11 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.eventBus.trigger('loadstart');
var stub = stubTracker.findStub(this.url, data, this.method);
this.dispatchStub(stub);
},
dispatchStub: function(stub) {
if (stub) {
if (stub.isReturn()) {
this.respondWith(stub);
@ -175,6 +180,8 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.responseError();
} else if (stub.isTimeout()) {
this.responseTimeout();
} else if (stub.isCallFunction()) {
this.responseCallFunction(stub);
}
}
},
@ -292,6 +299,12 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.eventBus.trigger('progress');
this.eventBus.trigger('error');
this.eventBus.trigger('loadend');
},
responseCallFunction: function(stub) {
stub.action = undefined;
stub.functionToCall(stub, this);
this.dispatchStub(stub);
}
});

View File

@ -1,7 +1,8 @@
getJasmineRequireObj().AjaxRequestStub = function() {
var RETURN = 0,
ERROR = 1,
TIMEOUT = 2;
TIMEOUT = 2,
CALL = 3;
function RequestStub(url, stubData, method) {
var normalizeQuery = function(query) {
@ -51,6 +52,15 @@ getJasmineRequireObj().AjaxRequestStub = function() {
return this.action === TIMEOUT;
};
this.andCallFunction = function(functionToCall) {
this.action = CALL;
this.functionToCall = functionToCall;
};
this.isCallFunction = function() {
return this.action === CALL;
};
this.matches = function(fullUrl, data, method) {
var urlMatches = false;
fullUrl = fullUrl.toString();