From 39d4f5bd1bc0fce4c46d45e63783f9b87bed41f0 Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 1 Feb 2018 21:27:03 +1100 Subject: [PATCH] fix: regenerate overriden members (#837) Fixes #836 --- .../react-stand-in/src/createClassProxy.js | 2 +- packages/react-stand-in/src/inject.js | 7 ++- .../react-stand-in/test/consistency.test.js | 51 +++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/packages/react-stand-in/src/createClassProxy.js b/packages/react-stand-in/src/createClassProxy.js index 7dd2d0fa7..14f4945b4 100644 --- a/packages/react-stand-in/src/createClassProxy.js +++ b/packages/react-stand-in/src/createClassProxy.js @@ -188,7 +188,6 @@ function createClassProxy(InitialComponent, proxyKey, wrapResult = identity) { isFunctionalComponent = !isReactClass(NextComponent) proxyGeneration++ - injectedMembers = {} // Save the next constructor so we call it const PreviousComponent = CurrentComponent @@ -222,6 +221,7 @@ function createClassProxy(InitialComponent, proxyKey, wrapResult = identity) { NextComponent, InitialComponent, lastInstance, + injectedMembers, ) } } diff --git a/packages/react-stand-in/src/inject.js b/packages/react-stand-in/src/inject.js index 60bc4ab18..488449047 100644 --- a/packages/react-stand-in/src/inject.js +++ b/packages/react-stand-in/src/inject.js @@ -14,6 +14,7 @@ function mergeComponents( NextComponent, InitialComponent, lastInstance, + injectedMembers, ) { const injectedCode = {} try { @@ -79,7 +80,11 @@ function mergeComponents( } const nextString = String(nextAttr) - if (nextString !== String(prevAttr)) { + const injectedBefore = injectedMembers[key] + if ( + nextString !== String(prevAttr) || + (injectedBefore && nextString !== String(injectedBefore)) + ) { if (!hasRegenerate) { if ( nextString.indexOf('function') < 0 && diff --git a/packages/react-stand-in/test/consistency.test.js b/packages/react-stand-in/test/consistency.test.js index 7fa0ad0d6..49bd1f4f6 100644 --- a/packages/react-stand-in/test/consistency.test.js +++ b/packages/react-stand-in/test/consistency.test.js @@ -214,6 +214,57 @@ describe('consistency', () => { expect(Proxy.prototype instanceof Bar).toBe(true) }) + + it('should revert arrow member change', () => { + /* eslint-disable */ + class BaseClass extends React.Component { + arrow = () => 42 + + render() { + return this.arrow() + } + + __reactstandin__regenerateByEval(key, code) { + this[key] = eval(code) + } + } + + class Update1Class extends React.Component { + arrow = () => 43 + + render() { + return this.arrow() + } + + __reactstandin__regenerateByEval(key, code) { + this[key] = eval(code) + } + } + + class Update2Class extends React.Component { + arrow = () => 42 + + render() { + return this.arrow() + } + + __reactstandin__regenerateByEval(key, code) { + this[key] = eval(code) + } + } + /* eslint-enable */ + + const proxy = createProxy(BaseClass) + const Proxy = proxy.get() + const instance = new Proxy() + expect(instance.render()).toBe(42) + + proxy.update(Update1Class) + expect(instance.render()).toBe(43) + + proxy.update(Update2Class) + expect(instance.render()).toBe(42) + }) }) })