Move some specs into unit tests for request tracker

This commit is contained in:
slackersoft 2014-08-06 18:30:35 -07:00
parent d5e04f35af
commit 35ffa0d72c
3 changed files with 86 additions and 54 deletions

View File

@ -123,59 +123,6 @@ describe("Jasmine Mock Ajax (for toplevel)", function() {
expect(mockAjax.requests.mostRecent()).toBeUndefined();
});
});
describe("filter", function() {
describe("when there are no matching requests", function() {
it("returns an empty array", function() {
expect(mockAjax.requests.filter('/philharmonic')).toEqual([]);
});
});
describe("when there is one matching request", function() {
describe("returns an array with the single matching request", function() {
it("matches by exact URL", function() {
expect(mockAjax.requests.filter('example.com/someApi').length).toEqual(1);
});
it("matches by regexp", function() {
expect(mockAjax.requests.filter(/some.pi/).length).toEqual(1);
});
it("matches by funcarg", function() {
expect(mockAjax.requests.filter(function(request) {
return request.url === "example.com/someApi";
}).length).toEqual(1);
});
});
});
describe("when there is a non-matching request", function() {
beforeEach(function() {
client = new fakeGlobal.XMLHttpRequest();
client.onreadystatechange = onreadystatechange;
client.open("GET", "example.com/someOtherApi");
client.send();
});
it("returns just the matching requests", function() {
expect(mockAjax.requests.filter('example.com/someApi').length).toEqual(1);
});
it("matches by exact URL", function() {
expect(mockAjax.requests.filter('example.com/someApi').length).toEqual(1);
});
it("matches by regexp", function() {
expect(mockAjax.requests.filter(/some.pi/).length).toEqual(1);
});
it("matches by funcarg", function() {
expect(mockAjax.requests.filter(function(request) {
return request.url === "example.com/someApi";
}).length).toEqual(1);
});
});
});
});
describe("when simulating a response with request.response", function () {

View File

@ -0,0 +1,86 @@
describe('RequestTracker', function() {
beforeEach(function() {
var Constructor = getJasmineRequireObj().AjaxRequestTracker();
this.tracker = new Constructor();
});
it('tracks the number of times ajax requests are made', function() {
expect(this.tracker.count()).toBe(0);
this.tracker.track();
expect(this.tracker.count()).toBe(1);
});
it('simplifies access to the last (most recent) request', function() {
this.tracker.track();
this.tracker.track('request');
expect(this.tracker.mostRecent()).toEqual('request');
});
it('returns a useful falsy value when there is no last (most recent) request', function() {
expect(this.tracker.mostRecent()).toBeFalsy();
});
it('simplifies access to the first (oldest) request', function() {
this.tracker.track('request');
this.tracker.track();
expect(this.tracker.first()).toEqual('request');
});
it('returns a useful falsy value when there is no first (oldest) request', function() {
expect(this.tracker.first()).toBeFalsy();
});
it('allows the requests list to be reset', function() {
this.tracker.track();
this.tracker.track();
expect(this.tracker.count()).toBe(2);
this.tracker.reset();
expect(this.tracker.count()).toBe(0);
});
it('allows retrieval of an arbitrary request by index', function() {
this.tracker.track('1');
this.tracker.track('2');
this.tracker.track('3');
expect(this.tracker.at(1)).toEqual('2');
});
it('allows retrieval of all requests that are for a given url', function() {
this.tracker.track({ url: 'foo' });
this.tracker.track({ url: 'bar' });
expect(this.tracker.filter('bar')).toEqual([{ url: 'bar' }]);
});
it('allows retrieval of all requests that match a given RegExp', function() {
this.tracker.track({ url: 'foo' });
this.tracker.track({ url: 'bar' });
this.tracker.track({ url: 'baz' });
expect(this.tracker.filter(/ba[rz]/)).toEqual([{ url: 'bar' }, { url: 'baz' }]);
});
it('allows retrieval of all requests that match based on a function', function() {
this.tracker.track({ url: 'foo' });
this.tracker.track({ url: 'bar' });
this.tracker.track({ url: 'baz' });
var func = function(request) {
return request.url === 'bar';
};
expect(this.tracker.filter(func)).toEqual([{ url: 'bar' }]);
});
it('filters to nothing if no requests have been tracked', function() {
expect(this.tracker.filter('foo')).toEqual([]);
});
});

View File

@ -27,7 +27,6 @@ getJasmineRequireObj().AjaxRequestTracker = function() {
};
this.filter = function(url_to_match) {
if (requests.length === 0) { return []; }
var matching_requests = [];
for (var i = 0; i < requests.length; i++) {