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

Better emulate jQuery by resolving successful request on JSON parse error #46

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
6 changes: 5 additions & 1 deletion lib/najax.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ function najax (uri, options, callback) {
try {
data = JSON.parse(data.replace(/[\cA-\cZ]/gi, ''))
} catch (e) {
return onError(e)
//emulate jQuery functionality (replace data with JSON parse error)
Copy link
Contributor

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

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.

data = {
state: 'parsererror',
error: 'Failed to parse JSON string: ' + e.message
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions test/test-najax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Copy link
Contributor

Choose a reason for hiding this comment

The 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))
})
Expand All @@ -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))
Expand Down Expand Up @@ -343,6 +364,22 @@ function createSuccess (done) {
}
}

function jsonSuccess (done) {
return function (data, statusText) {
expect(data.test).to.equal('ok')
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure that this should be data.test?

Choose a reason for hiding this comment

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

I believe that data.test is right. jsonSuccess is used in the new test on line 98, nock replies with {'test': 'ok'}, the whole object becomes data and we are looking at the test prop.

Of course, without the nock.cleanAll() fix or my extra najax call fix, then this test receives the wrong nock and expect(data.test) fails.

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
}