fix the prototype chain of the FakeXMLHttpRequest object

the prototype of the FakeXMLHttpRequest object will now be an XMLHttpRequest object, including properties such as 'withCredentials', not the XMLHttpRequest constructor function
This commit is contained in:
Peet Goddard 2014-04-04 15:29:49 +01:00
parent 7bf2ab6a99
commit 7acac4b69f
3 changed files with 19 additions and 10 deletions

View File

@ -101,7 +101,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
this.requestHeaders = {};
}
extend(FakeXMLHttpRequest.prototype, window.XMLHttpRequest);
extend(FakeXMLHttpRequest.prototype, new window.XMLHttpRequest());
extend(FakeXMLHttpRequest.prototype, {
open: function() {
this.method = arguments[0];

View File

@ -2,8 +2,9 @@ describe("FakeXMLHttpRequest", function() {
var xhr;
var xhr2;
beforeEach(function() {
var realXMLHttpRequest = jasmine.createSpy('realRequest'),
fakeGlobal = {XMLHttpRequest: realXMLHttpRequest},
var realXMLHttpRequest = {someOtherProperty: 'someValue'},
realXMLHttpRequestCtor = spyOn(window, 'XMLHttpRequest').and.returnValue(realXMLHttpRequest),
fakeGlobal = {XMLHttpRequest: realXMLHttpRequestCtor},
mockAjax = new MockAjax(fakeGlobal);
mockAjax.install();
xhr = new fakeGlobal.XMLHttpRequest();
@ -118,4 +119,10 @@ describe("FakeXMLHttpRequest", function() {
expect(data['some=thing']).toEqual(['else entirely']);
});
});
describe("when a fake XMLHttpRequest is created", function() {
it("inherits the properties of the real XMLHttpRequest object", function() {
expect(xhr.someOtherProperty).toBe('someValue');
})
})
});

View File

@ -7,22 +7,24 @@ describe("withMock", function() {
};
it("installs the mock for passed in function, and uninstalls when complete", function() {
var xmlHttpRequest = spyOn(window, 'XMLHttpRequest').and.returnValue({open: function() {}, send: function() {}}),
fakeGlobal = {XMLHttpRequest: xmlHttpRequest},
var xmlHttpRequest = jasmine.createSpyObj('XMLHttpRequest', ['open', 'send']),
xmlHttpRequestCtor = spyOn(window, 'XMLHttpRequest').and.returnValue(xmlHttpRequest),
fakeGlobal = {XMLHttpRequest: xmlHttpRequestCtor},
mockAjax = new MockAjax(fakeGlobal);
mockAjax.withMock(function() {
sendRequest(fakeGlobal);
expect(xmlHttpRequest).not.toHaveBeenCalled();
expect(xmlHttpRequest.open).not.toHaveBeenCalled();
});
sendRequest(fakeGlobal);
expect(xmlHttpRequest).toHaveBeenCalled();
expect(xmlHttpRequest.open).toHaveBeenCalled();
});
it("properly uninstalls when the passed in function throws", function() {
var xmlHttpRequest = spyOn(window, 'XMLHttpRequest').and.returnValue({open: function() {}, send: function() {}}),
fakeGlobal = {XMLHttpRequest: xmlHttpRequest},
var xmlHttpRequest = jasmine.createSpyObj('XMLHttpRequest', ['open', 'send']),
xmlHttpRequestCtor = spyOn(window, 'XMLHttpRequest').and.returnValue(xmlHttpRequest),
fakeGlobal = {XMLHttpRequest: xmlHttpRequestCtor},
mockAjax = new MockAjax(fakeGlobal);
expect(function() {
@ -32,6 +34,6 @@ describe("withMock", function() {
}).toThrow("error");
sendRequest(fakeGlobal);
expect(xmlHttpRequest).toHaveBeenCalled();
expect(xmlHttpRequest.open).toHaveBeenCalled();
});
});