mirror of
https://github.com/bendtherules/react-fiber-traverse.git
synced 2026-08-18 13:52:36 +00:00
Add basic traverse function with tests
This commit is contained in:
@@ -0,0 +1,63 @@
|
|||||||
|
// import { isNodeSimple } from './utils';
|
||||||
|
|
||||||
|
export interface FiberNode {
|
||||||
|
child: FiberNode | null;
|
||||||
|
sibling: FiberNode | null;
|
||||||
|
// Add type
|
||||||
|
}
|
||||||
|
|
||||||
|
function traverse(node: FiberNode, fn: (node: FiberNode) => any) {
|
||||||
|
// if (node.child) {
|
||||||
|
// if (!isNodeSimple(node) && typeof node.child.type === "string") {
|
||||||
|
// node.child.stateNode.addEventListener('click', (ev) => {
|
||||||
|
// console.log('Clicked inside component: ' + node.type.name);
|
||||||
|
// ev.stopPropagation();
|
||||||
|
// });
|
||||||
|
// /* console.log('Set event listener for '+ node.type.name) */;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
fn.call(null, node);
|
||||||
|
console.log('Going through node with ', node);
|
||||||
|
|
||||||
|
if (node.child !== null) {
|
||||||
|
traverse(node.child, fn);
|
||||||
|
}
|
||||||
|
if (node.sibling !== null) {
|
||||||
|
traverse(node.sibling, fn);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// findComponent = (node, name) => {
|
||||||
|
// console.log(node)
|
||||||
|
// if (!isNodeSimple(node) && node.type.name === name) {
|
||||||
|
// console.log('Found node with name ' + node.type.name);
|
||||||
|
|
||||||
|
// /* if (node.child) {
|
||||||
|
// let innerNode = node.child;
|
||||||
|
// console.log('Highlighting its inner nodes in yellow');
|
||||||
|
|
||||||
|
// highlightNode(node, true);
|
||||||
|
|
||||||
|
// } */
|
||||||
|
// return node;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// let returnVal = undefined;
|
||||||
|
// returnVal = node.child && findComponent(node.child, name);
|
||||||
|
// if (returnVal) { return returnVal }
|
||||||
|
// returnVal = node.sibling && findComponent(node.sibling, name);
|
||||||
|
// if (returnVal) { return returnVal }
|
||||||
|
// };
|
||||||
|
|
||||||
|
// highlightNode = (node, ignoreSibling = false) => {
|
||||||
|
// if (isNodeSimple(node)) {
|
||||||
|
// node.stateNode.style.backgroundColor = "yellow";
|
||||||
|
// } else {
|
||||||
|
// node.child && highlightNode(node.child);
|
||||||
|
// }
|
||||||
|
// !ignoreSibling && node.sibling && highlightNode(node.sibling);
|
||||||
|
// }
|
||||||
|
|
||||||
|
export { traverse };
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import * as PropTypes from 'prop-types'
|
|
||||||
import * as React from 'react'
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
text: string
|
|
||||||
}
|
|
||||||
|
|
||||||
const C: React.SFC<Props> = ({ text }) => <p>{text}</p>
|
|
||||||
|
|
||||||
C.propTypes = {
|
|
||||||
text: PropTypes.string.isRequired
|
|
||||||
}
|
|
||||||
|
|
||||||
export default C
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
function isNodeSimple(node) {
|
||||||
|
return (typeof node.type === "string") || node.type === null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export { isNodeSimple };
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
// import * as PropTypes from "prop-types";
|
||||||
|
import * as React from "react";
|
||||||
|
|
||||||
|
class CDepth2 extends React.Component {
|
||||||
|
render() {
|
||||||
|
return <div></div>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// C.propTypes = {
|
||||||
|
// text: PropTypes.string.isRequired
|
||||||
|
// };
|
||||||
|
|
||||||
|
export default CDepth2;
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import * as React from "react";
|
||||||
|
// import ReactDOM from "react-dom";
|
||||||
|
import { mount } from "enzyme";
|
||||||
|
import CDepth2 from "./sample-components/depth-2-simple";
|
||||||
|
import { traverse, FiberNode } from "../src";
|
||||||
|
|
||||||
|
describe("Basic traverse test", () => {
|
||||||
|
let container: HTMLDivElement;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
container = document.body.appendChild(document.createElement("div"));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
document.body.removeChild(container);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should render correctly", () => {
|
||||||
|
const rootRef = React.createRef<CDepth2>();
|
||||||
|
|
||||||
|
// ReactDOM.render(
|
||||||
|
// <C text="text" ref={rootRef} />,
|
||||||
|
// document.getElementById("container")
|
||||||
|
// );
|
||||||
|
mount(<CDepth2 ref={rootRef} />, { attachTo: container });
|
||||||
|
|
||||||
|
const mockCallback = jest.fn();
|
||||||
|
|
||||||
|
if (rootRef.current !== null) {
|
||||||
|
traverse(
|
||||||
|
(rootRef.current as any)._reactInternalFiber.child as FiberNode,
|
||||||
|
mockCallback
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The mock function is called twice
|
||||||
|
expect(mockCallback.mock.calls.length).toBe(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user