2013-10-12 13:16:22 +08:00
|
|
|
describe("mockAjax", function() {
|
2015-03-31 12:15:34 +08:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
it("does not replace XMLHttpRequest until it is installed", function() {
|
|
|
|
var fakeXmlHttpRequest = jasmine.createSpy('fakeXmlHttpRequest'),
|
|
|
|
fakeGlobal = { XMLHttpRequest: fakeXmlHttpRequest },
|
2014-07-29 09:38:06 +08:00
|
|
|
mockAjax = new window.MockAjax(fakeGlobal);
|
2013-10-12 13:16:22 +08:00
|
|
|
|
|
|
|
fakeGlobal.XMLHttpRequest('foo');
|
|
|
|
expect(fakeXmlHttpRequest).toHaveBeenCalledWith('foo');
|
|
|
|
fakeXmlHttpRequest.calls.reset();
|
|
|
|
|
|
|
|
mockAjax.install();
|
|
|
|
fakeGlobal.XMLHttpRequest('foo');
|
|
|
|
expect(fakeXmlHttpRequest).not.toHaveBeenCalled();
|
2011-08-08 04:01:38 +08:00
|
|
|
});
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
it("replaces the global XMLHttpRequest on uninstall", function() {
|
|
|
|
var fakeXmlHttpRequest = jasmine.createSpy('fakeXmlHttpRequest'),
|
|
|
|
fakeGlobal = { XMLHttpRequest: fakeXmlHttpRequest },
|
2014-07-29 09:38:06 +08:00
|
|
|
mockAjax = new window.MockAjax(fakeGlobal);
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
mockAjax.install();
|
|
|
|
mockAjax.uninstall();
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
fakeGlobal.XMLHttpRequest('foo');
|
|
|
|
expect(fakeXmlHttpRequest).toHaveBeenCalledWith('foo');
|
2011-08-08 04:01:38 +08:00
|
|
|
});
|
|
|
|
|
2013-11-02 05:41:35 +08:00
|
|
|
it("clears requests and stubs upon uninstall", function() {
|
|
|
|
var fakeXmlHttpRequest = jasmine.createSpy('fakeXmlHttpRequest'),
|
|
|
|
fakeGlobal = { XMLHttpRequest: fakeXmlHttpRequest },
|
2014-07-29 09:38:06 +08:00
|
|
|
mockAjax = new window.MockAjax(fakeGlobal);
|
2013-11-02 05:41:35 +08:00
|
|
|
|
|
|
|
mockAjax.install();
|
|
|
|
|
|
|
|
mockAjax.requests.track({url: '/testurl'});
|
2014-01-18 03:38:27 +08:00
|
|
|
mockAjax.stubRequest('/bobcat');
|
2013-11-02 05:41:35 +08:00
|
|
|
|
|
|
|
expect(mockAjax.requests.count()).toEqual(1);
|
|
|
|
expect(mockAjax.stubs.findStub('/bobcat')).toBeDefined();
|
|
|
|
|
|
|
|
mockAjax.uninstall();
|
|
|
|
|
|
|
|
expect(mockAjax.requests.count()).toEqual(0);
|
|
|
|
expect(mockAjax.stubs.findStub('/bobcat')).not.toBeDefined();
|
|
|
|
});
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
it("allows the httpRequest to be retrieved", function() {
|
|
|
|
var fakeXmlHttpRequest = jasmine.createSpy('fakeXmlHttpRequest'),
|
|
|
|
fakeGlobal = { XMLHttpRequest: fakeXmlHttpRequest },
|
2014-07-29 09:38:06 +08:00
|
|
|
mockAjax = new window.MockAjax(fakeGlobal);
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
mockAjax.install();
|
|
|
|
var request = new fakeGlobal.XMLHttpRequest();
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
expect(mockAjax.requests.count()).toBe(1);
|
|
|
|
expect(mockAjax.requests.mostRecent()).toBe(request);
|
2011-08-08 04:01:38 +08:00
|
|
|
});
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
it("allows the httpRequests to be cleared", function() {
|
|
|
|
var fakeXmlHttpRequest = jasmine.createSpy('fakeXmlHttpRequest'),
|
|
|
|
fakeGlobal = { XMLHttpRequest: fakeXmlHttpRequest },
|
2014-07-29 09:38:06 +08:00
|
|
|
mockAjax = new window.MockAjax(fakeGlobal);
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
mockAjax.install();
|
|
|
|
var request = new fakeGlobal.XMLHttpRequest();
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
expect(mockAjax.requests.mostRecent()).toBe(request);
|
|
|
|
mockAjax.requests.reset();
|
|
|
|
expect(mockAjax.requests.count()).toBe(0);
|
2011-08-08 04:01:38 +08:00
|
|
|
});
|
|
|
|
});
|