2024-06-08 00:05:00 +08:00
|
|
|
import { act, render, screen } from '@testing-library/react';
|
|
|
|
import { renderHook } from '@testing-library/react-hooks';
|
|
|
|
|
2024-08-19 14:43:11 +08:00
|
|
|
import { ExposedComponentsRegistry } from './registry/ExposedComponentsRegistry';
|
2024-06-08 00:05:00 +08:00
|
|
|
import { createUsePluginComponent } from './usePluginComponent';
|
|
|
|
|
|
|
|
jest.mock('app/features/plugins/pluginSettings', () => ({
|
|
|
|
getPluginSettings: jest.fn().mockResolvedValue({
|
|
|
|
id: 'my-app-plugin',
|
|
|
|
enabled: true,
|
|
|
|
jsonData: {},
|
|
|
|
type: 'panel',
|
|
|
|
name: 'My App Plugin',
|
|
|
|
module: 'app/plugins/my-app-plugin/module',
|
|
|
|
}),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('usePluginComponent()', () => {
|
2024-08-19 14:43:11 +08:00
|
|
|
let registry: ExposedComponentsRegistry;
|
2024-06-08 00:05:00 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2024-08-19 14:43:11 +08:00
|
|
|
registry = new ExposedComponentsRegistry();
|
2024-06-08 00:05:00 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return null if there are no component exposed for the id', () => {
|
2024-08-19 14:43:11 +08:00
|
|
|
const usePluginComponent = createUsePluginComponent(registry);
|
2024-06-08 00:05:00 +08:00
|
|
|
const { result } = renderHook(() => usePluginComponent('foo/bar'));
|
|
|
|
|
|
|
|
expect(result.current.component).toEqual(null);
|
|
|
|
expect(result.current.isLoading).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return component, that can be rendered, from the registry', async () => {
|
2024-08-19 14:43:11 +08:00
|
|
|
const id = 'my-app-plugin/foo/bar/v1';
|
2024-06-08 00:05:00 +08:00
|
|
|
const pluginId = 'my-app-plugin';
|
|
|
|
|
2024-08-19 14:43:11 +08:00
|
|
|
registry.register({
|
2024-06-08 00:05:00 +08:00
|
|
|
pluginId,
|
2024-08-19 14:43:11 +08:00
|
|
|
configs: [{ id, title: 'not important', description: 'not important', component: () => <div>Hello World</div> }],
|
2024-06-08 00:05:00 +08:00
|
|
|
});
|
|
|
|
|
2024-08-19 14:43:11 +08:00
|
|
|
const usePluginComponent = createUsePluginComponent(registry);
|
2024-06-08 00:05:00 +08:00
|
|
|
const { result } = renderHook(() => usePluginComponent(id));
|
|
|
|
const Component = result.current.component;
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
render(Component && <Component />);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(result.current.isLoading).toEqual(false);
|
|
|
|
expect(result.current.component).not.toBeNull();
|
|
|
|
expect(await screen.findByText('Hello World')).toBeVisible();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should dynamically update when component is registered to the registry', async () => {
|
2024-08-19 14:43:11 +08:00
|
|
|
const id = 'my-app-plugin/foo/bar/v1';
|
2024-06-08 00:05:00 +08:00
|
|
|
const pluginId = 'my-app-plugin';
|
2024-08-19 14:43:11 +08:00
|
|
|
const usePluginComponent = createUsePluginComponent(registry);
|
2024-06-08 00:05:00 +08:00
|
|
|
const { result, rerender } = renderHook(() => usePluginComponent(id));
|
|
|
|
|
|
|
|
// No extensions yet
|
|
|
|
expect(result.current.component).toBeNull();
|
|
|
|
expect(result.current.isLoading).toEqual(false);
|
|
|
|
|
|
|
|
// Add extensions to the registry
|
|
|
|
act(() => {
|
2024-08-19 14:43:11 +08:00
|
|
|
registry.register({
|
2024-06-08 00:05:00 +08:00
|
|
|
pluginId,
|
2024-08-19 14:43:11 +08:00
|
|
|
configs: [
|
2024-06-08 00:05:00 +08:00
|
|
|
{
|
2024-08-19 14:43:11 +08:00
|
|
|
id,
|
2024-06-08 00:05:00 +08:00
|
|
|
title: 'not important',
|
|
|
|
description: 'not important',
|
|
|
|
component: () => <div>Hello World</div>,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check if the hook returns the new extensions
|
|
|
|
rerender();
|
|
|
|
|
|
|
|
const Component = result.current.component;
|
|
|
|
expect(result.current.isLoading).toEqual(false);
|
|
|
|
expect(result.current.component).not.toBeNull();
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
render(Component && <Component />);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(await screen.findByText('Hello World')).toBeVisible();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should only render the hook once', () => {
|
2024-08-19 14:43:11 +08:00
|
|
|
const spy = jest.spyOn(registry, 'asObservable');
|
2024-06-08 00:05:00 +08:00
|
|
|
const id = 'my-app-plugin/foo/bar';
|
2024-08-19 14:43:11 +08:00
|
|
|
const usePluginComponent = createUsePluginComponent(registry);
|
2024-06-08 00:05:00 +08:00
|
|
|
|
|
|
|
renderHook(() => usePluginComponent(id));
|
|
|
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|