Files
flipper/desktop/flipper-plugin/src/__tests__/TestPlugin.tsx
Michel Weststrate dd7a9f5195 introduce onReady life-cycle
Summary: Flipper Sandy plugins didn't have an event to hook into that is run _after_ any state snapshot is loaded, which was needed by the graphQL plugin, as they do some post processing when a data snapshot is restored.

Reviewed By: passy

Differential Revision: D28189573

fbshipit-source-id: 4ef992f3fafc32787eab3bc235059f2c41396c80
2021-05-04 12:52:26 -07:00

104 lines
2.4 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 * as React from 'react';
import {PluginClient} from '../plugin/Plugin';
import {usePlugin} from '../plugin/PluginContext';
import {createState, useValue} from '../state/atom';
type Events = {
inc: {
delta: number;
};
};
type Methods = {
currentState(params: {since: number}): Promise<number>;
};
export function plugin(client: PluginClient<Events, Methods>) {
const connectStub = jest.fn();
const disconnectStub = jest.fn();
const activateStub = jest.fn();
const deactivateStub = jest.fn();
const destroyStub = jest.fn();
const readyStub = jest.fn();
const state = createState(
{
count: 0,
},
{
persist: 'counter',
},
);
const unhandledMessages = createState<any[]>([]);
client.onConnect(connectStub);
client.onDisconnect(disconnectStub);
client.onActivate(activateStub);
client.onDeactivate(deactivateStub);
client.onDestroy(destroyStub);
client.onReady(readyStub);
client.onMessage('inc', ({delta}) => {
state.update((draft) => {
draft.count += delta;
});
});
client.onUnhandledMessage((event, params) => {
unhandledMessages.update((draft) => {
draft.push({event, params});
});
});
function _unused_JustTypeChecks() {
// @ts-expect-error Argument of type '"bla"' is not assignable
client.send('bla', {});
// @ts-expect-error Argument of type '{ stuff: string; }' is not assignable to parameter of type
client.send('currentState', {stuff: 'nope'});
// @ts-expect-error
client.onMessage('stuff', (_params) => {
// noop
});
client.onMessage('inc', (params) => {
// @ts-expect-error
params.bla;
});
}
async function getCurrentState() {
return client.send('currentState', {since: 0});
}
expect(client.device).not.toBeNull();
return {
activateStub,
deactivateStub,
connectStub,
destroyStub,
disconnectStub,
readyStub,
getCurrentState,
state,
unhandledMessages,
appId: client.appId,
appName: client.appName,
};
}
export function Component() {
const api = usePlugin(plugin);
const count = useValue(api.state).count;
// @ts-expect-error
api.bla;
return <h1>Hi from test plugin {count}</h1>;
}