[enzyme] Add renderProp() function

This commit is contained in:
Christian Rackerseder
2019-02-19 09:42:34 +01:00
parent 90961b6110
commit 6362af9947
2 changed files with 19 additions and 2 deletions
+12 -1
View File
@@ -10,7 +10,7 @@ import {
ShallowRendererProps,
ComponentClass as EnzymeComponentClass
} from "enzyme";
import { Component, ReactElement, HTMLAttributes, ComponentClass, StatelessComponent } from "react";
import { Component, ReactElement, ReactNode, HTMLAttributes, ComponentClass, StatelessComponent } from "react";
// Help classes/interfaces
interface MyComponentProps {
@@ -35,6 +35,10 @@ interface MyComponentState {
stateProperty: string;
}
interface MyRenderPropProps {
children: (params: string) => ReactNode;
}
function toComponentType<T>(Component: ComponentClass<T> | StatelessComponent<T>): ComponentClass<T> | StatelessComponent<T> {
return Component;
}
@@ -59,6 +63,8 @@ class AnotherComponent extends Component<AnotherComponentProps> {
}
}
class MyRenderPropComponent extends Component<MyRenderPropProps> {}
const MyStatelessComponent = (props: StatelessProps) => <span />;
const AnotherStatelessComponent = (props: AnotherStatelessProps) => <span />;
@@ -477,6 +483,11 @@ function ShallowWrapperTest() {
shallowWrapper = new ShallowWrapper<MyComponentProps, MyComponentState>(<MyComponent stringProp="1" numberProp={1} />, undefined, { lifecycleExperimental: true });
shallowWrapper = new ShallowWrapper<MyComponentProps, MyComponentState>(<MyComponent stringProp="1" numberProp={1} />, shallowWrapper, { lifecycleExperimental: true });
}
function test_renderProp() {
let shallowWrapper = new ShallowWrapper<MyRenderPropProps>(<MyRenderPropComponent children={(params) => <div className={params} />} />);
shallowWrapper = shallowWrapper.renderProp('children')('test');
}
}
// ReactWrapper
+7 -1
View File
@@ -1,4 +1,4 @@
// Type definitions for Enzyme 3.1
// Type definitions for Enzyme 3.9
// Project: https://github.com/airbnb/enzyme
// Definitions by: Marian Palkus <https://github.com/MarianPalkus>
// Cap3 <http://www.cap3.de>
@@ -8,6 +8,7 @@
// MartynasZilinskas <https://github.com/MartynasZilinskas>
// Torgeir Hovden <https://github.com/thovden>
// Martin Hochel <https://github.com/hotell>
// Christian Rackerseder <https://github.com/screendriver>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
@@ -447,6 +448,11 @@ export class ShallowWrapper<P = {}, S = {}, C = Component> {
* Returns a wrapper with the direct parent of the node in the current wrapper.
*/
parent(): ShallowWrapper<any, any>;
/**
* Returns a wrapper of the node rendered by the provided render prop.
*/
renderProp<PropName extends keyof P>(prop: PropName): (...params: any[]) => ShallowWrapper<P, S>;
}
// tslint:disable-next-line no-empty-interface