2013-10-12 13:16:22 +08:00
|
|
|
describe("mockAjax", function() {
|
|
|
|
it("does not replace XMLHttpRequest until it is installed", function() {
|
|
|
|
var fakeXmlHttpRequest = jasmine.createSpy('fakeXmlHttpRequest'),
|
|
|
|
fakeGlobal = { XMLHttpRequest: fakeXmlHttpRequest },
|
|
|
|
mockAjax = new MockAjax(fakeGlobal);
|
|
|
|
|
|
|
|
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 },
|
|
|
|
mockAjax = new 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 },
|
|
|
|
mockAjax = new MockAjax(fakeGlobal);
|
|
|
|
|
|
|
|
mockAjax.install();
|
|
|
|
|
|
|
|
mockAjax.requests.track({url: '/testurl'});
|
|
|
|
mockAjax.stubs.addStub({url: '/bobcat'});
|
|
|
|
|
|
|
|
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 },
|
|
|
|
mockAjax = new 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 },
|
|
|
|
mockAjax = new 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
|
|
|
});
|
|
|
|
});
|