Skip to content

Commit 4b5672d

Browse files
committed
fix: fix bug #5, @component cannot handle the class fields added to the prototype of the class
1 parent b961c47 commit 4b5672d

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ function collectMethod(obj, key, options) {
6565
options[key] = obj[key];
6666
} else {
6767
const descriptor = Object.getOwnPropertyDescriptor(obj, key);
68-
if (descriptor.value) { // deal with class methods
68+
if (typeof descriptor.value === 'function') { // deal with class methods
6969
options.methods[key] = descriptor.value;
70-
} else { // deal with computed properties
70+
} else if (descriptor.value === undefined) { // deal with computed properties
7171
options.computed[key] = {
7272
get: descriptor.get,
7373
set: descriptor.set,
7474
};
75+
} else { // deal with class fields
76+
options.fields[key] = descriptor.value;
7577
}
7678
}
7779
}

test/bug-5.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*******************************************************************************
2+
*
3+
* Copyright (c) 2022 - 2023.
4+
* Haixing Hu, Qubit Co. Ltd.
5+
*
6+
* All rights reserved.
7+
*
8+
******************************************************************************/
9+
import { mount } from '@vue/test-utils';
10+
import { nextTick } from 'vue';
11+
import { Component, toVue } from '../src';
12+
13+
function AddMessage(target, context) {
14+
// add a new field to the target class
15+
target.prototype.message = 'Hello';
16+
}
17+
18+
@Component({
19+
template: '<div><div id="value">{{ value }}</div><div id="message">{{ message }}</div></div>',
20+
})
21+
@AddMessage
22+
class Test {
23+
value = 123;
24+
}
25+
const TestComponent = toVue(Test);
26+
27+
28+
describe('bug #5', () => {
29+
test('Should handle the class field added to the prototype of the class', async () => {
30+
const w1 = mount(TestComponent);
31+
const v1 = w1.vm;
32+
expect(v1.value).toBe(123);
33+
expect(v1.message).toBe('Hello');
34+
const w2 = mount(TestComponent);
35+
const v2 = w2.vm;
36+
expect(v2.value).toBe(123);
37+
expect(v2.message).toBe('Hello');
38+
v2.message = 'World';
39+
expect(v1.message).toBe('Hello');
40+
expect(v2.message).toBe('World');
41+
await nextTick();
42+
const m1 = w1.get('#message');
43+
const m2 = w2.get('#message');
44+
expect(m1.text()).toBe('Hello');
45+
expect(m2.text()).toBe('World');
46+
});
47+
});

0 commit comments

Comments
 (0)