diff --git a/test/findNodeByComponentRef-basic.spec.tsx b/test/findNodeByComponentRef-basic.spec.tsx new file mode 100644 index 0000000..0f73056 --- /dev/null +++ b/test/findNodeByComponentRef-basic.spec.tsx @@ -0,0 +1,105 @@ +import * as React from "react"; + +import { FiberNodeForComponentClass } from "../src/mocked-types"; + +// Import stuff from src +import { findNodeByComponentRef } from "../src"; +import { mountAndGetRootNode } from "./utils/mount-in-enzyme"; +import getWrappedComponent from "./utils/getWrappedComponent"; + +// Import test helpers and sample components + +describe("findNodeByComponentRef", () => { + let container: HTMLDivElement; + + beforeEach(() => { + container = document.body.appendChild(document.createElement("div")); + }); + + afterEach(() => { + document.body.removeChild(container); + }); + + describe("basic", () => { + it("should work for 2nd-level class", () => { + const C2Ref = React.createRef(); + + class C1 extends React.Component { + render() { + return ; + } + } + class C2 extends React.Component { + render() { + return
C2 here
; + } + } + const WrappedC = getWrappedComponent(C1); + const rootNode = mountAndGetRootNode(WrappedC, container); + + if (C2Ref.current === null) { + throw new Error("Ref is not yet set"); + } + + const found = findNodeByComponentRef(rootNode, C2Ref.current); + + // Shouldn't be null, or other falsy value + expect(found).not.toBeFalsy(); + expect((found as FiberNodeForComponentClass).stateNode).toBeInstanceOf( + C2 + ); + expect((found as FiberNodeForComponentClass).type.name).toBe(C2.name); + expect( + (found as FiberNodeForComponentClass).stateNode + ).not.toBeInstanceOf(C1); + expect( + (found as FiberNodeForComponentClass).stateNode + ).not.toBeInstanceOf(WrappedC); + }); + + it("should work for 5th-level class", () => { + const C2Ref = React.createRef(); + + class C1 extends React.Component { + render() { + return ( +
+
+ + +
+
+ ); + } + } + + class C2 extends React.Component { + render() { + return Fn2 here; + } + } + + const WrappedC = getWrappedComponent(C1); + const rootNode = mountAndGetRootNode(WrappedC, container); + + if (C2Ref.current === null) { + throw new Error("Ref is not yet set"); + } + + const found = findNodeByComponentRef(rootNode, C2Ref.current); + + // Shouldn't be null, or other falsy value + expect(found).not.toBeFalsy(); + expect((found as FiberNodeForComponentClass).stateNode).toBeInstanceOf( + C2 + ); + expect((found as FiberNodeForComponentClass).type.name).toBe(C2.name); + expect( + (found as FiberNodeForComponentClass).stateNode + ).not.toBeInstanceOf(C1); + expect( + (found as FiberNodeForComponentClass).stateNode + ).not.toBeInstanceOf(WrappedC); + }); + }); +});