Skip to content

Commit

Permalink
Fix findByType broken for SimpleMemoComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Dec 24, 2019
1 parent f887d1a commit 151c2c2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ describe('ReactHooksInspectionIntegration', () => {
}
let Foo = React.memo(InnerFoo);
let renderer = ReactTestRenderer.create(<Foo />);
// TODO: Test renderer findByType is broken for memo. Have to search for the inner.
let childFiber = renderer.root.findByType(InnerFoo)._currentFiber();
let childFiber = renderer.root.findByType(Foo)._currentFiber();
let tree = ReactDebugTools.inspectHooksOfFiber(childFiber);
expect(tree).toEqual([
{
Expand Down
4 changes: 4 additions & 0 deletions packages/react-test-renderer/src/ReactTestRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ class ReactTestInstance {
}

get type() {
if (this._fiber.tag === SimpleMemoComponent) {
return this._fiber.elementType;
}

return this._fiber.type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1022,4 +1022,30 @@ describe('ReactTestRenderer', () => {
expect(Scheduler).toFlushWithoutYielding();
ReactTestRenderer.create(<App />);
});

it('supports SimpleMemoComponent with findByType', () => {
const App = () => null;
const SimpleMemoApp = React.memo(App);

const renderer = ReactTestRenderer.create(<SimpleMemoApp />);

const child = renderer.root.findByType(SimpleMemoApp);

expect(child.type).toBe(SimpleMemoApp);
});

it('supports MemoComponent with findByType', () => {
class App extends React.Component {
render() {
return null;
}
}
const MemoApp = React.memo(App);

const renderer = ReactTestRenderer.create(<MemoApp />);

const child = renderer.root.findByType(MemoApp);

expect(child.type).toBe(MemoApp);
});
});

0 comments on commit 151c2c2

Please sign in to comment.