Merge pull request #26565 from Hotell/mh/enzyme-support-instance-flow

[enzyme]: propagate component class type via generic on mount/sha…
This commit is contained in:
Nathan Shively-Sanders
2018-06-22 09:27:45 -07:00
committed by GitHub
2 changed files with 37 additions and 8 deletions

View File

@@ -36,10 +36,18 @@ interface MyComponentState {
}
class MyComponent extends Component<MyComponentProps, MyComponentState> {
handleEcho(value: string) {
return value;
}
setState(...args: any[]) {
console.log(args);
}
}
class MyComponentPropsOnly extends Component<MyComponentProps> {
handleEcho(value: string) {
return value;
}
}
class AnotherComponent extends Component<AnotherComponentProps> {
setState(...args: any[]) {
@@ -340,7 +348,16 @@ function ShallowWrapperTest() {
}
function test_instance() {
const myComponent: MyComponent = shallowWrapper.instance();
const myComponent = shallowWrapper.instance() as MyComponent;
myComponent.handleEcho('it works');
const wrapper = shallow<MyComponent>(<MyComponent stringProp="value" numberProp={1}/>);
wrapper.instance().handleEcho('it works');
const wrapperPropsOnly = shallow<MyComponentProps>(<MyComponent stringProp="value" numberProp={1}/>);
wrapperPropsOnly.setProps({stringProp: 'new value'});
// $ExpectError
wrapperPropsOnly.instance().handleEcho;
}
function test_update() {
@@ -696,7 +713,16 @@ function ReactWrapperTest() {
}
function test_instance() {
const myComponent: MyComponent = reactWrapper.instance();
const myComponent = reactWrapper.instance() as MyComponent;
myComponent.handleEcho('it works');
const wrapperPropsOnly = mount<MyComponentProps>(<MyComponent stringProp="value" numberProp={1}/>);
wrapperPropsOnly.setProps({stringProp: 'new value'});
// $ExpectError
wrapperPropsOnly.instance().handleEcho;
const wrapper = mount<MyComponent>(<MyComponent stringProp="value" numberProp={1}/>);
wrapper.instance().handleEcho('it works');
}
function test_update() {

View File

@@ -7,6 +7,7 @@
// huhuanming <https://github.com/huhuanming>
// MartynasZilinskas <https://github.com/MartynasZilinskas>
// Torgeir Hovden <https://github.com/thovden>
// Martin Hochel <https://github.com/hotell>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.6
@@ -45,7 +46,7 @@ export type EnzymeSelector = string | StatelessComponent<any> | ComponentClass<a
export type Intercepter<T> = (intercepter: T) => void;
export interface CommonWrapper<P = {}, S = {}> {
export interface CommonWrapper<P = {}, S = {}, C = Component<P, S>> {
/**
* Returns a new wrapper with only the nodes of the current wrapper that, when passed into the provided predicate function, return true.
*/
@@ -258,7 +259,7 @@ export interface CommonWrapper<P = {}, S = {}> {
*
* NOTE: can only be called on a wrapper instance that is also the root instance.
*/
instance(): Component<P, S>;
instance(): C;
/**
* Forces a re-render. Useful to run before checking the render output if something external may be updating
@@ -354,8 +355,8 @@ export interface CommonWrapper<P = {}, S = {}> {
}
// tslint:disable-next-line no-empty-interface
export interface ShallowWrapper<P = {}, S = {}> extends CommonWrapper<P, S> { }
export class ShallowWrapper<P = {}, S = {}> {
export interface ShallowWrapper<P = {}, S = {}, C = Component> extends CommonWrapper<P, S, C> { }
export class ShallowWrapper<P = {}, S = {}, C = Component> {
constructor(nodes: JSX.Element[] | JSX.Element, root?: ShallowWrapper<any, any>, options?: ShallowRendererProps);
shallow(options?: ShallowRendererProps): ShallowWrapper<P, S>;
unmount(): this;
@@ -440,8 +441,8 @@ export class ShallowWrapper<P = {}, S = {}> {
}
// tslint:disable-next-line no-empty-interface
export interface ReactWrapper<P = {}, S = {}> extends CommonWrapper<P, S> { }
export class ReactWrapper<P = {}, S = {}> {
export interface ReactWrapper<P = {}, S = {}, C = Component> extends CommonWrapper<P, S, C> { }
export class ReactWrapper<P = {}, S = {}, C = Component> {
constructor(nodes: JSX.Element | JSX.Element[], root?: ReactWrapper<any, any>, options?: MountRendererProps);
unmount(): this;
@@ -588,12 +589,14 @@ export interface MountRendererProps {
* Shallow rendering is useful to constrain yourself to testing a component as a unit, and to ensure that
* your tests aren't indirectly asserting on behavior of child components.
*/
export function shallow<C extends Component, P = Component['props'], S = Component['state']>(node: ReactElement<P>, options?: ShallowRendererProps): ShallowWrapper<P, S, C>;
export function shallow<P>(node: ReactElement<P>, options?: ShallowRendererProps): ShallowWrapper<P, any>;
export function shallow<P, S>(node: ReactElement<P>, options?: ShallowRendererProps): ShallowWrapper<P, S>;
/**
* Mounts and renders a react component into the document and provides a testing wrapper around it.
*/
export function mount<C extends Component, P = Component['props'], S = Component['state']>(node: ReactElement<P>, options?: MountRendererProps): ReactWrapper<P, S, C>;
export function mount<P>(node: ReactElement<P>, options?: MountRendererProps): ReactWrapper<P, any>;
export function mount<P, S>(node: ReactElement<P>, options?: MountRendererProps): ReactWrapper<P, S>;