Added usePlugin hooks

Summary:
`usePlugin(pluginFactory)` returns the current plugin instance's api that was exposed by the plugin directory.

Passing `pluginFactory` is technically strictly not needed, but having the user pass it, we can make sure it is strongly typed

Reviewed By: nikoant

Differential Revision: D22286293

fbshipit-source-id: 4268b6849b8cd3d524103de7eadbd6c0a65c7a61
This commit is contained in:
Michel Weststrate
2020-07-01 08:58:40 -07:00
committed by Facebook GitHub Bot
parent 952e929699
commit 159c0deaf1
5 changed files with 44 additions and 8 deletions

View File

@@ -16,6 +16,7 @@ import {
SandyPluginDefinition,
FlipperClient,
TestUtils,
usePlugin,
} from 'flipper-plugin';
import {selectPlugin} from '../reducers/connections';
@@ -92,8 +93,17 @@ test('PluginContainer can render Sandy plugins', async () => {
function MySandyPlugin() {
renders++;
const sandyContext = useContext(SandyPluginContext);
expect(sandyContext).not.toBe(null);
const sandyApi = usePlugin(plugin);
expect(Object.keys(sandyApi)).toEqual([
'connectedStub',
'disconnectedStub',
]);
expect(() => {
// eslint-disable-next-line
usePlugin(function bla() {
return {};
});
}).toThrowError(/didn't match the type of the requested plugin/);
return <div>Hello from Sandy</div>;
}