Skip to content

Commit

Permalink
Merge pull request #24 from github/content-type-in-payload
Browse files Browse the repository at this point in the history
Set content type in error event payload
  • Loading branch information
koddsson authored Aug 27, 2019
2 parents 72e84e0 + c4566cf commit 0d4522c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function check(autoCheckElement: AutoCheckElement) {
if (error.statusCode === 422 && error.responseText) {
if (error.contentType.includes('application/json')) {
validity = JSON.parse(error.responseText).text
} else {
} else if (error.contentType.includes('text/plain')) {
validity = error.responseText
}
}
Expand All @@ -140,7 +140,12 @@ function check(autoCheckElement: AutoCheckElement) {
input.setCustomValidity(validity)
}
autoCheckElement.dispatchEvent(new CustomEvent('error'))
input.dispatchEvent(new CustomEvent('auto-check-error', {detail: {message: error.responseText}, bubbles: true}))
input.dispatchEvent(
new CustomEvent('auto-check-error', {
detail: {message: error.responseText, contentType: error.contentType},
bubbles: true
})
)
})
.then(always, always)
}
Expand Down
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,22 @@ describe('auto-check element', function() {
assert.deepEqual('This is a warning', result)
})
})

describe('`auto-check-error` event', function() {
it('includes `Content-Type` header in event payload', function() {
return new Promise(resolve => {
const autoCheck = document.querySelector('auto-check')
const input = document.querySelector('input')
autoCheck.src = '/fail'
input.value = 'hub'
input.dispatchEvent(new InputEvent('change'))
input.addEventListener('auto-check-error', event => {
resolve(event.detail.contentType)
})
}).then(contentType => {
assert.equal('application/json', contentType)
})
})
})
})
})

0 comments on commit 0d4522c

Please sign in to comment.