Move some specs over to unit tests for requestStub

This commit is contained in:
slackersoft 2014-08-06 07:45:03 -07:00
parent c825452b60
commit 3a0fb720d2
2 changed files with 64 additions and 84 deletions

64
spec/requestStubSpec.js Normal file
View File

@ -0,0 +1,64 @@
describe('RequestStub', function() {
beforeEach(function() {
this.RequestStub = getJasmineRequireObj().AjaxRequestStub();
jasmine.addMatchers({
toMatchRequest: function() {
return {
compare: function(actual) {
return {
pass: actual.matches.apply(actual, Array.prototype.slice.call(arguments, 1))
};
}
};
}
});
});
it('matches just by exact url', function() {
var stub = new this.RequestStub('www.example.com/foo');
expect(stub).toMatchRequest('www.example.com/foo');
});
it('does not match if the url differs', function() {
var stub = new this.RequestStub('www.example.com/foo');
expect(stub).not.toMatchRequest('www.example.com/bar');
});
it('matches unordered query params', function() {
var stub = new this.RequestStub('www.example.com?foo=bar&baz=quux');
expect(stub).toMatchRequest('www.example.com?baz=quux&foo=bar');
});
it('requires all specified query params to be there', function() {
var stub = new this.RequestStub('www.example.com?foo=bar&baz=quux');
expect(stub).not.toMatchRequest('www.example.com?foo=bar');
});
it('can match the url with a RegExp', function() {
var stub = new this.RequestStub(/ba[rz]/);
expect(stub).toMatchRequest('bar');
expect(stub).toMatchRequest('baz');
expect(stub).not.toMatchRequest('foo');
});
it('requires the method to match if supplied', function() {
var stub = new this.RequestStub('www.example.com/foo', null, 'POST');
expect(stub).not.toMatchRequest('www.example.com/foo');
expect(stub).not.toMatchRequest('www.example.com/foo', null, 'GET');
expect(stub).toMatchRequest('www.example.com/foo', null, 'POST');
});
it('requires the data submitted to match if supplied', function() {
var stub = new this.RequestStub('/foo', 'foo=bar&baz=quux');
expect(stub).toMatchRequest('/foo', 'baz=quux&foo=bar');
expect(stub).not.toMatchRequest('/foo', 'foo=bar');
});
});

View File

@ -65,88 +65,4 @@ describe("Webmock style mocking", function() {
expect(response.responseText).toEqual('no'); expect(response.responseText).toEqual('no');
}); });
}); });
describe('stubs with method specified', function() {
beforeEach(function() {
mockAjax.stubRequest('http://example.com/myApi', null, 'POST').andReturn({responseText: 'post', status: '201'});
mockAjax.stubRequest('http://example.com/myApi', null, 'PUT').andReturn({responseText: 'put', status: '200'});
});
it("does not match a different method", function() {
sendRequest(fakeGlobal, 'http://example.com/myApi', 'GET');
expect(successSpy).not.toHaveBeenCalled();
});
it("matches with the right method", function() {
sendRequest(fakeGlobal, 'http://example.com/myApi', 'POST');
expect(response.responseText).toEqual('post');
});
});
describe("with a query string", function() {
beforeEach(function() {
mockAjax.stubRequest("http://example.com/someApi?foo=bar&baz=quux").andReturn({responseText: "greetings", status: 422});
});
it("should match the query string in any order", function() {
sendRequest(fakeGlobal, "http://example.com/someApi?baz=quux&foo=bar");
expect(response.status).toEqual(422);
expect(response.responseText).toEqual('greetings');
});
});
it("allows stubs to use RegExp", function () {
mockAjax.stubRequest(/thisGuy/).andReturn({responseText: 'regex', status: 200});
sendRequest(fakeGlobal, 'http://www.example.com/thisGuy/thatGuy');
expect(successSpy).toHaveBeenCalled();
expect(response.responseText).toBe('regex');
});
describe("stubbing with form data", function() {
beforeEach(function() {
mockAjax.stubRequest("http://example.com/someApi", 'foo=bar&baz=quux').andReturn({responseText: "form", status: 201});
});
var postRequest = function(data) {
var xhr = new fakeGlobal.XMLHttpRequest();
xhr.onreadystatechange = function(args) {
if (this.readyState === (this.DONE || 4)) { // IE 8 doesn't support DONE
response = this;
successSpy();
}
};
xhr.open("POST", "http://example.com/someApi");
xhr.send(data);
};
it("uses the form data stub when the data matches", function() {
postRequest('foo=bar&baz=quux');
expect(response.status).toEqual(201);
expect(response.responseText).toEqual('form');
});
it("matches data params in any order", function() {
postRequest('baz=quux&foo=bar');
expect(response.status).toEqual(201);
expect(response.responseText).toEqual('form');
});
it("falls back to the stub without data specified if the data doesn't match", function() {
postRequest('foo=bar');
expect(response.status).toEqual(200);
expect(response.responseText).toEqual('hi!');
});
it("uses the stub without data specified if no data is passed", function() {
postRequest();
expect(response.status).toEqual(200);
expect(response.responseText).toEqual('hi!');
});
});
}); });