Skip to content

Commit

Permalink
Add PluginUtils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrunyon committed Oct 18, 2023
1 parent 5f1debd commit 80116e2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
6 changes: 3 additions & 3 deletions packages/dashboard-core-plugins/src/panels/ConsolePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
RootState,
} from '@deephaven/redux';
import { assertNotNull } from '@deephaven/utils';
import { getIconForType, pluginSupportsType } from '@deephaven/plugin';
import { getIconForPlugin, pluginSupportsType } from '@deephaven/plugin';
import type { JSZipObject } from 'jszip';
import { ConsoleEvent } from '../events';
import Panel from './Panel';
Expand Down Expand Up @@ -326,9 +326,9 @@ export class ConsolePanel extends PureComponent<
const { plugins } = this.props;
const plugin = [...plugins.values()].find(p => pluginSupportsType(p, type));
if (plugin != null) {
return getIconForType(plugin, type);
return getIconForPlugin(plugin);
}
// TODO: #1573 Remove this default and always return getIconForType
// TODO: #1573 Remove this default and always return getIconForPlugin
return <ObjectIcon type={type} />;
}

Expand Down
60 changes: 60 additions & 0 deletions packages/plugin/src/PluginUtils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import { dhTruck, vsPreview } from '@deephaven/icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { DashboardPlugin, PluginType, type WidgetPlugin } from './PluginTypes';
import { pluginSupportsType, getIconForPlugin } from './PluginUtils';

function TestWidget() {
return <div>TestWidget</div>;
}

const widgetPlugin: WidgetPlugin = {
name: 'test-widget-plugin',
type: PluginType.WIDGET_PLUGIN,
component: TestWidget,
supportedTypes: ['test-widget', 'test-widget-two'],
};

const dashboardPlugin: DashboardPlugin = {
name: 'test-widget-plugin',
type: PluginType.DASHBOARD_PLUGIN,
component: TestWidget,
};

test('pluginSupportsType', () => {
expect(pluginSupportsType(widgetPlugin, 'test-widget')).toBe(true);
expect(pluginSupportsType(widgetPlugin, 'test-widget-two')).toBe(true);
expect(pluginSupportsType(widgetPlugin, 'test-widget-three')).toBe(false);
expect(pluginSupportsType(dashboardPlugin, 'test-widget')).toBe(false);
expect(pluginSupportsType(undefined, 'test-widget')).toBe(false);
});

const DEFAULT_ICON = <FontAwesomeIcon icon={vsPreview} />;

describe('getIconForPlugin', () => {
test('default icon', () => {
expect(getIconForPlugin(widgetPlugin)).toEqual(DEFAULT_ICON);
});

test('default icon for non-widget plugin', () => {
expect(getIconForPlugin(dashboardPlugin)).toEqual(DEFAULT_ICON);
});

test('custom icon', () => {
const customIcon = <FontAwesomeIcon icon={dhTruck} />;
const customWidgetPlugin: WidgetPlugin = {
...widgetPlugin,
icon: dhTruck,
};
expect(getIconForPlugin(customWidgetPlugin)).toEqual(customIcon);
});

test('custom icon element', () => {
const customIcon = <div>Test</div>;
const customWidgetPlugin: WidgetPlugin = {
...widgetPlugin,
icon: customIcon,
};
expect(getIconForPlugin(customWidgetPlugin)).toEqual(customIcon);
});
});
10 changes: 3 additions & 7 deletions packages/plugin/src/PluginUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ export function pluginSupportsType(
return [plugin.supportedTypes].flat().some(t => t === type);
}

export function getIconForType(
plugin: PluginModule | undefined,
type: string
): React.ReactElement {
export function getIconForPlugin(plugin: PluginModule): React.ReactElement {
const defaultIcon = <FontAwesomeIcon icon={vsPreview} />;
if (plugin == null || !isWidgetPlugin(plugin)) {
if (!isWidgetPlugin(plugin)) {
return defaultIcon;
}

const supportsType = pluginSupportsType(plugin, type);
const { icon } = plugin;

if (!supportsType || icon == null) {
if (icon == null) {
return defaultIcon;
}

Expand Down

0 comments on commit 80116e2

Please sign in to comment.