Skip to content

Commit 7f965a1

Browse files
committed
fix: deal with a corner case
1 parent 4b5672d commit 7f965a1

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

src/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ function collectMethod(obj, key, options) {
6161
return;
6262
}
6363
if (VUE_LIFECYCLE_HOOKS.includes(key) || VUE_SPECIAL_FUNCTIONS.includes(key)) {
64-
// obj[key] must be a function
64+
if (typeof obj[key] !== 'function') {
65+
throw new Error(`The property "${key}" of the class must be a Vue component function.`);
66+
}
6567
options[key] = obj[key];
6668
} else {
6769
const descriptor = Object.getOwnPropertyDescriptor(obj, key);

test/bug-5.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { mount } from '@vue/test-utils';
1010
import { nextTick } from 'vue';
1111
import { Component, toVue } from '../src';
1212

13-
function AddMessage(target, context) {
13+
function AddMessage(target) {
1414
// add a new field to the target class
1515
target.prototype.message = 'Hello';
1616
}
@@ -24,7 +24,6 @@ class Test {
2424
}
2525
const TestComponent = toVue(Test);
2626

27-
2827
describe('bug #5', () => {
2928
test('Should handle the class field added to the prototype of the class', async () => {
3029
const w1 = mount(TestComponent);

test/component.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,18 @@ describe('@Component decorator', () => {
214214
+ 'as the class method and decorated by @Watch.',
215215
);
216216
});
217+
test('@Component on class with invalid Vue method `created`', () => {
218+
expect(() => {
219+
function AddCreated(target) {
220+
target.prototype.created = 'Hello';
221+
}
222+
@Component
223+
@AddCreated
224+
class F1 {}
225+
new F1();
226+
}).toThrowWithMessage(
227+
Error,
228+
'The property "created" of the class must be a Vue component function.',
229+
);
230+
});
217231
});

test/create-decorator.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('createDecorator() function', () => {
2525
const methodName = context.name;
2626
const originalMethod = options.methods[methodName];
2727
// note that the following function can NOT be an arrow function
28-
options.methods[methodName] = function (...args) {
28+
options.methods[methodName] = function log(...args) {
2929
messages.push(`Calling ${Class.name}.${methodName}() with arguments: ${args.join(', ')}`);
3030
console.log(`${Class.name}.${methodName}: ${args.join(', ')}`);
3131
return originalMethod.apply(this, args);

0 commit comments

Comments
 (0)