Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose custom return type for our mount function #63

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions src/mount.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as enzyme from 'enzyme';
import type { ReactWrapper } from 'enzyme';
import type { VNode } from 'preact';
import { FunctionComponent, JSX, VNode } from 'preact';

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

export type EnzymeSelector = string | FunctionComponent<any>;

/**
* Inspired by DefinitelyTyped's ReactWrapper.
* @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/cebb88fecfa52f854826e216a537867c11b0150e/types/enzyme/index.d.ts
*/
export type ComponentWrapper<P = {}, S = {}> = {
at(index: number): ComponentWrapper;
childAt(index: number): ComponentWrapper<any, any>;

closest<P2>(component: FunctionComponent<P2>): ComponentWrapper<P2, any>;
closest(selector: string): ComponentWrapper<JSX.HTMLAttributes, any>;

exists(selector?: EnzymeSelector): boolean;

find<P2>(component: FunctionComponent<P2>): ComponentWrapper<P2, any>;
find(selector: string): ComponentWrapper<JSX.HTMLAttributes, any>;

first(): ComponentWrapper<P, S>;
forEach(
fn: (wrapper: ComponentWrapper<P, S>, index: number) => any,
): ComponentWrapper<P, S>;
getDOMNode(): HTMLElement;
hasClass(className: string | RegExp): boolean;
last(): ComponentWrapper<P, S>;
length: number;
map<V>(fn: (wrapper: ComponentWrapper<P, S>, index: number) => V): V[];
prop<K extends keyof P>(key: K): P[K];
props(): P;
setProps<K extends keyof P>(props: Pick<P, K>): ComponentWrapper<P, S>;
simulate(event: string, ...args: any[]): ComponentWrapper<P, S>;
text(): string;
unmount(): void;
update(): ComponentWrapper<P, S>;
};

/**
* Render a Preact component using Enzyme and return a wrapper.
*
* The component can be unmounted by calling `wrapper.unmount()` or by calling
* {@link unmountAll} at the end of the test.
*/
export function mount(
export function mount<P = {}, S = {}>(
jsx: VNode,
{ connected = false, prepareContainer }: MountOptions = {},
) {
): ComponentWrapper<P, S> {
let wrapper;
if (connected) {
const container = document.createElement('div');
Expand All @@ -46,7 +82,7 @@ export function mount(

wrappers.push(wrapper);

return wrapper;
return wrapper as ComponentWrapper<P, S>;
}

/**
Expand Down
Loading