Skip to content

Commit fd5d0ae

Browse files
authored
Merge pull request #63 from hypothesis/mount-types
Expose custom return type for our mount function
2 parents bc6ae96 + e80c7f2 commit fd5d0ae

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

src/mount.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as enzyme from 'enzyme';
22
import type { ReactWrapper } from 'enzyme';
3-
import type { VNode } from 'preact';
3+
import { FunctionComponent, JSX, VNode } from 'preact';
44

55
let containers: HTMLElement[] = [];
66
let wrappers: ReactWrapper[] = [];
@@ -20,16 +20,52 @@ export type MountOptions = {
2020
prepareContainer?: (container: HTMLElement) => void;
2121
};
2222

23+
export type EnzymeSelector = string | FunctionComponent<any>;
24+
25+
/**
26+
* Inspired by DefinitelyTyped's ReactWrapper.
27+
* @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/cebb88fecfa52f854826e216a537867c11b0150e/types/enzyme/index.d.ts
28+
*/
29+
export type ComponentWrapper<P = {}, S = {}> = {
30+
at(index: number): ComponentWrapper;
31+
childAt(index: number): ComponentWrapper<any, any>;
32+
33+
closest<P2>(component: FunctionComponent<P2>): ComponentWrapper<P2, any>;
34+
closest(selector: string): ComponentWrapper<JSX.HTMLAttributes, any>;
35+
36+
exists(selector?: EnzymeSelector): boolean;
37+
38+
find<P2>(component: FunctionComponent<P2>): ComponentWrapper<P2, any>;
39+
find(selector: string): ComponentWrapper<JSX.HTMLAttributes, any>;
40+
41+
first(): ComponentWrapper<P, S>;
42+
forEach(
43+
fn: (wrapper: ComponentWrapper<P, S>, index: number) => any,
44+
): ComponentWrapper<P, S>;
45+
getDOMNode(): HTMLElement;
46+
hasClass(className: string | RegExp): boolean;
47+
last(): ComponentWrapper<P, S>;
48+
length: number;
49+
map<V>(fn: (wrapper: ComponentWrapper<P, S>, index: number) => V): V[];
50+
prop<K extends keyof P>(key: K): P[K];
51+
props(): P;
52+
setProps<K extends keyof P>(props: Pick<P, K>): ComponentWrapper<P, S>;
53+
simulate(event: string, ...args: any[]): ComponentWrapper<P, S>;
54+
text(): string;
55+
unmount(): void;
56+
update(): ComponentWrapper<P, S>;
57+
};
58+
2359
/**
2460
* Render a Preact component using Enzyme and return a wrapper.
2561
*
2662
* The component can be unmounted by calling `wrapper.unmount()` or by calling
2763
* {@link unmountAll} at the end of the test.
2864
*/
29-
export function mount(
65+
export function mount<P = {}, S = {}>(
3066
jsx: VNode,
3167
{ connected = false, prepareContainer }: MountOptions = {},
32-
) {
68+
): ComponentWrapper<P, S> {
3369
let wrapper;
3470
if (connected) {
3571
const container = document.createElement('div');
@@ -46,7 +82,7 @@ export function mount(
4682

4783
wrappers.push(wrapper);
4884

49-
return wrapper;
85+
return wrapper as ComponentWrapper<P, S>;
5086
}
5187

5288
/**

0 commit comments

Comments
 (0)