-
Notifications
You must be signed in to change notification settings - Fork 43
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
Better emulate jQuery by resolving successful request on JSON parse error #46
base: master
Are you sure you want to change the base?
Changes from 5 commits
2d605b3
549eded
ef033f5
fe4ff6d
da1ee3c
616e3bb
9655ef5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,14 @@ describe('url', function (next) { | |
} | ||
|
||
it('should accept plain URL', function (done) { | ||
// An extra nock seems to be being | ||
// registered and affecting the subsequent tests | ||
// (I beleive it is caused by: | ||
// https://github.com/najaxjs/najax/commit/54cfac6aa0b7cf8a89efae0f39d1f054c8859de0 | ||
// commenting out the 'default to "GET"' test also fixes this) | ||
// make an extra call to najax in order to clear it | ||
najax({url: 'http://www.example.com'}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah sorry about this, we should probably clear all the nocks after each test? e.g. afterEach(function () {
nock.cleanAll()
}) |
||
|
||
mockPlain('get') | ||
najax('http://www.example.com', createSuccess(done)) | ||
}) | ||
|
@@ -82,6 +90,19 @@ describe('url', function (next) { | |
najax({ url: 'http://www.example.com' }, createSuccess(done)) | ||
}) | ||
|
||
it('should not fail when result is broken JSON', function (done) { | ||
mockPlain('get') | ||
najax({url: 'http://www.example.com', dataType: 'json'}, jsonError(done)) | ||
}) | ||
|
||
it('should succeed when result is good JSON', function (done) { | ||
nock('http://www.example.com') | ||
.get('/') | ||
.reply(200, {'test': 'ok'}) | ||
|
||
najax({url: 'http://www.example.com', dataType: 'json'}, jsonSuccess(done)) | ||
}) | ||
|
||
it('should parse auth from the url', function (done) { | ||
mockAuth('get') | ||
najax({ url: 'http://' + authString + '@www.example.com' }, createSuccess(done)) | ||
|
@@ -343,6 +364,22 @@ function createSuccess (done) { | |
} | ||
} | ||
|
||
function jsonSuccess (done) { | ||
return function (data, statusText) { | ||
expect(data.test).to.equal('ok') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure that this should be data.test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that Of course, without the |
||
expect(statusText).to.equal('success') | ||
done() | ||
} | ||
} | ||
|
||
function jsonError (done) { | ||
return function (data, statusText) { | ||
expect(data.state).to.equal('parsererror') | ||
expect(statusText).to.equal('success') | ||
done() | ||
} | ||
} | ||
|
||
function error (e) { | ||
throw e | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
standard requires that you start of each comment with a space, e.g.
// emulate jQuery
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh.. this whole time I was misreading the error and thought it was talking about the test-najax.js file.