2013-02-05 11:32:59 +08:00
|
|
|
describe("Webmock style mocking", function() {
|
2015-06-02 01:12:13 +08:00
|
|
|
var successSpy, errorSpy, timeoutSpy, response, fakeGlobal, mockAjax;
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2014-07-11 03:44:56 +08:00
|
|
|
var sendRequest = function(fakeGlobal, url, method) {
|
2014-07-29 09:38:06 +08:00
|
|
|
url = url || "http://example.com/someApi";
|
2014-07-11 03:44:56 +08:00
|
|
|
method = method || 'GET';
|
2013-10-12 13:16:22 +08:00
|
|
|
var xhr = new fakeGlobal.XMLHttpRequest();
|
2014-07-29 09:38:06 +08:00
|
|
|
xhr.onreadystatechange = function(args) {
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-02 01:12:13 +08:00
|
|
|
xhr.onerror = function() {
|
|
|
|
errorSpy();
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.ontimeout = function() {
|
|
|
|
timeoutSpy();
|
|
|
|
};
|
|
|
|
|
2014-07-11 03:44:56 +08:00
|
|
|
xhr.open(method, 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');
|
2015-06-02 01:12:13 +08:00
|
|
|
errorSpy = jasmine.createSpy('error');
|
|
|
|
timeoutSpy = jasmine.createSpy('timeout');
|
2013-10-12 13:16:22 +08:00
|
|
|
fakeGlobal = {XMLHttpRequest: jasmine.createSpy('realXMLHttpRequest')};
|
2014-07-29 09:38:06 +08:00
|
|
|
mockAjax = new window.MockAjax(fakeGlobal);
|
2013-10-12 13:16:22 +08:00
|
|
|
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!"});
|
2015-06-02 01:12:13 +08:00
|
|
|
mockAjax.stubRequest("http://example.com/someErrorApi").andError();
|
|
|
|
mockAjax.stubRequest("http://example.com/someTimeoutApi").andTimeout();
|
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
|
|
|
});
|
|
|
|
|
2015-06-02 01:12:13 +08:00
|
|
|
it("allows a url to be setup as a stub which trigger error", function() {
|
|
|
|
sendRequest(fakeGlobal, "http://example.com/someErrorApi");
|
|
|
|
expect(errorSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("allows a url to be setup as a stub which timeouts", function() {
|
|
|
|
jasmine.clock().install();
|
|
|
|
sendRequest(fakeGlobal, "http://example.com/someTimeoutApi");
|
|
|
|
expect(timeoutSpy).toHaveBeenCalled();
|
|
|
|
jasmine.clock().uninstall();
|
|
|
|
});
|
|
|
|
|
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);
|
2014-07-31 09:46:02 +08:00
|
|
|
expect(response.getResponseHeader('Content-Type')).toEqual('application/json');
|
2013-02-05 11:32:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2015-02-17 16:58:10 +08:00
|
|
|
it("should set the responseHeaders", function() {
|
|
|
|
mockAjax.stubRequest("http://example.com/someApi").andReturn({
|
|
|
|
responseText: "hi!",
|
|
|
|
responseHeaders: [{name: "X-Custom", value: "header value"}]
|
|
|
|
});
|
|
|
|
sendRequest(fakeGlobal);
|
|
|
|
expect(response.getResponseHeader('X-Custom')).toEqual('header value');
|
|
|
|
});
|
|
|
|
|
2013-02-19 12:37:17 +08:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|