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 all 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
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"route-recognizer": "~0.1.1"
},
"devDependencies": {
"jquery": "~2.1.0"
"jquery": "~2.1.0",
"jquery-1": "https://code.jquery.com/jquery-1.11.3.min.js"
}
}
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function(config) {
files: [
'bower_components/FakeXMLHttpRequest/fake_xml_http_request.js',
'bower_components/route-recognizer/dist/route-recognizer.js',
'bower_components/jquery-1/index.js',
'bower_components/jquery/dist/jquery.js',
'pretender.js',
'test/**/*.js'
Expand Down
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 @@ -193,6 +192,8 @@ function interceptor(pretender) {
}
}

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

var i;
for (i = 0; i < evts.length; i++) {
createHandler(evts[i]);
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
40 changes: 37 additions & 3 deletions test/passthrough_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ asyncTest('allows matched paths to be pass-through', function(assert) {
});
});

asyncTest('passthrough request calls jQuery v1 handler', function(assert) {
var jQuery2 = jQuery.noConflict(true);
pretender.get('/some/:route', pretender.passthrough);

assert.ok(/^1/.test(jQuery.fn.jquery));
jQuery.ajax({
url: '/some/path',
error: function(xhr) {
assert.equal(xhr.status, 404);
jQuery = $ = jQuery2;
assert.ok(/^2/.test(jQuery.fn.jquery));
QUnit.start();
}
});
});

asyncTest('asynchronous request with pass-through has timeout, withCredentials and onprogress event', function(assert) {
function testXHR() {
this.pretender = pretender;
Expand Down Expand Up @@ -98,17 +114,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 +150,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 +179,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