Files
flipper/src/dispatcher/__tests__/plugins.node.js
Anton Nikolaev 07bc0088ef Install plugin from package file
Summary:
Adding a way to install plugins directly from package files. This is required for testing after packaging format changes.

Stage 1: refactored the plugin manager component file layout,  no functional changes.

Reviewed By: jknoxville

Differential Revision: D19741085

fbshipit-source-id: bd9d72382ddc4894de5b3cd9a71877c799886fbf
2020-02-06 05:30:05 -08:00

132 lines
3.2 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import dispatcher, {
getDynamicPlugins,
checkDisabled,
checkGK,
requirePlugin,
} from '../plugins.tsx';
import path from 'path';
import {ipcRenderer, remote} from 'electron';
import {FlipperPlugin} from 'flipper';
import reducers from '../../reducers/index.tsx';
import {init as initLogger} from '../../fb-stubs/Logger.tsx';
import configureStore from 'redux-mock-store';
import {TEST_PASSING_GK, TEST_FAILING_GK} from '../../fb-stubs/GK.tsx';
import TestPlugin from './TestPlugin';
const mockStore = configureStore([])(reducers(undefined, {type: 'INIT'}));
const logger = initLogger(mockStore);
test('dispatcher dispatches REGISTER_PLUGINS', () => {
dispatcher(mockStore, logger);
const actions = mockStore.getActions();
expect(actions.map(a => a.type)).toContain('REGISTER_PLUGINS');
});
test('getDynamicPlugins returns empty array on errors', () => {
ipcRenderer.sendSync = jest.fn();
ipcRenderer.sendSync.mockImplementation(() => {
throw new Error('ooops');
});
const res = getDynamicPlugins();
expect(res).toEqual([]);
});
test('getDynamicPlugins from main process via ipc', () => {
const plugins = [{name: 'test'}];
ipcRenderer.sendSync = jest.fn();
ipcRenderer.sendSync.mockReturnValue(plugins);
const res = getDynamicPlugins();
expect(res).toEqual(plugins);
});
test('checkDisabled', () => {
const disabledPlugin = 'pluginName';
const config = {disabledPlugins: [disabledPlugin]};
// $FlowFixMe process.env not defined in electron API spec
remote.process.env.CONFIG = JSON.stringify(config);
const disabled = checkDisabled([]);
expect(
disabled({
name: 'other Name',
out: './test/index.js',
}),
).toBeTruthy();
expect(
disabled({
name: disabledPlugin,
out: './test/index.js',
}),
).toBeFalsy();
});
test('checkGK for plugin without GK', () => {
expect(
checkGK([])({
name: 'pluginID',
out: './test/index.js',
}),
).toBeTruthy();
});
test('checkGK for passing plugin', () => {
expect(
checkGK([])({
name: 'pluginID',
gatekeeper: TEST_PASSING_GK,
out: './test/index.js',
}),
).toBeTruthy();
});
test('checkGK for failing plugin', () => {
const gatekeepedPlugins = [];
const name = 'pluginID';
const plugins = checkGK(gatekeepedPlugins)({
name,
gatekeeper: TEST_FAILING_GK,
out: './test/index.js',
});
expect(plugins).toBeFalsy();
expect(gatekeepedPlugins[0].name).toEqual(name);
});
test('requirePlugin returns null for invalid requires', () => {
const plugin = requirePlugin(
[],
require,
)({
name: 'pluginID',
out: 'this/path/does not/exist',
});
expect(plugin).toBeNull();
});
test('requirePlugin loads plugin', () => {
const name = 'pluginID';
const homepage = 'https://fb.workplace.com/groups/flippersupport/';
const plugin = requirePlugin(
[],
require,
)({
name,
homepage,
out: path.join(__dirname, 'TestPlugin.js'),
});
expect(plugin.prototype).toBeInstanceOf(FlipperPlugin);
expect(plugin.homepage).toBe(homepage);
expect(plugin.id).toBe(TestPlugin.id);
});