From 7819c71d384e37dc01a8ea9a75d83c4f7761215c Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Wed, 25 Apr 2018 10:51:44 +1000 Subject: [PATCH] fix: always update bound functions. #949 --- src/proxy/inject.js | 12 +++++----- test/proxy/consistency.test.js | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/proxy/inject.js b/src/proxy/inject.js index 8d5d316da..599d7e2a8 100644 --- a/src/proxy/inject.js +++ b/src/proxy/inject.js @@ -81,15 +81,15 @@ function mergeComponents( const nextString = String(nextAttr) const injectedBefore = injectedMembers[key] + const isFunction = + nextString.indexOf('function') >= 0 || nextString.indexOf('=>') >= 0 if ( nextString !== String(prevAttr) || - (injectedBefore && nextString !== String(injectedBefore)) + (injectedBefore && nextString !== String(injectedBefore)) || + isFunction ) { if (!hasRegenerate) { - if ( - nextString.indexOf('function') < 0 && - nextString.indexOf('=>') < 0 - ) { + if (!isFunction) { // just copy prop over injectedCode[key] = nextAttr } else { @@ -106,6 +106,8 @@ function mergeComponents( } else { injectedCode[key] = nextAttr } + } else { + // key was skipped } } }) diff --git a/test/proxy/consistency.test.js b/test/proxy/consistency.test.js index 007263425..a30cdb863 100644 --- a/test/proxy/consistency.test.js +++ b/test/proxy/consistency.test.js @@ -264,6 +264,46 @@ describe('consistency', () => { expect(instance.render()).toBe(42) }) + it('should reflect external dependencies', () => { + /* eslint-disable */ + const externalValue = 42 + class BaseClass extends React.Component { + arrow = () => externalValue + + render() { + return this.arrow() + } + + __reactstandin__regenerateByEval(key, code) { + this[key] = eval(code) + } + } + + const proxy = createProxy(BaseClass) + const Proxy = proxy.get() + const instance = new Proxy() + expect(instance.render()).toBe(42) + + { + const externalValue = 24 + class Update1Class extends React.Component { + arrow = () => externalValue + + render() { + return this.arrow() + } + + __reactstandin__regenerateByEval(key, code) { + this[key] = eval(code) + } + } + proxy.update(Update1Class) + } + /* eslint-enable */ + + expect(instance.render()).toBe(24) + }) + it('should stand-for all class members', () => { class Initial { constructor() {