2013-10-12 13:16:22 +08:00
|
|
|
describe("withMock", function() {
|
|
|
|
var sendRequest = function(fakeGlobal) {
|
|
|
|
var xhr = new fakeGlobal.XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.open("GET", "http://example.com/someApi");
|
|
|
|
xhr.send();
|
|
|
|
};
|
|
|
|
|
|
|
|
it("installs the mock for passed in function, and uninstalls when complete", function() {
|
2014-04-04 22:29:49 +08:00
|
|
|
var xmlHttpRequest = jasmine.createSpyObj('XMLHttpRequest', ['open', 'send']),
|
|
|
|
xmlHttpRequestCtor = spyOn(window, 'XMLHttpRequest').and.returnValue(xmlHttpRequest),
|
|
|
|
fakeGlobal = {XMLHttpRequest: xmlHttpRequestCtor},
|
2013-10-12 13:16:22 +08:00
|
|
|
mockAjax = new MockAjax(fakeGlobal);
|
|
|
|
|
|
|
|
mockAjax.withMock(function() {
|
|
|
|
sendRequest(fakeGlobal);
|
2014-04-04 22:29:49 +08:00
|
|
|
expect(xmlHttpRequest.open).not.toHaveBeenCalled();
|
2013-10-12 13:16:22 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
sendRequest(fakeGlobal);
|
2014-04-04 22:29:49 +08:00
|
|
|
expect(xmlHttpRequest.open).toHaveBeenCalled();
|
2013-10-12 13:16:22 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("properly uninstalls when the passed in function throws", function() {
|
2014-04-04 22:29:49 +08:00
|
|
|
var xmlHttpRequest = jasmine.createSpyObj('XMLHttpRequest', ['open', 'send']),
|
|
|
|
xmlHttpRequestCtor = spyOn(window, 'XMLHttpRequest').and.returnValue(xmlHttpRequest),
|
|
|
|
fakeGlobal = {XMLHttpRequest: xmlHttpRequestCtor},
|
2013-10-12 13:16:22 +08:00
|
|
|
mockAjax = new MockAjax(fakeGlobal);
|
|
|
|
|
|
|
|
expect(function() {
|
|
|
|
mockAjax.withMock(function() {
|
|
|
|
throw "error"
|
|
|
|
});
|
|
|
|
}).toThrow("error");
|
|
|
|
|
|
|
|
sendRequest(fakeGlobal);
|
2014-04-04 22:29:49 +08:00
|
|
|
expect(xmlHttpRequest.open).toHaveBeenCalled();
|
2013-10-12 13:16:22 +08:00
|
|
|
});
|
|
|
|
});
|