Allow plugins to send messages
Summary: Sandy plugins can now send messages to plugins. The methods and params are strongly typed in implementation and unit tests, based on the <Methods> generic of FlipperClient. Reviewed By: nikoant Differential Revision: D22256972 fbshipit-source-id: 549523a402949b3eb6bb4b4ca160dedb5c5e722d
This commit is contained in:
committed by
Facebook GitHub Bot
parent
8b2d8498e6
commit
ec85dd5b01
@@ -30,7 +30,7 @@ beforeEach(() => {
|
||||
initialized = false;
|
||||
});
|
||||
|
||||
function plugin(client: FlipperClient) {
|
||||
function plugin(client: FlipperClient<any, any>) {
|
||||
const connectStub = jest.fn();
|
||||
const disconnectStub = jest.fn();
|
||||
const destroyStub = jest.fn();
|
||||
@@ -45,6 +45,7 @@ function plugin(client: FlipperClient) {
|
||||
connectStub,
|
||||
disconnectStub,
|
||||
destroyStub,
|
||||
send: client.send,
|
||||
};
|
||||
}
|
||||
const TestPlugin = new SandyPluginDefinition(pluginDetails, {
|
||||
@@ -207,4 +208,30 @@ test('it trigger hooks for background plugins', async () => {
|
||||
expect(pluginInstance.disconnectStub).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('it can send messages from sandy clients', async () => {
|
||||
let testMethodCalledWith: any = undefined;
|
||||
const {client} = await createMockFlipperWithPlugin(TestPlugin, {
|
||||
onSend(method, params) {
|
||||
if (method === 'execute') {
|
||||
testMethodCalledWith = params;
|
||||
return {};
|
||||
}
|
||||
},
|
||||
});
|
||||
const pluginInstance: PluginApi = client.sandyPluginStates.get(TestPlugin.id)!
|
||||
.instanceApi;
|
||||
// without rendering, non-bg plugins won't connect automatically,
|
||||
client.initPlugin(TestPlugin.id);
|
||||
await pluginInstance.send('test', {test: 3});
|
||||
expect(testMethodCalledWith).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"api": "TestPlugin",
|
||||
"method": "test",
|
||||
"params": Object {
|
||||
"test": 3,
|
||||
},
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
// TODO: T68683449 state is persisted if a plugin connects and reconnects
|
||||
|
||||
Reference in New Issue
Block a user