Skip to content

Commit

Permalink
fix(VFileInput): fix rogue change events (#8193)
Browse files Browse the repository at this point in the history
v-file-input was emitting a change event both when pressing enter, and when it was blurred

closes #8167
  • Loading branch information
nekosaur authored and johnleider committed Aug 1, 2019
1 parent b0c3c44 commit 3568df3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/vuetify/src/components/VFileInput/VFileInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ export default VTextField.extend({
const files = [...(e.target as HTMLInputElement).files || []]

this.internalValue = this.isMultiple ? files : files[0]

// Set initialValue here otherwise isFocused
// watcher in VTextField will emit a change
// event whenever the component is blurred
this.initialValue = this.internalValue
},
onKeyDown (e: KeyboardEvent) {
this.$emit('keydown', e)
},
truncateText (str: string) {
if (str.length < Number(this.truncateLength)) return str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,43 @@ describe('VFileInput.ts', () => {

expect(wrapper.html()).toMatchSnapshot()
})

// https://github.com/vuetifyjs/vuetify/issues/8167
it('should not emit change event when blurred', async () => {
const change = jest.fn()
const wrapper = mountFunction({
listeners: {
change,
},
})

const input = wrapper.find('input')

input.trigger('focus')
await wrapper.vm.$nextTick()

// TODO: Is there a better way to fake the file change event?
wrapper.vm.onInput({ target: {} })

input.trigger('blur')
await wrapper.vm.$nextTick()

expect(change).toHaveBeenCalledTimes(1)
})

it('should not emit change event when pressing enter', async () => {
const change = jest.fn()
const wrapper = mountFunction({
listeners: {
change,
},
})

const input = wrapper.find('input')

input.trigger('keydown.enter')
await wrapper.vm.$nextTick()

expect(change).not.toHaveBeenCalled()
})
})

0 comments on commit 3568df3

Please sign in to comment.