Update test renderer to support new types of work (#12237)

Adds support for ContextProvider, ContextConsumer, and Mode.
This commit is contained in:
Andrew Clark
2018-02-16 11:27:20 -08:00
committed by GitHub
parent 54aa6c5c66
commit 878261732f
2 changed files with 83 additions and 0 deletions
+9
View File
@@ -22,6 +22,9 @@ import {
HostPortal,
HostText,
HostRoot,
ContextConsumer,
ContextProvider,
Mode,
} from 'shared/ReactTypeOfWork';
import invariant from 'fbjs/lib/invariant';
@@ -366,6 +369,9 @@ function toTree(node: ?Fiber) {
case HostText:
return node.stateNode.text;
case Fragment:
case ContextProvider:
case ContextConsumer:
case Mode:
return childrenToTree(node.child);
default:
invariant(
@@ -463,6 +469,9 @@ class ReactTestInstance {
children.push('' + node.memoizedProps);
break;
case Fragment:
case ContextProvider:
case ContextConsumer:
case Mode:
descend = true;
break;
default:
@@ -879,4 +879,78 @@ describe('ReactTestRenderer', () => {
'world',
]);
});
it('supports context providers and consumers', () => {
const {Consumer, Provider} = React.createContext('a');
function Child(props) {
return props.value;
}
function App() {
return (
<Provider value="b">
<Consumer>{value => <Child value={value} />}</Consumer>
</Provider>
);
}
const renderer = ReactTestRenderer.create(<App />);
const child = renderer.root.findByType(Child);
expect(child.children).toEqual(['b']);
expect(prettyFormat(renderer.toTree())).toEqual(
prettyFormat({
instance: null,
nodeType: 'component',
props: {},
rendered: {
instance: null,
nodeType: 'component',
props: {
value: 'b',
},
rendered: 'b',
type: Child,
},
type: App,
}),
);
});
it('supports modes', () => {
function Child(props) {
return props.value;
}
function App(props) {
return (
<React.StrictMode>
<Child value={props.value} />
</React.StrictMode>
);
}
const renderer = ReactTestRenderer.create(<App value="a" />);
const child = renderer.root.findByType(Child);
expect(child.children).toEqual(['a']);
expect(prettyFormat(renderer.toTree())).toEqual(
prettyFormat({
instance: null,
nodeType: 'component',
props: {
value: 'a',
},
rendered: {
instance: null,
nodeType: 'component',
props: {
value: 'a',
},
rendered: 'a',
type: Child,
},
type: App,
}),
);
});
});