Move RenderHost initialisation to Jest

Summary:
This diff moves RenderHost initialisation to jest, which is thereby treated as just another 'Host' like flipper-ui, the electron app etc. A benefit is that it provides a mocked flipperServer by default that can be used to mock or intercept requests. See LaunchEmulator.spec as example.

Also made the jest setup scripts strongly typed by converting them to TS.

This change allows the test stub configuration, which was OS dependent, out of flipper-ui-core.

Reviewed By: nikoant

Differential Revision: D32668632

fbshipit-source-id: fac0c09812b000fd7d1acb75010c35573087c99f
This commit is contained in:
Michel Weststrate
2021-12-08 04:25:28 -08:00
committed by Facebook GitHub Bot
parent e7f841b6d2
commit f9b72ac69e
14 changed files with 237 additions and 270 deletions

View File

@@ -20,6 +20,7 @@ import {useStore} from '../../utils/useStore';
import {Layout, renderReactRoot, withTrackingScope} from 'flipper-plugin';
import {Provider} from 'react-redux';
import {IOSDeviceParams} from 'flipper-common';
import {getRenderHostInstance} from '../../RenderHost';
const COLD_BOOT = 'cold-boot';
@@ -37,7 +38,6 @@ function LaunchEmulatorContainer({onClose}: {onClose: () => void}) {
export const LaunchEmulatorDialog = withTrackingScope(
function LaunchEmulatorDialog({onClose}: {onClose: () => void}) {
const flipperServer = useStore((state) => state.connections.flipperServer);
const iosEnabled = useStore((state) => state.settingsState.enableIOS);
const androidEnabled = useStore(
(state) => state.settingsState.enableAndroid,
@@ -49,8 +49,8 @@ export const LaunchEmulatorDialog = withTrackingScope(
if (!iosEnabled) {
return;
}
flipperServer!
.exec('ios-get-simulators', false)
getRenderHostInstance()
.flipperServer.exec('ios-get-simulators', false)
.then((emulators) => {
setIosEmulators(
emulators.filter(
@@ -63,21 +63,21 @@ export const LaunchEmulatorDialog = withTrackingScope(
.catch((e) => {
console.warn('Failed to find simulators', e);
});
}, [iosEnabled, flipperServer]);
}, [iosEnabled]);
useEffect(() => {
if (!androidEnabled) {
return;
}
flipperServer!
.exec('android-get-emulators')
getRenderHostInstance()
.flipperServer.exec('android-get-emulators')
.then((emulators) => {
setAndroidEmulators(emulators);
})
.catch((e) => {
console.warn('Failed to find emulators', e);
});
}, [androidEnabled, flipperServer]);
}, [androidEnabled]);
const items = [
...(androidEmulators.length > 0
@@ -85,8 +85,8 @@ export const LaunchEmulatorDialog = withTrackingScope(
: []),
...androidEmulators.map((name) => {
const launch = (coldBoot: boolean) => {
flipperServer!
.exec('android-launch-emulator', name, coldBoot)
getRenderHostInstance()
.flipperServer.exec('android-launch-emulator', name, coldBoot)
.then(onClose)
.catch((e) => {
console.error('Failed to start emulator: ', e);
@@ -123,8 +123,8 @@ export const LaunchEmulatorDialog = withTrackingScope(
<Button
key={device.udid}
onClick={() =>
flipperServer!
.exec('ios-launch-simulator', device.udid)
getRenderHostInstance()
.flipperServer.exec('ios-launch-simulator', device.udid)
.catch((e) => {
console.error('Failed to start simulator: ', e);
message.error('Failed to start simulator: ' + e);

View File

@@ -14,18 +14,19 @@ import {createStore} from 'redux';
import {LaunchEmulatorDialog} from '../LaunchEmulator';
import {createRootReducer} from '../../../reducers';
import {sleep, TestUtils} from 'flipper-plugin';
import {sleep} from 'flipper-plugin';
import {getRenderHostInstance} from '../../../RenderHost';
test('Can render and launch android apps - empty', async () => {
const store = createStore(createRootReducer());
const mockServer = TestUtils.createFlipperServerMock({
'ios-get-simulators': () => Promise.resolve([]),
'android-get-emulators': () => Promise.resolve([]),
});
store.dispatch({
type: 'SET_FLIPPER_SERVER',
payload: mockServer,
});
const responses: any = {
'ios-get-simulators': [],
'android-get-emulators': [],
};
getRenderHostInstance().flipperServer.exec = async function (cmd: any) {
return responses[cmd];
} as any;
const onClose = jest.fn();
const renderer = render(
@@ -44,21 +45,16 @@ test('Can render and launch android apps - empty', async () => {
});
test('Can render and launch android apps', async () => {
let p: Promise<any> | undefined = undefined;
const store = createStore(createRootReducer());
const launch = jest.fn().mockImplementation(() => Promise.resolve());
const mockServer = TestUtils.createFlipperServerMock({
'ios-get-simulators': () => Promise.resolve([]),
'android-get-emulators': () =>
(p = Promise.resolve(['emulator1', 'emulator2'])),
'android-launch-emulator': launch,
});
store.dispatch({
type: 'SET_FLIPPER_SERVER',
payload: mockServer,
const exec = jest.fn().mockImplementation(async (cmd) => {
if (cmd === 'android-get-emulators') {
return ['emulator1', 'emulator2'];
}
});
getRenderHostInstance().flipperServer.exec = exec;
store.dispatch({
type: 'UPDATE_SETTINGS',
payload: {
@@ -74,7 +70,7 @@ test('Can render and launch android apps', async () => {
</Provider>,
);
await p!;
await sleep(1); // give exec time to resolve
expect(await renderer.findAllByText(/emulator/)).toMatchInlineSnapshot(`
Array [
@@ -91,5 +87,16 @@ test('Can render and launch android apps', async () => {
fireEvent.click(renderer.getByText('emulator2'));
await sleep(1000);
expect(onClose).toBeCalled();
expect(launch).toBeCalledWith('emulator2', false);
expect(exec.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"android-get-emulators",
],
Array [
"android-launch-emulator",
"emulator2",
false,
],
]
`);
});