Skip to content

Commit 27f1017

Browse files
freakzlikecexbrayat
authored andcommitted
fix(setValue): trigger input event for checkbox, radio and select
1 parent c3a8711 commit 27f1017

File tree

2 files changed

+85
-9
lines changed

2 files changed

+85
-9
lines changed

src/domWrapper.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
9393
}
9494

9595
element.checked = checked
96+
this.trigger('input')
9697
return this.trigger('change')
9798
}
9899

@@ -118,6 +119,8 @@ export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
118119
} else {
119120
element.value = value
120121
}
122+
123+
this.trigger('input')
121124
return this.trigger('change')
122125
} else if (tagName === 'INPUT' || tagName === 'TEXTAREA') {
123126
element.value = value
@@ -146,7 +149,9 @@ export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
146149
parentElement = parentElement.parentElement!
147150
}
148151

149-
return new DOMWrapper(parentElement).trigger('change')
152+
const parentWrapper = new DOMWrapper(parentElement)
153+
parentWrapper.trigger('input')
154+
return parentWrapper.trigger('change')
150155
}
151156
}
152157

tests/setValue.spec.ts

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ describe('setValue', () => {
3333
})
3434

3535
describe('on select and option', () => {
36+
const renderOptions = () => [
37+
h('option', { value: 'A' }),
38+
h('option', { value: 'B' })
39+
]
40+
3641
it('sets element of select value', async () => {
3742
const wrapper = mount(ComponentWithInput)
3843
const select = wrapper.find<HTMLSelectElement>('select')
@@ -42,7 +47,7 @@ describe('setValue', () => {
4247
expect(wrapper.text()).toContain('selectB')
4348

4449
expect(wrapper.emitted('change')).toHaveLength(1)
45-
expect(wrapper.emitted('input')).toBeUndefined()
50+
expect(wrapper.emitted('input')).toHaveLength(1)
4651
})
4752

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

6974
const Component = {
7075
setup() {
71-
return () =>
72-
h('select', { onChange: handle }, [
73-
h('option', { value: 'A' }),
74-
h('option', { value: 'B' })
75-
])
76+
return () => h('select', { onChange: handle }, renderOptions())
7677
}
7778
}
7879

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

100101
expect(wrapper.emitted('change')).toHaveLength(1)
101-
expect(wrapper.emitted('input')).toBeUndefined()
102+
expect(wrapper.emitted('input')).toHaveLength(1)
102103
})
103104

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

116117
expect(wrapper.emitted('change')).toHaveLength(2)
117-
expect(wrapper.emitted('input')).toBeUndefined()
118+
expect(wrapper.emitted('input')).toHaveLength(2)
119+
})
120+
121+
it('does trigger input and change event on select', async () => {
122+
const onInput = vi.fn()
123+
const onChange = vi.fn()
124+
const Comp = defineComponent({
125+
setup() {
126+
return () => h('select', { onInput, onChange }, renderOptions())
127+
}
128+
})
129+
130+
await mount(Comp).find('select').setValue('A')
131+
132+
expect(onInput).toHaveBeenCalledTimes(1)
133+
expect(onChange).toHaveBeenCalledTimes(1)
134+
})
135+
136+
it('does trigger input and change event on option select', async () => {
137+
const onInput = vi.fn()
138+
const onChange = vi.fn()
139+
const Comp = defineComponent({
140+
setup() {
141+
return () => h('select', { onInput, onChange }, renderOptions())
142+
}
143+
})
144+
145+
await mount(Comp).findAll('option')[1].setValue()
146+
147+
expect(onInput).toHaveBeenCalledTimes(1)
148+
expect(onChange).toHaveBeenCalledTimes(1)
118149
})
119150
})
120151

@@ -202,6 +233,46 @@ describe('setValue', () => {
202233
const fn = radioFoo.setValue(false)
203234
await expect(fn).rejects.toThrowError(message)
204235
})
236+
237+
it('does trigger input and change event on checkbox', async () => {
238+
const onInput = vi.fn()
239+
const onChange = vi.fn()
240+
const Comp = defineComponent({
241+
setup() {
242+
return () =>
243+
h('input', {
244+
onInput,
245+
onChange,
246+
type: 'checkbox'
247+
})
248+
}
249+
})
250+
251+
await mount(Comp).find('input').setValue()
252+
253+
expect(onInput).toHaveBeenCalledTimes(1)
254+
expect(onChange).toHaveBeenCalledTimes(1)
255+
})
256+
257+
it('does trigger input and change event on radio', async () => {
258+
const onInput = vi.fn()
259+
const onChange = vi.fn()
260+
const Comp = defineComponent({
261+
setup() {
262+
return () =>
263+
h('input', {
264+
onInput,
265+
onChange,
266+
type: 'radio'
267+
})
268+
}
269+
})
270+
271+
await mount(Comp).find('input').setValue()
272+
273+
expect(onInput).toHaveBeenCalledTimes(1)
274+
expect(onChange).toHaveBeenCalledTimes(1)
275+
})
205276
})
206277

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

0 commit comments

Comments
 (0)