DefinitelyTyped/types/react-test-renderer/react-test-renderer-tests.ts
John Gozde c0ca3289cb react-test-renderer: update to v16 API (#20396)
* react-test-renderer: copy existing to v15

* react-test-renderer: update to v16 API

* react-test-renderer/shallow: update to v16 API

* react-test-renderer: fix lint, path mappings
2017-10-08 07:11:28 +01:00

69 lines
1.8 KiB
TypeScript

import * as React from "react";
import { create, ReactTestInstance } from "react-test-renderer";
import { createRenderer } from 'react-test-renderer/shallow';
class TestComponent extends React.Component { }
const renderer = create(React.createElement("div"), {
createNodeMock: (el: React.ReactElement<any>) => {
return {};
}
});
const json = renderer.toJSON();
if (json) {
json.type = "t";
json.props = {
prop1: "p",
};
json.children = [json];
}
const tree = renderer.toTree();
if (tree) {
tree.type = "t";
tree.props = {
prop1: "p",
};
tree.children = [tree];
tree.rendered = tree;
tree.nodeType = "component";
tree.nodeType = "host";
}
renderer.update(React.createElement(TestComponent));
renderer.unmount();
renderer.unmount(React.createElement(TestComponent));
function testInstance(inst: ReactTestInstance) {
inst.children = [inst, "a"];
inst.parent = instance;
inst.parent = null;
inst.props = {
prop1: "p",
};
inst.type = "t";
testInstance(inst.find(n => n.type === "t"));
testInstance(inst.findByProps({ prop1: "p" }));
testInstance(inst.findByType("t"));
testInstance(inst.findByType(TestComponent));
inst.findAll(n => n.type === "t", { deep: true }).map(testInstance);
inst.findAllByProps({ prop1: "p" }, { deep: true }).map(testInstance);
inst.findAllByType("t", { deep: true }).map(testInstance);
inst.findAllByType(TestComponent, { deep: true }).map(testInstance);
}
const instance = renderer.getInstance();
if (instance) {
testInstance(instance);
}
testInstance(renderer.root);
const component = React.createElement(TestComponent);
const shallowRenderer = createRenderer();
shallowRenderer.render(component);
shallowRenderer.getRenderOutput();
shallowRenderer.getMountedInstance();