Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix readystatechange #130

Merged
merged 4 commits into from
Dec 11, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions pretender.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,22 @@ function interceptor(pretender) {
}
};

// event types to handle on the xhr
var evts = ['error', 'timeout', 'abort'];

// event types to handle on the xhr.upload
var uploadEvents = ['progress'];
function createPassthrough(fakeXHR) {
// event types to handle on the xhr
var evts = ['error', 'timeout', 'abort', 'readystatechange'];

// properties to copy from the native xhr to fake xhr
var lifecycleProps = ['readyState', 'responseText', 'responseXML', 'status', 'statusText'];
// event types to handle on the xhr.upload
var uploadEvents = ['progress'];

// properties to copy from the native xhr to fake xhr
var lifecycleProps = ['readyState', 'responseText', 'responseXML', 'status', 'statusText'];

function createPassthrough(fakeXHR) {
var xhr = fakeXHR._passthroughRequest = new pretender._nativeXMLHttpRequest();

// Use onload instead of onreadystatechange if the browser supports it
// Use onload if the browser supports it
if ('onload' in xhr) {
evts.push('load');
} else {
evts.push('readystatechange');
}

// add progress event for async calls
Expand Down Expand Up @@ -183,6 +182,8 @@ function interceptor(pretender) {
};
}

xhr.open(fakeXHR.method, fakeXHR.url, fakeXHR.async, fakeXHR.username, fakeXHR.password);

// set the on- handler on the native xhr's `upload` property for
// the given eventType
function createUploadHandler(eventType) {
Expand All @@ -201,7 +202,6 @@ function interceptor(pretender) {
createUploadHandler(uploadEvents[i]);
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was it necessary to move where the xhr.open is being called?

xhr.open(fakeXHR.method, fakeXHR.url, fakeXHR.async, fakeXHR.username, fakeXHR.password);
if (fakeXHR.async) {
xhr.timeout = fakeXHR.timeout;
xhr.withCredentials = fakeXHR.withCredentials;
Expand Down
24 changes: 21 additions & 3 deletions test/passthrough_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,19 @@ asyncTest('synchronous request does not have timeout, withCredentials and onprog

test('asynchronous request fires events', function(assert) {
var done = assert.async();
assert.expect(4);
assert.expect(6);

pretender.post('/some/:route', pretender.passthrough);

var onEvents = {
load: false,
progress: false
progress: false,
readystatechange: false
};
var listenerEvents = {
load: false,
progress: false
progress: false,
readystatechange: false
};

var xhr = new window.XMLHttpRequest();
Expand All @@ -132,6 +134,20 @@ test('asynchronous request fires events', function(assert) {
finishNext();
};

xhr.addEventListener('readystatechange', function _load() {
if (xhr.readyState == 4) {
listenerEvents.readystatechange = true;
finishNext();
}
});

xhr.onreadystatechange = function _onload() {
if (xhr.readyState == 4) {
onEvents.readystatechange = true;
finishNext();
}
};

xhr.send();

// call `finish` in next tick to ensure both load event handlers
Expand All @@ -147,9 +163,11 @@ test('asynchronous request fires events', function(assert) {

assert.ok(onEvents.load, 'onload called');
assert.ok(onEvents.progress, 'onprogress called');
assert.ok(onEvents.readystatechange, 'onreadystate called');

assert.ok(listenerEvents.load, 'load listener called');
assert.ok(listenerEvents.progress, 'progress listener called');
assert.ok(listenerEvents.readystatechange, 'readystate listener called');

done();
}
Expand Down