Throw error on duplicate calls to MockAjax#install.

Fixes #96
This commit is contained in:
Jen Hamon 2015-03-30 21:15:34 -07:00 committed by Gregg Van Hove
parent 778966f60b
commit fb5a98e63e
3 changed files with 35 additions and 0 deletions

View File

@ -396,6 +396,10 @@ getJasmineRequireObj().MockAjax = function($ajax) {
mockAjaxFunction = $ajax.fakeRequest(global, requestTracker, stubTracker, paramParser);
this.install = function() {
if (global.XMLHttpRequest === mockAjaxFunction) {
throw "MockAjax is already installed.";
}
global.XMLHttpRequest = mockAjaxFunction;
};

View File

@ -1,4 +1,31 @@
describe("mockAjax", function() {
it("throws an error if installed multiple times", function() {
var fakeXmlHttpRequest = jasmine.createSpy('fakeXmlHttpRequest'),
fakeGlobal = { XMLHttpRequest: fakeXmlHttpRequest },
mockAjax = new window.MockAjax(fakeGlobal);
function doubleInstall() {
mockAjax.install();
mockAjax.install();
}
expect(doubleInstall).toThrow();
});
it("does not throw an error if uninstalled between installs", function() {
var fakeXmlHttpRequest = jasmine.createSpy('fakeXmlHttpRequest'),
fakeGlobal = { XMLHttpRequest: fakeXmlHttpRequest },
mockAjax = new window.MockAjax(fakeGlobal);
function sequentialInstalls() {
mockAjax.install();
mockAjax.uninstall();
mockAjax.install();
}
expect(sequentialInstalls).not.toThrow();
});
it("does not replace XMLHttpRequest until it is installed", function() {
var fakeXmlHttpRequest = jasmine.createSpy('fakeXmlHttpRequest'),
fakeGlobal = { XMLHttpRequest: fakeXmlHttpRequest },

View File

@ -7,6 +7,10 @@ getJasmineRequireObj().MockAjax = function($ajax) {
mockAjaxFunction = $ajax.fakeRequest(global, requestTracker, stubTracker, paramParser);
this.install = function() {
if (global.XMLHttpRequest === mockAjaxFunction) {
throw "MockAjax is already installed.";
}
global.XMLHttpRequest = mockAjaxFunction;
};