Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double proxy registration. Fix regression #912 #915

Merged
merged 1 commit into from
Apr 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/proxy/createClassProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import { inject, checkLifeCycleMethods, mergeComponents } from './inject'

const has = Object.prototype.hasOwnProperty

const proxies = new WeakMap()
let proxies = new WeakMap()

export const resetClassProxies = () => {
proxies = new WeakMap()
}

const blackListedClassMembers = [
'constructor',
Expand Down Expand Up @@ -174,6 +178,7 @@ function createClassProxy(InitialComponent, proxyKey, options) {

let ProxyFacade
let ProxyComponent = null
let proxy

if (!isFunctionalComponent) {
ProxyComponent = proxyClassCreator(InitialComponent, postConstructionAction)
Expand Down Expand Up @@ -255,10 +260,11 @@ function createClassProxy(InitialComponent, proxyKey, options) {
// Prevent proxy cycles
const existingProxy = proxies.get(NextComponent)
if (existingProxy) {
update(existingProxy[UNWRAP_PROXY]())
return
}

proxies.set(NextComponent, proxy)

isFunctionalComponent = !isReactClass(NextComponent)
proxyGeneration++

Expand Down Expand Up @@ -309,7 +315,9 @@ function createClassProxy(InitialComponent, proxyKey, options) {

update(InitialComponent)

const proxy = { get, update }
proxy = { get, update }

proxies.set(InitialComponent, proxy)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a fix. Everything else is related to this change - now tests have "reset" proxies sometimes.

proxies.set(ProxyFacade, proxy)

safeDefineProperty(proxy, UNWRAP_PROXY, {
Expand Down
2 changes: 2 additions & 0 deletions src/reconciler/proxies.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import createProxy from '../proxy'
import { resetClassProxies } from '../proxy/createClassProxy'

let proxiesByID
let idsByType
Expand Down Expand Up @@ -35,6 +36,7 @@ export const createProxyForType = type =>
export const resetProxies = () => {
proxiesByID = {}
idsByType = new WeakMap()
resetClassProxies()
}

resetProxies()
49 changes: 49 additions & 0 deletions test/AppContainer.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,55 @@ describe(`AppContainer (dev)`, () => {
expect(wrapper.text()).toBe('TESTnew childnew child')
})

it('renders children with chunked re-register', () => {
const spy = jest.fn()

class App extends Component {
componentWillUnmount() {
spy()
}

render() {
return <div>I AM CHILD</div>
}
}

RHL.reset()
RHL.register(App, 'App1', 'test.js')
RHL.register(App, 'App2', 'test.js')

const wrapper = mount(
<AppContainer>
<App />
</AppContainer>,
)
expect(spy).not.toHaveBeenCalled()
wrapper.setProps({ children: <App /> })
expect(spy).not.toHaveBeenCalled()
RHL.register(App, 'App3', 'test.js')
wrapper.setProps({ children: <App /> })
expect(spy).not.toHaveBeenCalled()
Copy link
Collaborator Author

@theKashey theKashey Mar 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will stop here. It is still the App, but that App will use ProxyComponent created for App3


{
class App extends Component {
componentWillUnmount() {
spy()
}

render() {
return <div>I AM NEW CHILD</div>
}
}
RHL.register(App, 'App3', 'test.js')
wrapper.setProps({ children: <App /> })
expect(spy).not.toHaveBeenCalled()

RHL.register(App, 'App4', 'test.js')
wrapper.setProps({ children: <App /> })
expect(spy).not.toHaveBeenCalled()
}
})

describe('with HOC-wrapped root', () => {
it('renders children', () => {
const spy = jest.fn()
Expand Down
6 changes: 6 additions & 0 deletions test/proxy/consistency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ describe('consistency', () => {
expect(proxy.get()).toBe(Proxy)
})

it('prevents double proxy creation', () => {
const proxy1 = createProxy(Bar)
const proxy2 = createProxy(Bar)
expect(proxy1.get()).toBe(proxy2.get())
})

it('prevents mutually recursive proxy cycle', () => {
const barProxy = createProxy(Bar)
const BarProxy = barProxy.get()
Expand Down
8 changes: 4 additions & 4 deletions test/proxy/instance-property.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react'
import { ensureNoWarnings, createMounter } from './helper'
import createProxy from '../../src/proxy'

const fixtures = {
const fixtures = () => ({
modern: {
InstanceProperty: class InstanceProperty extends React.Component {
answer = 42
Expand Down Expand Up @@ -79,7 +79,7 @@ const fixtures = {
}
},
},
}
})

describe('instance property', () => {
ensureNoWarnings()
Expand All @@ -91,7 +91,7 @@ describe('instance property', () => {
InstanceProperty,
InstancePropertyUpdate,
InstancePropertyRemoval,
} = fixtures[type]
} = fixtures()[type]

it('is available on proxy class instance', () => {
const proxy = createProxy(InstanceProperty)
Expand Down Expand Up @@ -158,7 +158,7 @@ describe('instance property', () => {
// })

it('show use the underlayer top value', () => {
const proxy = createProxy(fixtures.modern.InstancePropertyFromContext)
const proxy = createProxy(fixtures().modern.InstancePropertyFromContext)
const Proxy = proxy.get()
const wrapper = mount(<Proxy />)
expect(wrapper.text()).toBe('42')
Expand Down
11 changes: 7 additions & 4 deletions test/proxy/static-method.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import React from 'react'
import { ensureNoWarnings, createMounter } from './helper'
import createProxy from '../../src/proxy'
import reactHotLoader from '../../src/reactHotLoader'

const fixtures = {
const fixtures = () => ({
modern: {
StaticMethod: class StaticMethod extends React.Component {
static getAnswer() {
Expand Down Expand Up @@ -31,19 +32,21 @@ const fixtures = {
}
},
},
}
})

describe('static method', () => {
ensureNoWarnings()
const { mount } = createMounter()

Object.keys(fixtures).forEach(type => {
Object.keys(fixtures()).forEach(type => {
describe(type, () => {
const {
StaticMethod,
StaticMethodUpdate,
StaticMethodRemoval,
} = fixtures[type]
} = fixtures()[type]

beforeEach(() => reactHotLoader.reset())

it('is available on proxy class instance', () => {
const proxy = createProxy(StaticMethod)
Expand Down
11 changes: 7 additions & 4 deletions test/proxy/static-property.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import React from 'react'
import PropTypes from 'prop-types'
import { ensureNoWarnings, createMounter } from './helper'
import createProxy from '../../src/proxy'
import reactHotLoader from '../../src/reactHotLoader'

const fixtures = {
const fixtures = () => ({
modern: {
StaticProperty: class StaticProperty extends React.Component {
static answer = 42
Expand Down Expand Up @@ -65,21 +66,23 @@ const fixtures = {
}
},
},
}
})

describe('static property', () => {
ensureNoWarnings()
const { mount } = createMounter()

Object.keys(fixtures).forEach(type => {
Object.keys(fixtures()).forEach(type => {
describe(type, () => {
const {
StaticProperty,
StaticPropertyUpdate,
StaticPropertyRemoval,
WithPropTypes,
WithPropTypesUpdate,
} = fixtures[type]
} = fixtures()[type]

beforeEach(() => reactHotLoader.reset())

it('is available on proxy class instance', () => {
const proxy = createProxy(StaticProperty)
Expand Down