mirror of
https://github.com/bendtherules/react-fiber-traverse.git
synced 2026-08-18 22:01:49 +00:00
Fix usages on mountAndGetRootNode with function component
This commit is contained in:
@@ -10,7 +10,6 @@ import { traverseGenerator } from "../src";
|
|||||||
|
|
||||||
// Import test helpers and sample components
|
// Import test helpers and sample components
|
||||||
import { mountAndGetRootNode } from "./utils/mountInEnzyme";
|
import { mountAndGetRootNode } from "./utils/mountInEnzyme";
|
||||||
import getWrappedComponent from './utils/getWrappedComponent';
|
|
||||||
import CDepth1 from "./sample-components/depth-1-simple";
|
import CDepth1 from "./sample-components/depth-1-simple";
|
||||||
import CDepth2 from "./sample-components/depth-2-simple";
|
import CDepth2 from "./sample-components/depth-2-simple";
|
||||||
import CDepth5 from "./sample-components/depth-5-simple";
|
import CDepth5 from "./sample-components/depth-5-simple";
|
||||||
@@ -63,9 +62,7 @@ describe("traverseGenerator", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should work for depth=1 function", () => {
|
it("should work for depth=1 function", () => {
|
||||||
const WrappedC = getWrappedComponent(FnDepth1);
|
const rootNode = mountAndGetRootNode(FnDepth1, container);
|
||||||
const rootNode = mountAndGetRootNode(WrappedC, container)
|
|
||||||
.child as FiberNodeForFunctionComponent;
|
|
||||||
|
|
||||||
const nodeIterator = traverseGenerator(rootNode);
|
const nodeIterator = traverseGenerator(rootNode);
|
||||||
const nodes = [...nodeIterator];
|
const nodes = [...nodeIterator];
|
||||||
@@ -86,9 +83,7 @@ describe("traverseGenerator", () => {
|
|||||||
return <div>Fn2 here</div>;
|
return <div>Fn2 here</div>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const WrappedC = getWrappedComponent(Fn1);
|
const rootNode = mountAndGetRootNode(Fn1, container);
|
||||||
const rootNode = mountAndGetRootNode(WrappedC, container)
|
|
||||||
.child as FiberNodeForFunctionComponent;
|
|
||||||
|
|
||||||
const nodeIterator = traverseGenerator(rootNode);
|
const nodeIterator = traverseGenerator(rootNode);
|
||||||
const nodes = [...nodeIterator];
|
const nodes = [...nodeIterator];
|
||||||
@@ -113,9 +108,7 @@ describe("traverseGenerator", () => {
|
|||||||
return <div>Fn2 here</div>;
|
return <div>Fn2 here</div>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const WrappedC = getWrappedComponent(Fn1);
|
const rootNode = mountAndGetRootNode(Fn1, container);
|
||||||
const rootNode = mountAndGetRootNode(WrappedC, container)
|
|
||||||
.child as FiberNodeForFunctionComponent;
|
|
||||||
|
|
||||||
const nodeIterator = traverseGenerator(rootNode);
|
const nodeIterator = traverseGenerator(rootNode);
|
||||||
const nodes = [...nodeIterator];
|
const nodes = [...nodeIterator];
|
||||||
|
|||||||
@@ -1,21 +1,19 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
|
|
||||||
// Import stuff from src
|
// Import stuff from src
|
||||||
// import {
|
import {
|
||||||
// FiberNodeForComponentClass,
|
FiberNodeForFunctionComponent,
|
||||||
// FiberNodeForInstrinsicElement,
|
FiberNodeForComponentClass
|
||||||
// FiberNodeForFunctionComponent
|
} from "../src/mocked-types";
|
||||||
// } from "../src/mocked-types";
|
|
||||||
import { traverseGenerator } from "../src";
|
import { traverseGenerator } from "../src";
|
||||||
|
|
||||||
// Import test helpers and sample components
|
// Import test helpers and sample components
|
||||||
import { mountAndGetRootNode } from "./utils/mountInEnzyme";
|
import { mountAndGetRootNode } from "./utils/mountInEnzyme";
|
||||||
// import getWrappedComponent from './utils/getWrappedComponent';
|
import {
|
||||||
|
createClassComponents,
|
||||||
|
createFunctionComponents
|
||||||
|
} from "./utils/createComponent";
|
||||||
import CDepth1 from "./sample-components/depth-1-simple";
|
import CDepth1 from "./sample-components/depth-1-simple";
|
||||||
import { FiberNodeForFunctionComponent } from "../src/mocked-types";
|
|
||||||
// import CDepth2 from "./sample-components/depth-2-simple";
|
|
||||||
// import CDepth5 from "./sample-components/depth-5-simple";
|
|
||||||
// import FnDepth1 from './sample-components/depth-1-fn-simple';
|
|
||||||
|
|
||||||
describe("traverseGenerator", () => {
|
describe("traverseGenerator", () => {
|
||||||
let container: HTMLDivElement;
|
let container: HTMLDivElement;
|
||||||
@@ -155,7 +153,8 @@ describe("traverseGenerator", () => {
|
|||||||
expect(nodes.length).not.toBe(1);
|
expect(nodes.length).not.toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should work for depth=3 depth-first", () => {
|
it("should work for depth-first - class component", () => {
|
||||||
|
const [C2, C3, C4, C5] = createClassComponents(["C2", "C3", "C4", "C5"]);
|
||||||
class C1 extends React.Component {
|
class C1 extends React.Component {
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
@@ -170,18 +169,40 @@ describe("traverseGenerator", () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function C2(props: { children?: any }) {
|
|
||||||
return props.children || null;
|
const rootNode = mountAndGetRootNode(C1, container);
|
||||||
}
|
|
||||||
function C3(props: { children?: any }) {
|
const nodeIterator = traverseGenerator(rootNode, {
|
||||||
return props.children || null;
|
order: ["self", "child", "sibling"]
|
||||||
}
|
});
|
||||||
function C4(props: { children?: any }) {
|
const nodes = [...nodeIterator];
|
||||||
return props.children || null;
|
|
||||||
}
|
expect(nodes.length).toBe(5);
|
||||||
function C5(props: { children?: any }) {
|
// Check depth-based order (default order)
|
||||||
return props.children || null;
|
expect(
|
||||||
}
|
nodes.map(tmpNode => (tmpNode as FiberNodeForComponentClass).type)
|
||||||
|
).toEqual([C1, C2, C3, C4, C5]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should work for depth-first - function component", () => {
|
||||||
|
const [C2, C3, C4, C5] = createFunctionComponents([
|
||||||
|
"C2",
|
||||||
|
"C3",
|
||||||
|
"C4",
|
||||||
|
"C5"
|
||||||
|
]);
|
||||||
|
const C1 = function() {
|
||||||
|
return (
|
||||||
|
<React.Fragment>
|
||||||
|
<C2>
|
||||||
|
<C3 />
|
||||||
|
</C2>
|
||||||
|
<C4>
|
||||||
|
<C5 />
|
||||||
|
</C4>
|
||||||
|
</React.Fragment>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const rootNode = mountAndGetRootNode(C1, container);
|
const rootNode = mountAndGetRootNode(C1, container);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user