Skip to content

Commit

Permalink
[enzyme] Add renderProp() function
Browse files Browse the repository at this point in the history
  • Loading branch information
screendriver committed Feb 19, 2019
1 parent 90961b6 commit 6362af9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 12 additions & 1 deletion types/enzyme/enzyme-tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand All @@ -59,6 +63,8 @@ class AnotherComponent extends Component<AnotherComponentProps> {
}
}

class MyRenderPropComponent extends Component<MyRenderPropProps> {}

const MyStatelessComponent = (props: StatelessProps) => <span />;

const AnotherStatelessComponent = (props: AnotherStatelessProps) => <span />;
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion types/enzyme/index.d.ts
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6362af9

Please sign in to comment.