Skip to content

Commit 111d56e

Browse files
committed
fix: RHL should add new class methods
1 parent f5d57ee commit 111d56e

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

Diff for: src/proxy/inject.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ function mergeComponents(
4040
if (key.startsWith(PREFIX)) return
4141
const nextAttr = nextInstance[key]
4242
const prevAttr = proxyInstance[key]
43-
if (prevAttr && nextAttr) {
43+
if (nextAttr) {
4444
if (isNativeFunction(nextAttr) || isNativeFunction(prevAttr)) {
4545
// this is bound method
4646
const isSameArity = nextAttr.length === prevAttr.length
4747
const existsInPrototype =
4848
ownKeys.indexOf(key) >= 0 || ProxyComponent.prototype[key]
49-
if (isSameArity && existsInPrototype) {
49+
if ((isSameArity || !prevAttr) && existsInPrototype) {
5050
if (hasRegenerate) {
5151
injectedCode[
5252
key
@@ -109,6 +109,10 @@ function mergeComponents(
109109
} else {
110110
// key was skipped
111111
}
112+
} else {
113+
// key does not exists anymore
114+
// we could not delete it, yet #840
115+
// injectedCode[key] = null;
112116
}
113117
})
114118
} catch (e) {

Diff for: test/proxy/lifecycle-method.test.js

+48
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,52 @@ describe('lifecycle method', () => {
105105
wrapper.instance().forceUpdate()
106106
expect(wrapper.text()).toContain('PATCHED + !15')
107107
})
108+
109+
it('handle dynamic method creation', () => {
110+
class App1 extends Component {
111+
method1 = () => 41 + this.var1
112+
113+
var1 = 1
114+
115+
render() {
116+
return <div>{this.method1()}</div>
117+
}
118+
}
119+
120+
class App2 extends Component {
121+
method2 = () => 22 + this.var2
122+
123+
var2 = 2
124+
125+
/* eslint-disable */
126+
__reactstandin__regenerateByEval(key, code) {
127+
this[key] = eval(code)
128+
}
129+
/* eslint-enable */
130+
131+
render() {
132+
return (
133+
<div>
134+
{this.method1()} + {this.method2()}
135+
</div>
136+
)
137+
}
138+
}
139+
140+
const proxy = createProxy(App1)
141+
const Proxy = proxy.get()
142+
143+
const wrapper = mount(<Controller Proxy={Proxy} />)
144+
145+
expect(wrapper.text()).toContain('42')
146+
147+
proxy.update(App2)
148+
wrapper.instance().forceUpdate()
149+
150+
// first render before hot render
151+
expect(wrapper.text()).toContain('42')
152+
wrapper.instance().forceUpdate()
153+
// both methods expected to be present
154+
expect(wrapper.text()).toContain('42 + 24')
155+
})
108156
})

0 commit comments

Comments
 (0)