Skip to content

Commit

Permalink
fix: binding context vm when using function without parentheses (#148)
Browse files Browse the repository at this point in the history
* fix: binding context vm when using function without parentesis #fixes 144

* improve test descriptions

Co-authored-by: Carlos Rodrigues <carlos.rodrigues@just-eat.com>
  • Loading branch information
pikax and Carlos Rodrigues authored Jun 6, 2020
1 parent 70452af commit 5b269f7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
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(markRaw(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 @@ -381,4 +381,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);
});
})
});

0 comments on commit 5b269f7

Please sign in to comment.