Provide initial plugin test infra for plugin devs

Summary:
This sets up the initial infra that is to be used by plugin devs to test plugins.

There is not much yet to see, as there is no state or message sending yet. But at least the life cycle of plugins can be test, things are strongly typed and everything is in the place where it should be :)

N.b. the import difference with these utils and the createFlipperMock utilities in Flipper are

1. this testing infra is entirely inside flipper-plugin package, so that plugin devs don't need flipper as a dependency
2. this testing infra doesn't provide abstractions for plugin / device / client switching; it tests plugins purely in isolation of the rest of the world (except for firing `onConnect` / `onDisconnect` which is normally the effect of switching plugins)

Reviewed By: nikoant

Differential Revision: D22255262

fbshipit-source-id: b94ccbab720d2b49428a646aed3c55af71a5bc80
This commit is contained in:
Michel Weststrate
2020-07-01 08:58:40 -07:00
committed by Facebook GitHub Bot
parent c902a27bce
commit df6a8cd031
14 changed files with 336 additions and 70 deletions

View File

@@ -13,9 +13,11 @@ import {FlipperPluginFactory, FlipperPluginComponent} from './Plugin';
/**
* FlipperPluginModule describe the exports that are provided by a typical Flipper Desktop plugin
*/
export type FlipperPluginModule = {
export type FlipperPluginModule<
Factory extends FlipperPluginFactory<any, any>
> = {
/** the factory function that initializes a plugin instance */
plugin: FlipperPluginFactory<any, any>;
plugin: Factory;
/** the component type that can render this plugin */
Component: FlipperPluginComponent;
// TODO: support device plugins T68738317
@@ -23,14 +25,14 @@ export type FlipperPluginModule = {
};
/**
* A sandy plugin definitions represents a loaded plugin definition, storing two things:
* A sandy plugin definition represents a loaded plugin definition, storing two things:
* the loaded JS module, and the meta data (typically coming from package.json).
*
* Also delegates some of the standard plugin functionality to have a similar public static api as FlipperPlugin
*/
export class SandyPluginDefinition {
id: string;
module: FlipperPluginModule;
module: FlipperPluginModule<any>;
details: PluginDetails;
// TODO: Implement T68683449
@@ -45,7 +47,7 @@ export class SandyPluginDefinition {
) => Promise<any /* TODO: StaticPersistedState | undefined */>)
| undefined = undefined;
constructor(details: PluginDetails, module: FlipperPluginModule) {
constructor(details: PluginDetails, module: FlipperPluginModule<any>) {
this.id = details.id;
this.details = details;
if (!module.plugin || typeof module.plugin !== 'function') {