2013-02-05 11:32:59 +08:00
|
|
|
describe("Webmock style mocking", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
var successSpy, errorSpy, response, fakeGlobal, mockAjax;
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2014-01-22 11:06:23 +08:00
|
|
|
var sendRequest = function(fakeGlobal, url) {
|
|
|
|
url = url || "http://example.com/someApi"
|
2013-10-12 13:16:22 +08:00
|
|
|
var xhr = new fakeGlobal.XMLHttpRequest();
|
2013-02-05 11:32:59 +08:00
|
|
|
xhr.onreadystatechange = function(arguments) {
|
2014-06-02 06:37:19 +08:00
|
|
|
if (this.readyState == (this.DONE || 4)) { // IE 8 doesn't support DONE
|
2013-02-05 11:32:59 +08:00
|
|
|
response = this;
|
2013-10-12 13:16:22 +08:00
|
|
|
successSpy();
|
2013-02-05 11:32:59 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-22 11:06:23 +08:00
|
|
|
xhr.open("GET", url);
|
2013-02-05 11:32:59 +08:00
|
|
|
xhr.send();
|
2013-02-19 12:37:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
successSpy = jasmine.createSpy('success');
|
|
|
|
fakeGlobal = {XMLHttpRequest: jasmine.createSpy('realXMLHttpRequest')};
|
|
|
|
mockAjax = new MockAjax(fakeGlobal);
|
|
|
|
mockAjax.install();
|
2013-02-19 12:37:17 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
mockAjax.stubRequest("http://example.com/someApi").andReturn({responseText: "hi!"});
|
2013-02-19 12:37:17 +08:00
|
|
|
});
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
it("allows a url to be setup as a stub", function() {
|
|
|
|
sendRequest(fakeGlobal);
|
|
|
|
expect(successSpy).toHaveBeenCalled();
|
2013-02-19 12:37:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should allow you to clear all the ajax stubs", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
mockAjax.stubs.reset();
|
|
|
|
sendRequest(fakeGlobal);
|
|
|
|
expect(successSpy).not.toHaveBeenCalled();
|
2013-02-05 11:32:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should set the contentType", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
sendRequest(fakeGlobal);
|
2013-02-05 11:32:59 +08:00
|
|
|
expect(response.responseHeaders['Content-type']).toEqual('application/json');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should set the responseText", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
sendRequest(fakeGlobal);
|
2013-02-05 11:32:59 +08:00
|
|
|
expect(response.responseText).toEqual('hi!');
|
|
|
|
});
|
|
|
|
|
2013-02-19 12:37:17 +08:00
|
|
|
it("should default the status to 200", function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
sendRequest(fakeGlobal);
|
2013-02-19 12:37:17 +08:00
|
|
|
expect(response.status).toEqual(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("with another stub for the same url", function() {
|
|
|
|
beforeEach(function() {
|
2013-10-12 13:16:22 +08:00
|
|
|
mockAjax.stubRequest("http://example.com/someApi").andReturn({responseText: "no", status: 403});
|
|
|
|
sendRequest(fakeGlobal);
|
2013-02-19 12:37:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should set the status", function() {
|
|
|
|
expect(response.status).toEqual(403);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should allow the latest stub to win", function() {
|
|
|
|
expect(response.responseText).toEqual('no');
|
|
|
|
});
|
2013-02-05 11:32:59 +08:00
|
|
|
});
|
2014-01-18 03:12:01 +08:00
|
|
|
|
2014-01-22 11:06:23 +08:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-18 03:12:01 +08:00
|
|
|
describe("stubbing with form data", function() {
|
|
|
|
beforeEach(function() {
|
2014-01-18 03:38:27 +08:00
|
|
|
mockAjax.stubRequest("http://example.com/someApi", 'foo=bar&baz=quux').andReturn({responseText: "form", status: 201});
|
2014-01-18 03:12:01 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
var postRequest = function(data) {
|
|
|
|
var xhr = new fakeGlobal.XMLHttpRequest();
|
|
|
|
xhr.onreadystatechange = function(arguments) {
|
2014-06-02 06:37:19 +08:00
|
|
|
if (this.readyState == (this.DONE || 4)) { // IE 8 doesn't support DONE
|
2014-01-18 03:12:01 +08:00
|
|
|
response = this;
|
|
|
|
successSpy();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.open("POST", "http://example.com/someApi");
|
|
|
|
xhr.send(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
it("uses the form data stub when the data matches", function() {
|
2014-01-18 03:38:27 +08:00
|
|
|
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');
|
2014-01-18 03:12:01 +08:00
|
|
|
|
|
|
|
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() {
|
2014-01-18 03:38:27 +08:00
|
|
|
postRequest('foo=bar');
|
2014-01-18 03:12:01 +08:00
|
|
|
|
|
|
|
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!');
|
|
|
|
});
|
|
|
|
});
|
2013-02-05 11:32:59 +08:00
|
|
|
});
|