jest-chrome
Version:
Test Chrome extensions with Jest. A complete mock of the Chrome API.
78 lines (77 loc) • 3.38 kB
TypeScript
export interface EventCallback {
(...args: any[]): void;
}
export interface EventSelector<C extends EventCallback> {
(...args: any[]): Parameters<C>;
}
export interface MonotypeEventSelector<C extends EventCallback> {
(...args: Parameters<C>): Parameters<C>;
}
export interface OptionsSelector {
(...options: any[]): any[];
}
/** An object which allows the addition, removal, and invocation of listener functions. */
declare type CoreEvent<C extends EventCallback> = {
/**
* Registers an event listener callback to an event.
* @param callback Called when an event occurs. The parameters of this function depend on the type of event.
* The callback parameter should be a function that looks like this:
* function() {...};
*/
addListener(callback: C): void;
/**
* @param callback Listener whose registration status shall be tested.
*/
hasListener(callback: C): boolean;
hasListeners(): boolean;
/**
* Deregisters an event listener callback from an event.
* @param callback Listener that shall be unregistered.
* The callback parameter should be a function that looks like this:
* function() {...};
*/
removeListener(callback: C): void;
};
declare type Callable<C extends EventCallback, R extends MonotypeEventSelector<C> | EventSelector<C>> = {
/**
* Calls all listeners with a data argument.
*/
callListeners: (...args: Parameters<R>) => void;
/**
* Remove all listeners
*/
clearListeners(): void;
/**
* Get the listener Set
*/
getListeners(): Set<C>;
/**
* Returns CallableEvent without callListeners
*/
toEvent(): CoreEvent<C>;
};
export declare type CallableEvent<C extends EventCallback, R extends MonotypeEventSelector<C> | EventSelector<C>> = CoreEvent<C> & Callable<C, R>;
export declare const createEvent: <C extends EventCallback>(selector: EventSelector<C>, validator?: OptionsSelector | undefined) => CallableEvent<EventCallback, EventSelector<C>>;
/**
* Create an event that takes a single callback as an argument
*
* @export
* @template C The callback signature
* @template R The event selector signature
* @param {R} selector Validate a call to callListeners and map it to the callback arguments
* @returns {CallableEvent<C, R>}
*/
export declare function createSetEvent<C extends EventCallback, R extends EventSelector<C> | MonotypeEventSelector<C>>(selector: R): CallableEvent<C, R>;
/**
* Create an event that takes a single callback and any number of option arguments
*
* @export
* @template C The listener callback signature. Will be called with the result of the event selector
* @template E The event selector function signature. Will be called with the result of the options selector and the arguments for callListeners
* @template O The options selector function signature. Will be called with the options arguments from addListener
* @param {E} eventSelector Validate a call to callListeners and map it to the callback arguments
* @param {O} [optionsSelector] Validate the options arguments passed to addListener
* @returns {CallableEvent<C, E>}
*/
export declare function createMapEvent<C extends EventCallback, E extends EventSelector<C> | MonotypeEventSelector<C>, O extends OptionsSelector>(eventSelector: E, optionsSelector?: O): CallableEvent<C, E>;
export {};