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:
Michel Weststrate
2020-07-01 08:58:40 -07:00
committed by Facebook GitHub Bot
parent 8b2d8498e6
commit ec85dd5b01
9 changed files with 161 additions and 10 deletions

View File

@@ -8,14 +8,15 @@
*/
import * as TestUtils from '../test-utils/test-utils';
import * as testPlugin from './TestPlugin';
test('it can start a plugin and lifecycle events', () => {
const {instance, ...p} = TestUtils.startPlugin(testPlugin);
// TODO T69105011 @ts-expect-error
// p.bla;
// @ts-expect-error
p.bla;
// @ts-expect-error
instance.bla;
// startPlugin starts connected
expect(instance.connectStub).toBeCalledTimes(1);
@@ -59,3 +60,36 @@ test('it can render a plugin', () => {
`);
// TODO: test sending updates T68683442
});
test('a plugin can send messages', async () => {
const {instance, onSend} = TestUtils.startPlugin(testPlugin);
// By default send is stubbed
expect(await instance.getCurrentState()).toBeUndefined();
expect(onSend).toHaveBeenCalledWith('currentState', {since: 0});
// @ts-expect-error
onSend('bla');
// ... But we can intercept!
onSend.mockImplementationOnce(async (method, params) => {
expect(method).toEqual('currentState');
expect(params).toEqual({since: 0});
return 3;
});
expect(await instance.getCurrentState()).toEqual(3);
});
test('a plugin cannot send messages after being disconnected', async () => {
const {instance, disconnect} = TestUtils.startPlugin(testPlugin);
disconnect();
let threw = false;
try {
await instance.getCurrentState();
} catch (e) {
threw = true; // for some weird reason expect(async () => instance.getCurrentState()).toThrow(...) doesn't work today...
expect(e).toMatchInlineSnapshot(`[Error: Plugin is not connected]`);
}
expect(threw).toBeTruthy();
});