diff --git a/types/enzyme/enzyme-tests.tsx b/types/enzyme/enzyme-tests.tsx index bbdd8f404f..b97711edec 100644 --- a/types/enzyme/enzyme-tests.tsx +++ b/types/enzyme/enzyme-tests.tsx @@ -36,10 +36,18 @@ interface MyComponentState { } class MyComponent extends Component { + handleEcho(value: string) { + return value; + } setState(...args: any[]) { console.log(args); } } +class MyComponentPropsOnly extends Component { + handleEcho(value: string) { + return value; + } +} class AnotherComponent extends Component { 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(); + wrapper.instance().handleEcho('it works'); + + const wrapperPropsOnly = shallow(); + 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(); + wrapperPropsOnly.setProps({stringProp: 'new value'}); + // $ExpectError + wrapperPropsOnly.instance().handleEcho; + + const wrapper = mount(); + wrapper.instance().handleEcho('it works'); } function test_update() { diff --git a/types/enzyme/index.d.ts b/types/enzyme/index.d.ts index 0a741460f6..f8dc9e9406 100644 --- a/types/enzyme/index.d.ts +++ b/types/enzyme/index.d.ts @@ -7,6 +7,7 @@ // huhuanming // MartynasZilinskas // Torgeir Hovden +// Martin Hochel // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.6 @@ -45,7 +46,7 @@ export type EnzymeSelector = string | StatelessComponent | ComponentClass = (intercepter: T) => void; -export interface CommonWrapper

{ +export interface CommonWrapper

> { /** * 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

{ * * NOTE: can only be called on a wrapper instance that is also the root instance. */ - instance(): Component; + 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

{ } // tslint:disable-next-line no-empty-interface -export interface ShallowWrapper

extends CommonWrapper { } -export class ShallowWrapper

{ +export interface ShallowWrapper

extends CommonWrapper { } +export class ShallowWrapper

{ constructor(nodes: JSX.Element[] | JSX.Element, root?: ShallowWrapper, options?: ShallowRendererProps); shallow(options?: ShallowRendererProps): ShallowWrapper; unmount(): this; @@ -440,8 +441,8 @@ export class ShallowWrapper

{ } // tslint:disable-next-line no-empty-interface -export interface ReactWrapper

extends CommonWrapper { } -export class ReactWrapper

{ +export interface ReactWrapper

extends CommonWrapper { } +export class ReactWrapper

{ constructor(nodes: JSX.Element | JSX.Element[], root?: ReactWrapper, 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(node: ReactElement

, options?: ShallowRendererProps): ShallowWrapper; export function shallow

(node: ReactElement

, options?: ShallowRendererProps): ShallowWrapper; export function shallow(node: ReactElement

, options?: ShallowRendererProps): ShallowWrapper; /** * Mounts and renders a react component into the document and provides a testing wrapper around it. */ +export function mount(node: ReactElement

, options?: MountRendererProps): ReactWrapper; export function mount

(node: ReactElement

, options?: MountRendererProps): ReactWrapper; export function mount(node: ReactElement

, options?: MountRendererProps): ReactWrapper;