Skip to content

Commit

Permalink
fix: RHL should add new class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed May 24, 2018
1 parent f5d57ee commit 111d56e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/proxy/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ function mergeComponents(
if (key.startsWith(PREFIX)) return
const nextAttr = nextInstance[key]
const prevAttr = proxyInstance[key]
if (prevAttr && nextAttr) {
if (nextAttr) {
if (isNativeFunction(nextAttr) || isNativeFunction(prevAttr)) {
// this is bound method
const isSameArity = nextAttr.length === prevAttr.length
const existsInPrototype =
ownKeys.indexOf(key) >= 0 || ProxyComponent.prototype[key]
if (isSameArity && existsInPrototype) {
if ((isSameArity || !prevAttr) && existsInPrototype) {
if (hasRegenerate) {
injectedCode[
key
Expand Down Expand Up @@ -109,6 +109,10 @@ function mergeComponents(
} else {
// key was skipped
}
} else {
// key does not exists anymore
// we could not delete it, yet #840
// injectedCode[key] = null;
}
})
} catch (e) {
Expand Down
48 changes: 48 additions & 0 deletions test/proxy/lifecycle-method.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,52 @@ describe('lifecycle method', () => {
wrapper.instance().forceUpdate()
expect(wrapper.text()).toContain('PATCHED + !15')
})

it('handle dynamic method creation', () => {
class App1 extends Component {
method1 = () => 41 + this.var1

var1 = 1

render() {
return <div>{this.method1()}</div>
}
}

class App2 extends Component {
method2 = () => 22 + this.var2

var2 = 2

/* eslint-disable */
__reactstandin__regenerateByEval(key, code) {
this[key] = eval(code)
}
/* eslint-enable */

render() {
return (
<div>
{this.method1()} + {this.method2()}
</div>
)
}
}

const proxy = createProxy(App1)
const Proxy = proxy.get()

const wrapper = mount(<Controller Proxy={Proxy} />)

expect(wrapper.text()).toContain('42')

proxy.update(App2)
wrapper.instance().forceUpdate()

// first render before hot render
expect(wrapper.text()).toContain('42')
wrapper.instance().forceUpdate()
// both methods expected to be present
expect(wrapper.text()).toContain('42 + 24')
})
})

0 comments on commit 111d56e

Please sign in to comment.