Skip to content

Commit

Permalink
Merge pull request #42 from github/no-change
Browse files Browse the repository at this point in the history
Remove change event listeners
  • Loading branch information
muan authored Dec 10, 2019
2 parents 6ce5cea + 4c46835 commit 61f28d4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
4 changes: 0 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export default class AutoCheckElement extends HTMLElement {
const state = {check: checker, controller: null}
states.set(this, state)

input.addEventListener('change', setLoadingState)
input.addEventListener('input', setLoadingState)
input.addEventListener('change', checker)
input.addEventListener('input', checker)
input.autocomplete = 'off'
input.spellcheck = false
Expand All @@ -36,9 +34,7 @@ export default class AutoCheckElement extends HTMLElement {
if (!state) return
states.delete(this)

input.removeEventListener('change', setLoadingState)
input.removeEventListener('input', setLoadingState)
input.removeEventListener('change', state.check)
input.removeEventListener('input', state.check)
input.setCustomValidity('')
}
Expand Down
38 changes: 19 additions & 19 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,27 @@ describe('auto-check element', function() {
})

it('invalidates the input element on keypress', async function() {
const inputEvent = once(input, 'change')
triggerChange(input, 'hub')
const inputEvent = once(input, 'input')
triggerInput(input, 'hub')
await inputEvent
assert.isFalse(input.checkValidity())
})

it('invalidates input request is in-flight', async function() {
triggerChange(input, 'hub')
triggerInput(input, 'hub')
await once(checker, 'loadstart')
assert.isFalse(input.checkValidity())
})

it('invalidates input with failed server response', async function() {
checker.src = '/fail'
triggerChange(input, 'hub')
triggerInput(input, 'hub')
await once(input, 'auto-check-complete')
assert.isFalse(input.checkValidity())
})

it('validates input with successful server response', async function() {
triggerChange(input, 'hub')
triggerInput(input, 'hub')
await once(input, 'auto-check-complete')
assert.isTrue(input.checkValidity())
})
Expand All @@ -77,7 +77,7 @@ describe('auto-check element', function() {
event.detail.setValidity('Checking with server')
resolve()
})
triggerChange(input, 'hub')
triggerInput(input, 'hub')
})
await send
assert(!input.validity.valid)
Expand All @@ -91,7 +91,7 @@ describe('auto-check element', function() {
event.detail.setValidity('A custom error')
resolve()
})
triggerChange(input, 'hub')
triggerInput(input, 'hub')
})
await error
assert(!input.validity.valid)
Expand All @@ -103,7 +103,7 @@ describe('auto-check element', function() {
checker.required = false
input.value = 'hub'
assert.isTrue(input.checkValidity())
input.dispatchEvent(new InputEvent('change'))
input.dispatchEvent(new InputEvent('input'))
await once(input, 'auto-check-complete')
assert.isTrue(input.checkValidity())
})
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('auto-check element', function() {
checker.addEventListener('loadend', track)

const completed = Promise.all([once(checker, 'loadstart'), once(checker, 'load'), once(checker, 'loadend')])
triggerChange(input, 'hub')
triggerInput(input, 'hub')
await completed

assert.deepEqual(['loadstart', 'load', 'loadend'], events)
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('auto-check element', function() {

it('emits auto-check-send on change', function(done) {
input.addEventListener('auto-check-send', () => done())
triggerChange(input, 'hub')
triggerInput(input, 'hub')
})

it('emits auto-check-start on input', function(done) {
Expand All @@ -189,7 +189,7 @@ describe('auto-check element', function() {

it('emits auto-check-start on change', function(done) {
input.addEventListener('auto-check-start', () => done())
triggerChange(input, 'hub')
triggerInput(input, 'hub')
})

it('emits auto-check-send 300 milliseconds after keypress', function(done) {
Expand All @@ -199,23 +199,23 @@ describe('auto-check element', function() {
})

it('emits auto-check-success when server responds with 200 OK', async function() {
triggerChange(input, 'hub')
triggerInput(input, 'hub')
const event = await once(input, 'auto-check-success')
const result = await event.detail.response.text()
assert.equal('This is a warning', result)
})

it('emits auto-check-error event when server returns an error response', async function() {
checker.src = '/fail'
triggerChange(input, 'hub')
triggerInput(input, 'hub')
const event = await once(input, 'auto-check-error')
const result = await event.detail.response.text()
assert.equal('This is an error', result)
})

it('emits auto-check-complete event at the end of the lifecycle', function(done) {
input.addEventListener('auto-check-complete', () => done())
triggerChange(input, 'hub')
triggerInput(input, 'hub')
})

it('emits auto-check-send event before checking if there is a duplicate request', function(done) {
Expand All @@ -229,15 +229,15 @@ describe('auto-check element', function() {
})

input.value = 'hub'
input.dispatchEvent(new InputEvent('change'))
input.dispatchEvent(new InputEvent('change'))
input.dispatchEvent(new InputEvent('input'))
input.dispatchEvent(new InputEvent('input'))
})

it('do not emit if essential attributes are missing', async function() {
const events = []
checker.removeAttribute('src')
input.addEventListener('auto-check-start', event => events.push(event.type))
triggerChange(input, 'hub')
triggerInput(input, 'hub')
assert.deepEqual(events, [])
})
})
Expand All @@ -249,7 +249,7 @@ function once(element, eventName) {
})
}

function triggerChange(input, value) {
function triggerInput(input, value) {
input.value = value
return input.dispatchEvent(new InputEvent('change'))
return input.dispatchEvent(new InputEvent('input'))
}

0 comments on commit 61f28d4

Please sign in to comment.