From 04a69af199e01a8c48d5451d6c384e67803ac304 Mon Sep 17 00:00:00 2001 From: bendtherules Date: Tue, 30 Jul 2019 11:41:07 +0530 Subject: [PATCH] Add testcase - findNodeByComponent - similar to component name cases --- test/findNodeByComponent-basic.spec.tsx | 125 ++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 test/findNodeByComponent-basic.spec.tsx diff --git a/test/findNodeByComponent-basic.spec.tsx b/test/findNodeByComponent-basic.spec.tsx new file mode 100644 index 0000000..c12d538 --- /dev/null +++ b/test/findNodeByComponent-basic.spec.tsx @@ -0,0 +1,125 @@ +import * as React from "react"; + +import { FiberNodeForComponentClass } from "../src/mocked-types"; + +// Import stuff from src +import { findNodeByComponent } from "../src"; +import { mountAndGetRootNode } from "./utils/mount-in-enzyme"; +import getWrappedComponent from "./utils/getWrappedComponent"; + +// Import test helpers and sample components +import CDepth1 from "./sample-components/depth-1-simple"; +import FnDepth1 from "./sample-components/depth-1-fn-simple"; + +describe("findNodeByComponent", () => { + let container: HTMLDivElement; + + beforeEach(() => { + container = document.body.appendChild(document.createElement("div")); + }); + + afterEach(() => { + document.body.removeChild(container); + }); + + describe("basic", () => { + it("should work for top-level class", () => { + const WrappedC = getWrappedComponent(CDepth1); + const rootNode = mountAndGetRootNode(WrappedC, container); + + const found = findNodeByComponent(rootNode, CDepth1); + + expect(found).not.toBeFalsy(); + expect((found as FiberNodeForComponentClass).stateNode).toBeInstanceOf( + CDepth1 + ); + }); + + it("should work for 2nd-level class", () => { + 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); + + const found = findNodeByComponent(rootNode, C2); + + // 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 top-level function", () => { + const WrappedC = getWrappedComponent(FnDepth1); + const rootNode = mountAndGetRootNode(WrappedC, container); + + const found = findNodeByComponent(rootNode, FnDepth1); + + expect(found).not.toBeFalsy(); + expect((found as FiberNodeForComponentClass).type).toBe(FnDepth1); + expect((found as FiberNodeForComponentClass).stateNode).toBe(null); + }); + + it("should work for 2nd-level function", () => { + function Fn1() { + return ; + } + function Fn2() { + return
Fn2 here
; + } + + const WrappedC = getWrappedComponent(Fn1); + const rootNode = mountAndGetRootNode(WrappedC, container); + + const found = findNodeByComponent(rootNode, Fn2); + + // Shouldn't be null, or other falsy value + expect(found).not.toBeFalsy(); + expect((found as FiberNodeForComponentClass).stateNode).toBe(null); + expect((found as FiberNodeForComponentClass).type.name).toBe(Fn2.name); + }); + + it("should work for 5th-level function", () => { + function Fn1() { + return ( +
+
+ + +
+
+ ); + } + + function Fn2() { + return Fn2 here; + } + + const WrappedC = getWrappedComponent(Fn1); + const rootNode = mountAndGetRootNode(WrappedC, container); + + const found = findNodeByComponent(rootNode, Fn2); + + // Shouldn't be null, or other falsy value + expect(found).not.toBeFalsy(); + expect((found as FiberNodeForComponentClass).stateNode).toBe(null); + expect((found as FiberNodeForComponentClass).type.name).toBe(Fn2.name); + }); + }); +});