Add basic traverse function with tests

This commit is contained in:
2019-07-19 18:45:25 +05:30
parent 0ad6620437
commit 92444eacb8
5 changed files with 121 additions and 14 deletions
+14
View File
@@ -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;
+39
View File
@@ -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);
});
});