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

fix(setValue): trigger input event for checkbox, radio and select #2019

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/domWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
}

element.checked = checked
this.trigger('input')
return this.trigger('change')
}

Expand All @@ -118,6 +119,8 @@ export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
} else {
element.value = value
}

this.trigger('input')
return this.trigger('change')
} else if (tagName === 'INPUT' || tagName === 'TEXTAREA') {
element.value = value
Expand Down Expand Up @@ -146,7 +149,9 @@ export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
parentElement = parentElement.parentElement!
}

return new DOMWrapper(parentElement).trigger('change')
const parentWrapper = new DOMWrapper(parentElement)
parentWrapper.trigger('input')
return parentWrapper.trigger('change')
}
}

Expand Down
87 changes: 79 additions & 8 deletions tests/setValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ describe('setValue', () => {
})

describe('on select and option', () => {
const renderOptions = () => [
h('option', { value: 'A' }),
h('option', { value: 'B' })
]

it('sets element of select value', async () => {
const wrapper = mount(ComponentWithInput)
const select = wrapper.find<HTMLSelectElement>('select')
Expand All @@ -42,7 +47,7 @@ describe('setValue', () => {
expect(wrapper.text()).toContain('selectB')

expect(wrapper.emitted('change')).toHaveLength(1)
expect(wrapper.emitted('input')).toBeUndefined()
expect(wrapper.emitted('input')).toHaveLength(1)
})

it('as an option of a select as selected', async () => {
Expand All @@ -68,11 +73,7 @@ describe('setValue', () => {

const Component = {
setup() {
return () =>
h('select', { onChange: handle }, [
h('option', { value: 'A' }),
h('option', { value: 'B' })
])
return () => h('select', { onChange: handle }, renderOptions())
}
}

Expand All @@ -98,7 +99,7 @@ describe('setValue', () => {
expect(wrapper.vm.multiselectVal).toEqual(['selectA', 'selectC'])

expect(wrapper.emitted('change')).toHaveLength(1)
expect(wrapper.emitted('input')).toBeUndefined()
expect(wrapper.emitted('input')).toHaveLength(1)
})

it('overrides elements of multiselect', async () => {
Expand All @@ -114,7 +115,37 @@ describe('setValue', () => {
expect(wrapper.vm.multiselectVal).toEqual(['selectB'])

expect(wrapper.emitted('change')).toHaveLength(2)
expect(wrapper.emitted('input')).toBeUndefined()
expect(wrapper.emitted('input')).toHaveLength(2)
})

it('does trigger input and change event on select', async () => {
const onInput = vi.fn()
const onChange = vi.fn()
const Comp = defineComponent({
setup() {
return () => h('select', { onInput, onChange }, renderOptions())
}
})

await mount(Comp).find('select').setValue('A')

expect(onInput).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledTimes(1)
})

it('does trigger input and change event on option select', async () => {
const onInput = vi.fn()
const onChange = vi.fn()
const Comp = defineComponent({
setup() {
return () => h('select', { onInput, onChange }, renderOptions())
}
})

await mount(Comp).findAll('option')[1].setValue()

expect(onInput).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledTimes(1)
})
})

Expand Down Expand Up @@ -202,6 +233,46 @@ describe('setValue', () => {
const fn = radioFoo.setValue(false)
await expect(fn).rejects.toThrowError(message)
})

it('does trigger input and change event on checkbox', async () => {
const onInput = vi.fn()
const onChange = vi.fn()
const Comp = defineComponent({
setup() {
return () =>
h('input', {
onInput,
onChange,
type: 'checkbox'
})
}
})

await mount(Comp).find('input').setValue()

expect(onInput).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledTimes(1)
})

it('does trigger input and change event on radio', async () => {
const onInput = vi.fn()
const onChange = vi.fn()
const Comp = defineComponent({
setup() {
return () =>
h('input', {
onInput,
onChange,
type: 'radio'
})
}
})

await mount(Comp).find('input').setValue()

expect(onInput).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledTimes(1)
})
})

it('throws error if element is not valid', () => {
Expand Down