Skip to content

Commit bc40317

Browse files
authored
Merge pull request #133 from vuejs/feat/set-model-value
feat: add setValue for setting v-model on Vue component form elements
2 parents 896c98d + 4c44185 commit bc40317

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/vue-wrapper.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ export class VueWrapper<T extends ComponentPublicInstance> {
197197
return nextTick()
198198
}
199199

200+
setValue(value: any, prop?: string): Promise<void> {
201+
const propEvent = prop || 'modelValue'
202+
// @ts-ignore
203+
this.vm.$emit(`update:${propEvent}`, value)
204+
return this.vm.$nextTick()
205+
}
206+
200207
trigger(eventString: string, options?: TriggerOptions) {
201208
const rootElementWrapper = new DOMWrapper(this.element)
202209
return rootElementWrapper.trigger(eventString, options)

tests/setValue.spec.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defineComponent, h } from 'vue'
2-
import { mount } from '../src'
2+
import { mount, shallowMount } from '../src'
33
import ComponentWithInput from './components/ComponentWithInput.vue'
44

55
describe('setValue', () => {
@@ -177,4 +177,64 @@ describe('setValue', () => {
177177
const fn = () => input.setValue('')
178178
expect(fn).toThrowError(message)
179179
})
180+
181+
describe('on component instance', () => {
182+
const PlainInputComponent = defineComponent({
183+
props: ['modelValue'],
184+
template: '<div>{{ modelValue }}</div>'
185+
})
186+
187+
const MultiInputComponent = defineComponent({
188+
props: ['foo', 'bar'],
189+
template: '<div>{{ foo }} {{ bar }}</div>'
190+
})
191+
192+
const Component = defineComponent({
193+
template:
194+
'<PlainInputComponent v-model="plain" /><MultiInputComponent v-model:foo="foo" v-model:bar="bar" />',
195+
data() {
196+
return {
197+
plain: null,
198+
foo: null,
199+
bar: null
200+
}
201+
},
202+
components: { PlainInputComponent, MultiInputComponent }
203+
})
204+
205+
describe('mount', () => {
206+
it('triggers a normal `v-model` on a Vue Component', async () => {
207+
const wrapper = mount(Component)
208+
const plain = wrapper.findComponent(PlainInputComponent)
209+
await plain.setValue('plain-value')
210+
expect(wrapper.text()).toContain('plain-value')
211+
})
212+
213+
it('triggers `v-model:parameter` style', async () => {
214+
const wrapper = mount(Component)
215+
const multiInput = wrapper.findComponent(MultiInputComponent)
216+
await multiInput.setValue('fooValue', 'foo')
217+
await multiInput.setValue('barValue', 'bar')
218+
expect(multiInput.text()).toContain('fooValue')
219+
expect(multiInput.text()).toContain('barValue')
220+
})
221+
})
222+
describe('shallowMount', () => {
223+
it('triggers a normal `v-model` on a Vue Component', async () => {
224+
const wrapper = shallowMount(Component)
225+
const plain = wrapper.findComponent(PlainInputComponent)
226+
await plain.setValue('plain-value')
227+
expect(wrapper.vm.plain).toEqual('plain-value')
228+
})
229+
230+
it('triggers `v-model:parameter` style', async () => {
231+
const wrapper = shallowMount(Component)
232+
const multiInput = wrapper.findComponent(MultiInputComponent)
233+
await multiInput.setValue('fooValue', 'foo')
234+
await multiInput.setValue('barValue', 'bar')
235+
expect(wrapper.vm.foo).toEqual('fooValue')
236+
expect(wrapper.vm.bar).toEqual('barValue')
237+
})
238+
})
239+
})
180240
})

0 commit comments

Comments
 (0)