UNPKG

flipper-plugin

Version:

Flipper Desktop plugin SDK and components

144 lines 6.35 kB
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format */ /// <reference types="jest" /> import type { RenderResult } from '@testing-library/react'; import { queries } from '@testing-library/dom'; import { FlipperServerConfig, ServerAddOnControls } from 'flipper-common'; import { DeviceLogEntry } from 'flipper-common'; import { InstalledPluginDetails } from 'flipper-common'; import { FlipperServer, FlipperServerCommands } from 'flipper-common'; import { Device } from '../plugin/DevicePlugin'; import { FlipperLib } from '../plugin/FlipperLib'; import { PluginClient, PluginFactory } from '../plugin/Plugin'; import { FlipperDevicePluginModule, FlipperPluginModule, SandyPluginDefinition } from '../plugin/SandyPluginDefinition'; export interface StartPluginOptions { initialState?: Record<string, any>; isArchived?: boolean; isBackgroundPlugin?: boolean; startUnactivated?: boolean; /** Provide a set of unsupported methods to simulate older clients that don't support certain methods yet */ unsupportedMethods?: string[]; /** * Provide a set of GKs that are enabled in this test. */ GKs?: string[]; testDevice?: Device; } export declare function createStubFunction(): jest.Mock<any, any>; export declare function createMockFlipperLib(options?: StartPluginOptions): FlipperLib; export declare function createMockPluginDetails(details?: Partial<InstalledPluginDetails>): InstalledPluginDetails; export declare function createTestPlugin<T extends PluginFactory<any, any, any, any>>(implementation: Pick<FlipperPluginModule<T>, 'plugin'> & Partial<FlipperPluginModule<T>>, details?: Partial<InstalledPluginDetails>): SandyPluginDefinition; export declare function createTestDevicePlugin(implementation: Pick<FlipperDevicePluginModule, 'devicePlugin'> & Partial<FlipperDevicePluginModule>, details?: Partial<InstalledPluginDetails>): SandyPluginDefinition; export declare function createFlipperServerMock(overrides?: Partial<FlipperServerCommands>): FlipperServer; type Renderer = RenderResult<typeof queries>; type ExtractClientType<Module extends FlipperPluginModule<any>> = Parameters<Module['plugin']>[0]; type ExtractMethodsType<Module extends FlipperPluginModule<any>> = ExtractClientType<Module> extends PluginClient<any, infer Methods> ? Methods : never; type ExtractEventsType<Module extends FlipperPluginModule<any>> = ExtractClientType<Module> extends PluginClient<infer Events, any> ? Events : never; interface BasePluginResult { /** * Mock for Flipper utilities */ flipperLib: FlipperLib; /** * Emulates the 'onActivate' event */ activate(): void; /** * Emulates the 'onActivate' event (when the user opens the plugin in the UI). * Will also trigger the `onConnect` event for non-background plugins */ deactivate(): void; /** * Emulates the 'destroy' event. After calling destroy this plugin instance won't be usable anymore */ destroy(): void; /** * Emulate triggering a deeplink */ triggerDeepLink(deeplink: unknown): Promise<void>; /** * Grab all the persistable state, but will ignore any onExport handler */ exportState(): Record<string, any>; /** * Grab all the persistable state, respecting onExport handlers */ exportStateAsync(): Promise<Record<string, any>>; /** * Trigger menu entry by label */ triggerMenuEntry(label: string): void; /** * Communication with a server add-on */ serverAddOnControls: ServerAddOnControls; } interface StartPluginResult<Module extends FlipperPluginModule<any>> extends BasePluginResult { /** * the instantiated plugin for this test */ instance: ReturnType<Module['plugin']>; /** * module, from which any other exposed methods can be accessed during testing */ module: Module; /** * Emulates the 'onConnect' event */ connect(): void; /** * Emulatese the 'onDisconnect' event */ disconnect(): void; /** * Jest Stub that is called whenever client.send() is called by the plugin. * Use send.mockImplementation(function) to intercept the calls. */ onSend: jest.MockedFunction<(<Method extends keyof ExtractMethodsType<Module>>(method: Method, params: Parameters<ExtractMethodsType<Module>[Method]>[0]) => ReturnType<ExtractMethodsType<Module>[Method]>)>; /** * Send event to the plugin */ sendEvent<Event extends keyof ExtractEventsType<Module>>(event: Event, params: ExtractEventsType<Module>[Event]): void; /** * Send events to the plugin * The structure used here reflects events that can be recorded * with the pluginRecorder */ sendEvents(events: { method: keyof ExtractEventsType<Module>; params: any; }[]): void; } interface StartDevicePluginResult<Module extends FlipperDevicePluginModule> extends BasePluginResult { /** * the instantiated plugin for this test */ instance: ReturnType<Module['devicePlugin']>; /** * module, from which any other exposed methods can be accessed during testing */ module: Module; /** * Emulates sending a log message arriving from the device */ sendLogEntry(logEntry: DeviceLogEntry): void; } export declare function startPlugin<Module extends FlipperPluginModule<any>>(module: Module, options?: StartPluginOptions): StartPluginResult<Module>; export declare function renderPlugin<Module extends FlipperPluginModule<any>>(module: Module, options?: StartPluginOptions): StartPluginResult<Module> & { renderer: Renderer; act: (cb: () => void) => void; }; export declare function startDevicePlugin<Module extends FlipperDevicePluginModule>(module: Module, options?: StartPluginOptions): StartDevicePluginResult<Module>; export declare function renderDevicePlugin<Module extends FlipperDevicePluginModule>(module: Module, options?: StartPluginOptions): StartDevicePluginResult<Module> & { renderer: Renderer; act: (cb: () => void) => void; }; export declare function createStubFlipperServerConfig(): FlipperServerConfig; export {}; //# sourceMappingURL=test-utils.d.ts.map