-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Use _hostContainerInfo to track test renderer options #8261
Changes from 1 commit
c00a660
70f274f
7b8366f
a4b488f
e755a3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,20 +53,23 @@ class ReactTestComponent { | |
_currentElement: ReactElement; | ||
_renderedChildren: null | Object; | ||
_topLevelWrapper: null | ReactInstance; | ||
_hostContainerInfo: null | Object; | ||
|
||
constructor(element: ReactElement) { | ||
this._currentElement = element; | ||
this._renderedChildren = null; | ||
this._topLevelWrapper = null; | ||
this._hostContainerInfo = null; | ||
} | ||
|
||
mountComponent( | ||
transaction: ReactTestReconcileTransaction, | ||
nativeParent: null | ReactTestComponent, | ||
nativeContainerInfo: ?null, | ||
hostContainerInfo: null | Object, | ||
context: Object, | ||
) { | ||
var element = this._currentElement; | ||
this._hostContainerInfo = hostContainerInfo; | ||
// $FlowFixMe https://github.com/facebook/flow/issues/1805 | ||
this.mountChildren(element.props.children, transaction, context); | ||
} | ||
|
@@ -83,8 +86,11 @@ class ReactTestComponent { | |
|
||
getPublicInstance(transaction: ReactTestReconcileTransaction): Object { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove transaction as an argument here now? As well as not pass it wherever we currently do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yupp, I'll go ahead and do that as well 👍 |
||
var element = this._currentElement; | ||
var options = transaction.getTestOptions(); | ||
return options.createNodeMock(element); | ||
var options = this._hostContainerInfo; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's call this |
||
if (options && options.createNodeMock) { | ||
return options.createNodeMock(element); | ||
} | ||
return {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this branch ever runs. We always use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was because of While it never should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you throw if it is |
||
} | ||
|
||
toJSON(): ReactTestRendererJSON { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -306,6 +306,41 @@ describe('ReactTestRenderer', () => { | |
]); | ||
}); | ||
|
||
it('supports unmounting when using refs', () => { | ||
class Foo extends React.Component { | ||
render() { | ||
return <div ref="foo"/>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: space before /> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a linting rule for this? hard to remember if |
||
} | ||
} | ||
const inst = ReactTestRenderer.create( | ||
<Foo/>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
{ createNodeMock: () => 'foo' } | ||
); | ||
expect(() => inst.unmount()).not.toThrow(); | ||
}); | ||
|
||
it('supports updates when using refs', () => { | ||
const log = []; | ||
const createNodeMock = element => { | ||
log.push(element.type); | ||
return element.type; | ||
}; | ||
class Foo extends React.Component { | ||
render() { | ||
return this.props.useDiv | ||
? <div ref="foo" /> | ||
: <span ref="foo" />; | ||
} | ||
} | ||
const inst = ReactTestRenderer.create( | ||
<Foo useDiv={true} />, | ||
{ createNodeMock } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: no spaces in object literals, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A linting rule for this would also be great. |
||
); | ||
inst.update(<Foo useDiv={false} />); | ||
// It's called with 'div' twice (mounting and unmounting) | ||
expect(log).toEqual(['div', 'div', 'span']); | ||
}); | ||
|
||
it('supports error boundaries', () => { | ||
var log = []; | ||
class Angry extends React.Component { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't it always exist in test renderer? We probably shouldn't check for its existence below as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, it should always be populated here.