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

Bug: Emit doesn't update 2 way bound props #393

Merged
merged 5 commits into from
Feb 17, 2021
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
14 changes: 13 additions & 1 deletion src/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function recordEvent(
events: Events,
vm: ComponentInternalInstance,
event: string,
args: Events[number]
args: unknown[]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This I typed wrong in the previous PR, fixed here

): void {
// Functional component wrapper creates a parent component
let wrapperVm = vm
Expand All @@ -64,4 +64,16 @@ 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]
}
}
29 changes: 29 additions & 0 deletions tests/props.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -82,4 +83,32 @@ describe('props', () => {
object: {}
})
})

it('should return the updated props on 2 way binding', async () => {
Copy link
Contributor Author

@nandi95 nandi95 Feb 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is probably the wrong place for this test, please advise where it would be best.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is probably fine. Probably could re-think the test layout at some point.

const component = defineComponent({
template: '<button @click="increment"></button>',
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)
})
})