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

fix: binding context vm when using function without parentheses #148

Merged
merged 2 commits into from
Jun 6, 2020
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
4 changes: 4 additions & 0 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ export function mixin(Vue: VueConstructor) {
if (isReactive(bindingValue)) {
bindingValue = ref(bindingValue);
} else {
// bind function to the vm, this will make `this` = vm
if (isFunction(bindingValue)){
bindingValue = bindingValue.bind(vm);
}
// a non-reactive should not don't get reactivity
bindingValue = ref(nonReactive(bindingValue));
}
Expand Down
42 changes: 42 additions & 0 deletions test/setup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,46 @@ describe('setup', () => {
})
.then(done);
});

describe('Methods', () => {
it('binds methods when calling with parenthesis', async ()=>{
let context = null;
const contextFunction = jest.fn(function (){
context = this
});

const vm = new Vue({
template: '<div><button @click="contextFunction()"/></div>',
setup() {
return {
contextFunction
}
}
}).$mount();

await vm.$el.querySelector('button').click();
expect(contextFunction).toBeCalled();
expect(context).toBe(vm);
});

it('binds methods when calling without parenthesis', async () => {
let context = null;
const contextFunction = jest.fn(function (){
context = this
});

const vm = new Vue({
template: '<div><button @click="contextFunction"/></div>',
setup() {
return {
contextFunction
}
}
}).$mount();

await vm.$el.querySelector('button').click();
expect(contextFunction).toBeCalled();
expect(context).toBe(vm);
});
})
});