Files
flipper/desktop/flipper-ui-core/src/RenderHost.tsx
Michel Weststrate f9b72ac69e Move RenderHost initialisation to Jest
Summary:
This diff moves RenderHost initialisation to jest, which is thereby treated as just another 'Host' like flipper-ui, the electron app etc. A benefit is that it provides a mocked flipperServer by default that can be used to mock or intercept requests. See LaunchEmulator.spec as example.

Also made the jest setup scripts strongly typed by converting them to TS.

This change allows the test stub configuration, which was OS dependent, out of flipper-ui-core.

Reviewed By: nikoant

Differential Revision: D32668632

fbshipit-source-id: fac0c09812b000fd7d1acb75010c35573087c99f
2021-12-08 04:30:55 -08:00

109 lines
3.3 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import type {NotificationEvents} from './dispatcher/notifications';
import type {PluginNotification} from './reducers/notifications';
import type {NotificationConstructorOptions} from 'electron';
import {FlipperLib} from 'flipper-plugin';
import {FlipperServer, FlipperServerConfig} from 'flipper-common';
// Events that are emitted from the main.ts ovr the IPC process bridge in Electron
type MainProcessEvents = {
'flipper-protocol-handler': [query: string];
'open-flipper-file': [url: string];
notificationEvent: [
eventName: NotificationEvents,
pluginNotification: PluginNotification,
arg: null | string | number,
];
trackUsage: any[];
getLaunchTime: [launchStartTime: number];
};
// Events that are emitted by the child process, to the main process
type ChildProcessEvents = {
setTheme: [theme: 'dark' | 'light' | 'system'];
sendNotification: [
{
payload: NotificationConstructorOptions;
pluginNotification: PluginNotification;
closeAfter?: number;
},
];
getLaunchTime: [];
componentDidMount: [];
};
/**
* Utilities provided by the render host, e.g. Electron, the Browser, etc
*/
export interface RenderHost {
readonly processId: number;
readonly isProduction: boolean;
readTextFromClipboard(): string | undefined;
writeTextToClipboard(text: string): void;
/**
* @deprecated
* WARNING!
* It is a low-level API call that might be removed in the future.
* It is not really deprecated yet, but we'll try to make it so.
* TODO: Remove in favor of "exportFile"
*/
showSaveDialog?(options: {
defaultPath?: string;
message?: string;
title?: string;
}): Promise<string | undefined>;
/**
* @deprecated
* WARNING!
* It is a low-level API call that might be removed in the future.
* It is not really deprecated yet, but we'll try to make it so.
* TODO: Remove in favor of "importFile"
*/
showOpenDialog?(options: {
defaultPath?: string;
filter?: {
extensions: string[];
name: string;
};
}): Promise<string | undefined>;
showSelectDirectoryDialog?(defaultPath?: string): Promise<string | undefined>;
importFile: FlipperLib['importFile'];
exportFile: FlipperLib['exportFile'];
/**
* @returns
* A callback to unregister the shortcut
*/
registerShortcut(shortCut: string, callback: () => void): () => void;
hasFocus(): boolean;
onIpcEvent<Event extends keyof MainProcessEvents>(
event: Event,
callback: (...arg: MainProcessEvents[Event]) => void,
): void;
sendIpcEvent<Event extends keyof ChildProcessEvents>(
event: Event,
...args: ChildProcessEvents[Event]
): void;
shouldUseDarkColors(): boolean;
restartFlipper(update?: boolean): void;
openLink(url: string): void;
loadDefaultPlugins(): Record<string, any>;
GK(gatekeeper: string): boolean;
flipperServer: FlipperServer;
serverConfig: FlipperServerConfig;
}
export function getRenderHostInstance(): RenderHost {
if (!window.FlipperRenderHostInstance) {
throw new Error('global FlipperRenderHostInstance was never set');
}
return window.FlipperRenderHostInstance;
}