Skip to content

Commit

Permalink
[react-interactions] Add getInstanceFromNode support to TestHostRende…
Browse files Browse the repository at this point in the history
…rer (#17065)

Fix bad WeakMap key case

Fix bad WeakMap key case
  • Loading branch information
trueadm authored Oct 11, 2019
1 parent 22b2642 commit 0ac8e56
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,59 @@ describe('ReactScope', () => {
expect(node).toEqual(aRef.current);
});

it('containsNode() works as intended', () => {
const TestScope = React.unstable_createScope((type, props) => true);
const scopeRef = React.createRef();
const divRef = React.createRef();
const spanRef = React.createRef();
const aRef = React.createRef();
const outerSpan = React.createRef();
const emRef = React.createRef();

function Test({toggle}) {
return toggle ? (
<div>
<span ref={outerSpan}>SPAN</span>
<TestScope ref={scopeRef}>
<div ref={divRef}>DIV</div>
<span ref={spanRef}>SPAN</span>
<a ref={aRef}>A</a>
</TestScope>
<em ref={emRef}>EM</em>
</div>
) : (
<div>
<TestScope ref={scopeRef}>
<a ref={aRef}>A</a>
<div ref={divRef}>DIV</div>
<span ref={spanRef}>SPAN</span>
<em ref={emRef}>EM</em>
</TestScope>
<span ref={outerSpan}>SPAN</span>
</div>
);
}

const renderer = ReactTestRenderer.create(<Test toggle={true} />, {
createNodeMock: element => {
return element;
},
});
expect(scopeRef.current.containsNode(divRef.current)).toBe(true);
expect(scopeRef.current.containsNode(spanRef.current)).toBe(true);
expect(scopeRef.current.containsNode(aRef.current)).toBe(true);
expect(scopeRef.current.containsNode(outerSpan.current)).toBe(false);
expect(scopeRef.current.containsNode(emRef.current)).toBe(false);
renderer.update(<Test toggle={false} />);
expect(scopeRef.current.containsNode(divRef.current)).toBe(true);
expect(scopeRef.current.containsNode(spanRef.current)).toBe(true);
expect(scopeRef.current.containsNode(aRef.current)).toBe(true);
expect(scopeRef.current.containsNode(outerSpan.current)).toBe(false);
expect(scopeRef.current.containsNode(emRef.current)).toBe(true);
renderer.update(<Test toggle={true} />);
expect(scopeRef.current.containsNode(emRef.current)).toBe(false);
});

it('mixed getParent() and getAllNodes() works as intended', () => {
const TestScope = React.unstable_createScope((type, props) => true);
const TestScope2 = React.unstable_createScope((type, props) => true);
Expand Down
16 changes: 13 additions & 3 deletions packages/react-test-renderer/src/ReactTestHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export * from 'shared/HostConfigWithNoHydration';
const EVENT_COMPONENT_CONTEXT = {};
const NO_CONTEXT = {};
const UPDATE_SIGNAL = {};
const nodeToInstanceMap = new WeakMap();

if (__DEV__) {
Object.freeze(NO_CONTEXT);
Object.freeze(UPDATE_SIGNAL);
Expand All @@ -62,10 +64,14 @@ export function getPublicInstance(inst: Instance | TextInstance): * {
switch (inst.tag) {
case 'INSTANCE':
const createNodeMock = inst.rootContainerInstance.createNodeMock;
return createNodeMock({
const mockNode = createNodeMock({
type: inst.type,
props: inst.props,
});
if (typeof mockNode === 'object' && mockNode !== null) {
nodeToInstanceMap.set(mockNode, inst);
}
return mockNode;
default:
return inst;
}
Expand Down Expand Up @@ -354,6 +360,10 @@ export function unmountFundamentalComponent(
}
}

export function getInstanceFromNode(node: Object) {
throw new Error('Not yet implemented.');
export function getInstanceFromNode(mockNode: Object) {
const instance = nodeToInstanceMap.get(mockNode);
if (instance !== undefined) {
return instance.internalInstanceHandle;
}
return null;
}

0 comments on commit 0ac8e56

Please sign in to comment.