mirror of
https://github.com/bendtherules/react-fiber-traverse.git
synced 2026-08-18 13:52:36 +00:00
Add more testcases for findNodeByName with class and fn components
This commit is contained in:
@@ -7,6 +7,7 @@ import { mountAndGetRootNode } from "./utils/mount-in-enzyme";
|
|||||||
import CDepth1 from "./sample-components/depth-1-simple";
|
import CDepth1 from "./sample-components/depth-1-simple";
|
||||||
import { FiberNodeForComponentClass } from "../src/mocked-types";
|
import { FiberNodeForComponentClass } from "../src/mocked-types";
|
||||||
import getWrappedComponent from "./utils/getWrappedComponent";
|
import getWrappedComponent from "./utils/getWrappedComponent";
|
||||||
|
import FnDepth1 from "./sample-components/depth-1-fn-simple";
|
||||||
|
|
||||||
describe("findNodeByComponentName", () => {
|
describe("findNodeByComponentName", () => {
|
||||||
let container: HTMLDivElement;
|
let container: HTMLDivElement;
|
||||||
@@ -20,7 +21,7 @@ describe("findNodeByComponentName", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("basic", () => {
|
describe("basic", () => {
|
||||||
it("should work for top-level name", () => {
|
it("should work for top-level class name", () => {
|
||||||
const WrappedC = getWrappedComponent(CDepth1);
|
const WrappedC = getWrappedComponent(CDepth1);
|
||||||
const rootNode = mountAndGetRootNode(WrappedC, container);
|
const rootNode = mountAndGetRootNode(WrappedC, container);
|
||||||
|
|
||||||
@@ -31,5 +32,65 @@ describe("findNodeByComponentName", () => {
|
|||||||
CDepth1
|
CDepth1
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should work for 2nd-level class name", () => {
|
||||||
|
class C1 extends React.Component {
|
||||||
|
render() {
|
||||||
|
return <C2 />;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class C2 extends React.Component {
|
||||||
|
render() {
|
||||||
|
return <div>C2 here</div>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const WrappedC = getWrappedComponent(C1);
|
||||||
|
const rootNode = mountAndGetRootNode(WrappedC, container);
|
||||||
|
|
||||||
|
const found = findNodeByComponentName(rootNode, C2.name);
|
||||||
|
|
||||||
|
// 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 name", () => {
|
||||||
|
const WrappedC = getWrappedComponent(FnDepth1);
|
||||||
|
const rootNode = mountAndGetRootNode(WrappedC, container);
|
||||||
|
|
||||||
|
const found = findNodeByComponentName(rootNode, FnDepth1.name);
|
||||||
|
|
||||||
|
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 name", () => {
|
||||||
|
function Fn1() {
|
||||||
|
return <Fn2 />;
|
||||||
|
}
|
||||||
|
function Fn2() {
|
||||||
|
return <div>Fn2 here</div>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const WrappedC = getWrappedComponent(Fn1);
|
||||||
|
const rootNode = mountAndGetRootNode(WrappedC, container);
|
||||||
|
|
||||||
|
const found = findNodeByComponentName(rootNode, Fn2.name);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
function FnDepth1() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default FnDepth1;
|
||||||
Reference in New Issue
Block a user