Keep Navigation plugin alive even when disabled

Summary:
Navigation plugin is a special cause that will remain connected and process messages directly even when disabled, this is to make sure the bookmarks feature keeps working even when the plugin is not enabled.

Changelog: [Sandy][Navigation] on Android, the currently active deeplink of the application will now be shown in the sidebar

Reviewed By: jknoxville

Differential Revision: D24890375

fbshipit-source-id: eb5e4141740e0436396cea5a7aae24337f2e903e
This commit is contained in:
Michel Weststrate
2020-11-12 04:13:16 -08:00
committed by Facebook GitHub Bot
parent 273b895e30
commit b66f452271
4 changed files with 122 additions and 4 deletions

View File

@@ -30,10 +30,14 @@ function plugin(client: PluginClient<any, any>) {
const connectStub = jest.fn();
const disconnectStub = jest.fn();
const destroyStub = jest.fn();
const messages: any[] = [];
client.onConnect(connectStub);
client.onDisconnect(disconnectStub);
client.onDestroy(destroyStub);
client.onMessage('message', (msg) => {
messages.push(msg);
});
initialized = true;
@@ -42,6 +46,7 @@ function plugin(client: PluginClient<any, any>) {
disconnectStub,
destroyStub,
send: client.send,
messages,
};
}
const TestPlugin = new SandyPluginDefinition(pluginDetails, {
@@ -229,3 +234,62 @@ test('it can send messages from sandy clients', async () => {
}
`);
});
test('it should initialize "Navigation" plugin if not enabled', async () => {
const {client, store} = await createMockFlipperWithPlugin(TestPlugin);
const Plugin2 = new SandyPluginDefinition(
TestUtils.createMockPluginDetails({
name: 'Plugin2',
id: 'Navigation',
}),
{
plugin: jest.fn().mockImplementation(plugin),
Component() {
return null;
},
},
);
const pluginState1 = client.sandyPluginStates.get(TestPlugin.id);
expect(pluginState1).toBeInstanceOf(SandyPluginInstance);
store.dispatch(registerPlugins([Plugin2]));
await client.refreshPlugins();
// not enabled, but Navigation is an exception, so we still get an instance
const origInstance = client.sandyPluginStates.get(Plugin2.id);
expect(origInstance).toBeDefined();
expect(Plugin2.asPluginModule().plugin).toBeCalledTimes(1);
store.dispatch(
starPlugin({
plugin: Plugin2,
selectedApp: client.query.app,
}),
);
expect(client.sandyPluginStates.get(Plugin2.id)).toBe(origInstance);
const instance = client.sandyPluginStates.get(Plugin2.id)!
.instanceApi as PluginApi;
expect(Plugin2.asPluginModule().plugin).toBeCalledTimes(1);
expect(instance.destroyStub).toHaveBeenCalledTimes(0);
// disable plugin again
store.dispatch(
starPlugin({
plugin: Plugin2,
selectedApp: client.query.app,
}),
);
// stil enabled
expect(client.sandyPluginStates.get(Plugin2.id)).toBe(origInstance);
expect(instance.connectStub).toHaveBeenCalledTimes(0);
// disconnect wasn't called because connect was never called
expect(instance.disconnectStub).toHaveBeenCalledTimes(0);
expect(instance.destroyStub).toHaveBeenCalledTimes(0);
// closing does stop the plugin!
client.close();
expect(instance.destroyStub).toHaveBeenCalledTimes(1);
expect(client.sandyPluginStates.get(Plugin2.id)).toBeUndefined();
});