From dece6e42f246e5669d3f99ee0b763f06441232ac Mon Sep 17 00:00:00 2001 From: Nandor Kraszlan Date: Tue, 16 Feb 2021 12:44:38 +0000 Subject: [PATCH 1/4] bug: added test case --- tests/props.spec.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/props.spec.ts b/tests/props.spec.ts index d2e543fd3..ebb852e3b 100644 --- a/tests/props.spec.ts +++ b/tests/props.spec.ts @@ -1,6 +1,7 @@ import { mount } from '../src' import WithProps from './components/WithProps.vue' import Hello from './components/Hello.vue' +import { defineComponent } from 'vue' describe('props', () => { it('returns a single prop applied to a component', () => { @@ -82,4 +83,32 @@ describe('props', () => { object: {} }) }) + + it('should return the updated props on 2 way binding', async () => { + const component = defineComponent({ + template: '', + props: { + modelValue: { + type: Number, + required: true + } + }, + emits: ['update:modelValue'], + setup(props, ctx) { + return { + increment: () => ctx.emit('update:modelValue', props.modelValue + 1) + } + } + }); + + const wrapper = mount(component, { + props: { + modelValue: 1 + } + }) + + expect(wrapper.props('modelValue')).toBe(1); + await wrapper.trigger('click'); + expect(wrapper.props('modelValue')).toBe(2); + }) }) From 09a12391604e5e9f451e8f702814cdc4d8d2bfdd Mon Sep 17 00:00:00 2001 From: Nandor Kraszlan Date: Tue, 16 Feb 2021 17:17:06 +0000 Subject: [PATCH 2/4] fix: Added prop update on emit for v-model --- src/emit.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/emit.ts b/src/emit.ts index 27e292534..05b06c883 100644 --- a/src/emit.ts +++ b/src/emit.ts @@ -48,7 +48,7 @@ function recordEvent( events: Events, vm: ComponentInternalInstance, event: string, - args: Events[number] + args: unknown[] ): void { // Functional component wrapper creates a parent component let wrapperVm = vm @@ -64,4 +64,12 @@ function recordEvent( // Record the event message sent by the emit events[cid][event].push(args) + + if (event.startsWith('update:')) { + if (args.length !== 1) { + throw new Error('Two-way bound properties have to emit a single value.' + args.length + ' values given.') + } + + vm.props[event.slice('update:'.length)] = args[0] + } } From a3ad28e227bac1b740459b9d29971d47f3716ffc Mon Sep 17 00:00:00 2001 From: Nandor Kraszlan Date: Tue, 16 Feb 2021 17:19:00 +0000 Subject: [PATCH 3/4] fix: Fixed lint issues --- src/emit.ts | 6 +++++- tests/props.spec.ts | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/emit.ts b/src/emit.ts index 05b06c883..19a5f4046 100644 --- a/src/emit.ts +++ b/src/emit.ts @@ -67,7 +67,11 @@ function recordEvent( if (event.startsWith('update:')) { if (args.length !== 1) { - throw new Error('Two-way bound properties have to emit a single value.' + args.length + ' values given.') + throw new Error( + 'Two-way bound properties have to emit a single value.' + + args.length + + ' values given.' + ) } vm.props[event.slice('update:'.length)] = args[0] diff --git a/tests/props.spec.ts b/tests/props.spec.ts index ebb852e3b..cff91719f 100644 --- a/tests/props.spec.ts +++ b/tests/props.spec.ts @@ -99,7 +99,7 @@ describe('props', () => { increment: () => ctx.emit('update:modelValue', props.modelValue + 1) } } - }); + }) const wrapper = mount(component, { props: { @@ -107,8 +107,8 @@ describe('props', () => { } }) - expect(wrapper.props('modelValue')).toBe(1); - await wrapper.trigger('click'); - expect(wrapper.props('modelValue')).toBe(2); + expect(wrapper.props('modelValue')).toBe(1) + await wrapper.trigger('click') + expect(wrapper.props('modelValue')).toBe(2) }) }) From 1ef3f86714b486767e06c71405c650fd60146b35 Mon Sep 17 00:00:00 2001 From: nandi95 <41805560+nandi95@users.noreply.github.com> Date: Wed, 17 Feb 2021 00:41:54 +0000 Subject: [PATCH 4/4] fix: update spacing --- src/emit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/emit.ts b/src/emit.ts index 19a5f4046..74fd44c8e 100644 --- a/src/emit.ts +++ b/src/emit.ts @@ -68,7 +68,7 @@ function recordEvent( if (event.startsWith('update:')) { if (args.length !== 1) { throw new Error( - 'Two-way bound properties have to emit a single value.' + + 'Two-way bound properties have to emit a single value. ' + args.length + ' values given.' )