Skip to content

Commit b250b29

Browse files
feat: add setModelValue helper
1 parent 896c98d commit b250b29

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
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+
setModelValue(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/setModelValue.spec.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { mount, shallowMount } from '../src'
2+
import { defineComponent } from 'vue'
3+
4+
const PlainInputComponent = defineComponent({
5+
props: ['modelValue'],
6+
template: '<div>{{ modelValue }}</div>'
7+
})
8+
9+
const MultiInputComponent = defineComponent({
10+
props: ['foo', 'bar'],
11+
template: '<div>{{ foo }} {{ bar }}</div>'
12+
})
13+
14+
const Component = defineComponent({
15+
template:
16+
'<PlainInputComponent v-model="plain" /><MultiInputComponent v-model:foo="foo" v-model:bar="bar" />',
17+
data() {
18+
return {
19+
plain: null,
20+
foo: null,
21+
bar: null
22+
}
23+
},
24+
components: { PlainInputComponent, MultiInputComponent }
25+
})
26+
27+
describe('setModelValue', () => {
28+
describe('mount', () => {
29+
it('triggers a normal `v-model` on a Vue Component', async () => {
30+
const wrapper = mount(Component)
31+
const plain = wrapper.findComponent(PlainInputComponent)
32+
await plain.setModelValue('plain-value')
33+
expect(wrapper.text()).toContain('plain-value')
34+
})
35+
36+
it('triggers `v-model:parameter` style', async () => {
37+
const wrapper = mount(Component)
38+
const multiInput = wrapper.findComponent(MultiInputComponent)
39+
await multiInput.setModelValue('fooValue', 'foo')
40+
await multiInput.setModelValue('barValue', 'bar')
41+
expect(multiInput.text()).toContain('fooValue')
42+
expect(multiInput.text()).toContain('barValue')
43+
})
44+
})
45+
describe('shallowMount', () => {
46+
it('triggers a normal `v-model` on a Vue Component', async () => {
47+
const wrapper = shallowMount(Component)
48+
const plain = wrapper.findComponent(PlainInputComponent)
49+
await plain.setModelValue('plain-value')
50+
expect(wrapper.vm.plain).toEqual('plain-value')
51+
})
52+
53+
it('triggers `v-model:parameter` style', async () => {
54+
const wrapper = shallowMount(Component)
55+
const multiInput = wrapper.findComponent(MultiInputComponent)
56+
await multiInput.setModelValue('fooValue', 'foo')
57+
await multiInput.setModelValue('barValue', 'bar')
58+
expect(wrapper.vm.foo).toEqual('fooValue')
59+
expect(wrapper.vm.bar).toEqual('barValue')
60+
})
61+
})
62+
})

0 commit comments

Comments
 (0)