Skip to content

Commit

Permalink
fix: regenerate overriden members (#837)
Browse files Browse the repository at this point in the history
Fixes #836
  • Loading branch information
theKashey authored and gregberge committed Feb 1, 2018
1 parent d72a9dd commit 39d4f5b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/react-stand-in/src/createClassProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -222,6 +221,7 @@ function createClassProxy(InitialComponent, proxyKey, wrapResult = identity) {
NextComponent,
InitialComponent,
lastInstance,
injectedMembers,
)
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/react-stand-in/src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function mergeComponents(
NextComponent,
InitialComponent,
lastInstance,
injectedMembers,
) {
const injectedCode = {}
try {
Expand Down Expand Up @@ -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 &&
Expand Down
51 changes: 51 additions & 0 deletions packages/react-stand-in/test/consistency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

Expand Down

0 comments on commit 39d4f5b

Please sign in to comment.