Add infra to test a plugin
Summary: There were several reports that it is hard to test an entire plugin, and send updates. This should address that and help with avoiding plugin regressions in the future. Added a `renderMockFlipperWithPlugin` utility and demo test. Reviewed By: passy Differential Revision: D22114217 fbshipit-source-id: ceefd954abc4ea47c336eab495fb50f161993df9
This commit is contained in:
committed by
Facebook GitHub Bot
parent
5dd6edc533
commit
f373872b5c
@@ -7,7 +7,14 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
import {createStore} from 'redux';
|
||||
import {Provider} from 'react-redux';
|
||||
import {
|
||||
render,
|
||||
RenderResult,
|
||||
act as testingLibAct,
|
||||
} from '@testing-library/react';
|
||||
|
||||
import {
|
||||
selectPlugin,
|
||||
@@ -24,6 +31,7 @@ import {buildClientId} from '../utils/clientUtils';
|
||||
import {Logger} from '../fb-interfaces/Logger';
|
||||
import {FlipperPlugin} from '../plugin';
|
||||
import {registerPlugins} from '../reducers/plugins';
|
||||
import PluginContainer from '../PluginContainer';
|
||||
|
||||
export function createStubLogger(): Logger {
|
||||
return {
|
||||
@@ -33,16 +41,19 @@ export function createStubLogger(): Logger {
|
||||
};
|
||||
}
|
||||
|
||||
type MockFlipperCallbackArgs = {
|
||||
client: Client;
|
||||
device: BaseDevice;
|
||||
store: Store;
|
||||
sendMessage(method: string, params: any): void;
|
||||
createDevice(serial: string): BaseDevice;
|
||||
createClient(device: BaseDevice, name: string): Client;
|
||||
logger: Logger;
|
||||
};
|
||||
|
||||
export async function createMockFlipperWithPlugin(
|
||||
pluginClazz: typeof FlipperPlugin,
|
||||
callback: (args: {
|
||||
client: Client;
|
||||
device: BaseDevice;
|
||||
store: Store;
|
||||
sendMessage(method: string, params: any): void;
|
||||
createDevice(serial: string): BaseDevice;
|
||||
createClient(device: BaseDevice, name: string): Client;
|
||||
}) => Promise<void>,
|
||||
callback: (args: MockFlipperCallbackArgs) => Promise<void>,
|
||||
) {
|
||||
const store = createStore(reducers);
|
||||
const logger = createStubLogger();
|
||||
@@ -141,5 +152,50 @@ export async function createMockFlipperWithPlugin(
|
||||
},
|
||||
createDevice,
|
||||
createClient,
|
||||
logger,
|
||||
});
|
||||
}
|
||||
|
||||
type Renderer = RenderResult<typeof import('testing-library__dom/queries')>;
|
||||
|
||||
export async function renderMockFlipperWithPlugin(
|
||||
pluginClazz: typeof FlipperPlugin,
|
||||
callback: (
|
||||
args: MockFlipperCallbackArgs & {
|
||||
renderer: Renderer;
|
||||
act: (cb: () => void) => void;
|
||||
},
|
||||
) => Promise<void>,
|
||||
) {
|
||||
return createMockFlipperWithPlugin(pluginClazz, async (args) => {
|
||||
function selectTestPlugin(store: Store, client: Client) {
|
||||
store.dispatch(
|
||||
selectPlugin({
|
||||
selectedPlugin: pluginClazz.id,
|
||||
selectedApp: client.query.app,
|
||||
deepLinkPayload: null,
|
||||
selectedDevice: store.getState().connections.selectedDevice!,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
selectTestPlugin(args.store, args.client);
|
||||
|
||||
const renderer = render(
|
||||
<Provider store={args.store}>
|
||||
<PluginContainer logger={args.logger} />
|
||||
</Provider>,
|
||||
);
|
||||
|
||||
await callback({
|
||||
...args,
|
||||
renderer: renderer as any,
|
||||
act(cb) {
|
||||
testingLibAct(cb);
|
||||
args.client.flushMessageBuffer();
|
||||
},
|
||||
});
|
||||
|
||||
renderer.unmount();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user