Skip to content

Commit

Permalink
Move the logic for responding with a stub into the stub itself
Browse files Browse the repository at this point in the history
- Note that this includes a breaking change to the interface. The
  function passed in will no longer receive the stub as an argument,
  only the request will be passed.
- The `responseCallFunction` on the FakeRequest has also been removed.
  If you were using that directly and not through the stub
  functionality, you should be able to replace it by calling your
  function directly and passing in the request.
- Fixes #193
  • Loading branch information
Gregg Van Hove committed Feb 11, 2019
1 parent 55cde58 commit a0dc4b4
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 201 deletions.
67 changes: 17 additions & 50 deletions lib/mock-ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,21 +357,8 @@ extend(FakeXMLHttpRequest, {
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);
} else if (stub.isError()) {
this.responseError(stub);
} else if (stub.isTimeout()) {
this.responseTimeout();
} else if (stub.isCallFunction()) {
this.responseCallFunction(stub);
}
stub.handleRequest(this);
}
},

Expand Down Expand Up @@ -502,12 +489,6 @@ extend(FakeXMLHttpRequest, {
this.eventBus.trigger('loadend');
},

responseCallFunction: function(stub) {
stub.action = undefined;
stub.functionToCall(stub, this);
this.dispatchStub(stub);
},

startStream: function(options) {
if (!options) {
options = {};
Expand Down Expand Up @@ -701,6 +682,10 @@ getJasmineRequireObj().AjaxRequestStub = function() {
return query ? query.split('&').sort().join('&') : undefined;
};

var timeoutRequest = function(request) {
request.responseTimeout();
};

function RequestStub(url, stubData, method) {
if (url instanceof RegExp) {
this.url = url;
Expand All @@ -717,48 +702,30 @@ getJasmineRequireObj().AjaxRequestStub = function() {

RequestStub.prototype = {
andReturn: function(options) {
this.action = RETURN;
this.status = (typeof options.status !== 'undefined') ? options.status : 200;
this.statusText = options.statusText;

this.contentType = options.contentType;
this.response = options.response;
this.responseText = options.responseText;
this.responseHeaders = options.responseHeaders;
this.responseURL = options.responseURL;
},

isReturn: function() {
return this.action === RETURN;
options.status = (typeof options.status !== 'undefined') ? options.status : 200;
this.handleRequest = function(request) {
request.respondWith(options);
};
},

andError: function(options) {
if (!options) {
options = {};
}
this.action = ERROR;
this.status = options.status || 500;
},

isError: function() {
return this.action === ERROR;
options.status = options.status || 500;
this.handleRequest = function(request) {
request.responseError(options);
};
},

andTimeout: function() {
this.action = TIMEOUT;
},

isTimeout: function() {
return this.action === TIMEOUT;
this.handleRequest = timeoutRequest;
},

andCallFunction: function(functionToCall) {
this.action = CALL;
this.functionToCall = functionToCall;
},

isCallFunction: function() {
return this.action === CALL;
this.handleRequest = function(request) {
functionToCall(request);
};
},

matches: function(fullUrl, data, method) {
Expand Down
90 changes: 0 additions & 90 deletions spec/fakeRequestSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,96 +800,6 @@ 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);
});
});

describe('stream response', function() {
it('can return a response', function() {
var request = new this.FakeRequest();
Expand Down
84 changes: 73 additions & 11 deletions spec/requestStubSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,41 +71,103 @@ describe('RequestStub', function() {
});

describe('when returning successfully', function() {
it('isReturn', function() {
it('passes response information to the request', function() {
var stub = new this.RequestStub('/foo');
stub.andReturn({});

expect(stub.isReturn()).toBe(true);
stub.andReturn({
status: 300,
statusText: 'hi there',
contentType: 'text/plain',
extra: 'stuff'
});
var fakeRequest = { respondWith: jasmine.createSpy('respondWith') };

stub.handleRequest(fakeRequest);

expect(fakeRequest.respondWith).toHaveBeenCalledWith({
status: 300,
statusText: 'hi there',
contentType: 'text/plain',
extra: 'stuff'
});
});

it('defaults to status 200', function() {
var stub = new this.RequestStub('/foo');
stub.andReturn({});
var fakeRequest = { respondWith: jasmine.createSpy('respondWith') };

stub.handleRequest(fakeRequest);

expect(stub.status).toBe(200);
expect(fakeRequest.respondWith).toHaveBeenCalledWith(jasmine.objectContaining({
status: 200
}));
});

it('allows setting a response code of 0', function() {
var stub = new this.RequestStub('/foo');
stub.andReturn({status: 0});
var fakeRequest = { respondWith: jasmine.createSpy('respondWith') };

expect(stub.status).toBe(0);
stub.handleRequest(fakeRequest);

expect(fakeRequest.respondWith).toHaveBeenCalledWith(jasmine.objectContaining({
status: 0
}));
});
});

describe('when erroring', function() {
it('isReturn', function() {
it('passes error information to request', function() {
var stub = new this.RequestStub('/foo');
stub.andError({});

expect(stub.isError()).toBe(true);
stub.andError({
status: 502,
extra: 'stuff'
});

var fakeRequest = { responseError: jasmine.createSpy('responseError') };
stub.handleRequest(fakeRequest);

expect(fakeRequest.responseError).toHaveBeenCalledWith({
status: 502,
extra: 'stuff'
});
});

it('defaults to status 500', function() {
var stub = new this.RequestStub('/foo');
stub.andError({});

expect(stub.status).toBe(500);
var fakeRequest = { responseError: jasmine.createSpy('responseError') };
stub.handleRequest(fakeRequest);

expect(fakeRequest.responseError).toHaveBeenCalledWith(jasmine.objectContaining({
status: 500
}));
});
});

describe('when timing out', function() {
it('tells the request to time out', function() {
var stub = new this.RequestStub('/foo');
stub.andTimeout();

var fakeRequest = { responseTimeout: jasmine.createSpy('responseTimeout') };
stub.handleRequest(fakeRequest);

expect(fakeRequest.responseTimeout).toHaveBeenCalled();
});
});

describe('when calling a function', function() {
it('invokes the function with the request', function() {
var stub = new this.RequestStub('/foo');
var callback = jasmine.createSpy('callback').and.returnValue({ status: 201 });
stub.andCallFunction(callback);

var fakeRequest = { things: 'stuff' };
stub.handleRequest(fakeRequest);

expect(callback).toHaveBeenCalledWith(fakeRequest);
});
});
});
21 changes: 1 addition & 20 deletions src/fakeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,8 @@ extend(FakeXMLHttpRequest, {
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);
} else if (stub.isError()) {
this.responseError(stub);
} else if (stub.isTimeout()) {
this.responseTimeout();
} else if (stub.isCallFunction()) {
this.responseCallFunction(stub);
}
stub.handleRequest(this);
}
},

Expand Down Expand Up @@ -325,12 +312,6 @@ extend(FakeXMLHttpRequest, {
this.eventBus.trigger('loadend');
},

responseCallFunction: function(stub) {
stub.action = undefined;
stub.functionToCall(stub, this);
this.dispatchStub(stub);
},

startStream: function(options) {
if (!options) {
options = {};
Expand Down
Loading

0 comments on commit a0dc4b4

Please sign in to comment.