Skip to content

Commit

Permalink
Fix ComponentClass type definition (#20)
Browse files Browse the repository at this point in the history
* Fix ComponentClass type definition

* Fix hook and ComponentClass types
  • Loading branch information
rkuykendall authored Feb 27, 2019
1 parent 36380b5 commit 19905d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
27 changes: 15 additions & 12 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@ import React, { Component } from 'react';
import Tester from './tester';
import ConfigurationClass from './ConfigurationClass';

export type ComponentClass = React.FC | (new() => Component<any>);
export type ComponentClass = React.FC | (new (props: any) => Component<any>);

export interface IMountOps {
async?: boolean;
}

export interface IWrapper {
component: ComponentClass;
name: string;
props: object;
renderChildren?: boolean;
}
export type IOnBeforeMount = (tester: Tester, mountOpts?: IMountOps) => void | Promise<void>;
export type IOnInit = (tester: Tester) => void;

export interface IHook extends IWrapper {
export interface IBaseHook {
[key: string]: any;
onBeforeMount: (tester: Tester, mountOpts?: IMountOps) => void | Promise<void>;
onInit: (tester: Tester) => void;
props: object | (() => void); // fn() allows this.AppState to be set for e.g
name: string;
onBeforeMount?: IOnBeforeMount;
onInit?: IOnInit;
shortCuts?: { [shortCutName: string]: () => void };
wrapper?: () => { Component: ComponentClass, name: string, props: object };
}

export interface IWrapper extends IBaseHook {
component: ComponentClass;
props?: object | (() => void); // fn() allows this.AppState to be set for e.g
renderChildren?: boolean;
}

export type IHook = IBaseHook | IWrapper;

export interface IProfile {
[key: string]: boolean | string;
name: string;
Expand Down
11 changes: 6 additions & 5 deletions src/tester.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './utils';

import ConfigurationClass from './ConfigurationClass';
import { IHook, IProfile, ITesterOpts, IWrapper, ComponentClass } from './interfaces';
import { IHook, IProfile, ITesterOpts, IWrapper, ComponentClass, IOnInit, IOnBeforeMount } from './interfaces';

type ISelectArg = string | { simulate: (event: string) => void };

Expand Down Expand Up @@ -76,9 +76,8 @@ class Tester {
}

// Loop through hooks onInit(),
this.config.getValidHooks(this, 'onInit').forEach((hook: IHook) => {
hook.onInit(this);
});
const validHooks = this.config.getValidHooks(this, 'onInit') as Array<IHook & { onInit: IOnInit }>;
validHooks.forEach(hook => hook.onInit(this));
}

public getWrappers (): IWrapper[] {
Expand Down Expand Up @@ -169,7 +168,9 @@ class Tester {

// Loop through hooks onBeforeMount(),
// This MUST be a regular for () loop to not throw the promise away. (forEach won't work)
for (const hook of this.config.getValidHooks(this, 'onBeforeMount')) {
type IValidHook = IHook & { onBeforeMount: IOnBeforeMount };
const validHooks = this.config.getValidHooks(this, 'onBeforeMount') as IValidHook[];
for (const hook of validHooks) {
await hook.onBeforeMount(this, mountOpts);
}

Expand Down

0 comments on commit 19905d7

Please sign in to comment.