|
|
|
@ -1,9 +1,9 @@ |
|
|
|
|
import { render, screen, act } from '@testing-library/react'; |
|
|
|
|
// import { render } from 'test/test-utils';
|
|
|
|
|
|
|
|
|
|
import { store } from '@grafana/data'; |
|
|
|
|
import { config } from '@grafana/runtime'; |
|
|
|
|
import { store, EventBusSrv, EventBus } from '@grafana/data'; |
|
|
|
|
import { config, getAppEvents, setAppEvents } from '@grafana/runtime'; |
|
|
|
|
import { getExtensionPointPluginMeta } from 'app/features/plugins/extensions/utils'; |
|
|
|
|
import { OpenExtensionSidebarEvent } from 'app/types/events'; |
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
ExtensionSidebarContextProvider, |
|
|
|
@ -52,10 +52,31 @@ const mockPluginMeta = { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
describe('ExtensionSidebarProvider', () => { |
|
|
|
|
let subscribeSpy: jest.SpyInstance; |
|
|
|
|
let originalAppEvents: EventBus; |
|
|
|
|
let mockEventBus: EventBusSrv; |
|
|
|
|
|
|
|
|
|
beforeEach(() => { |
|
|
|
|
jest.clearAllMocks(); |
|
|
|
|
|
|
|
|
|
originalAppEvents = getAppEvents(); |
|
|
|
|
|
|
|
|
|
mockEventBus = new EventBusSrv(); |
|
|
|
|
subscribeSpy = jest.spyOn(mockEventBus, 'subscribe'); |
|
|
|
|
|
|
|
|
|
setAppEvents(mockEventBus); |
|
|
|
|
|
|
|
|
|
(getExtensionPointPluginMeta as jest.Mock).mockReturnValue(new Map([[mockPluginMeta.pluginId, mockPluginMeta]])); |
|
|
|
|
|
|
|
|
|
jest.replaceProperty(config.featureToggles, 'extensionSidebar', true); |
|
|
|
|
|
|
|
|
|
(store.get as jest.Mock).mockReturnValue(undefined); |
|
|
|
|
(store.set as jest.Mock).mockImplementation(() => {}); |
|
|
|
|
(store.delete as jest.Mock).mockImplementation(() => {}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
afterEach(() => { |
|
|
|
|
setAppEvents(originalAppEvents); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const TestComponent = () => { |
|
|
|
@ -199,6 +220,114 @@ describe('ExtensionSidebarProvider', () => { |
|
|
|
|
expect(screen.getByTestId('available-components-size')).toHaveTextContent('1'); |
|
|
|
|
expect(screen.getByTestId('plugin-ids')).toHaveTextContent(permittedPluginMeta.pluginId); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should subscribe to OpenExtensionSidebarEvent when feature is enabled', async () => { |
|
|
|
|
render( |
|
|
|
|
<ExtensionSidebarContextProvider> |
|
|
|
|
<TestComponent /> |
|
|
|
|
</ExtensionSidebarContextProvider> |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
expect(subscribeSpy).toHaveBeenCalledWith(OpenExtensionSidebarEvent, expect.any(Function)); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should not subscribe to OpenExtensionSidebarEvent when feature is disabled', () => { |
|
|
|
|
jest.replaceProperty(config.featureToggles, 'extensionSidebar', false); |
|
|
|
|
|
|
|
|
|
render( |
|
|
|
|
<ExtensionSidebarContextProvider> |
|
|
|
|
<TestComponent /> |
|
|
|
|
</ExtensionSidebarContextProvider> |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
expect(subscribeSpy).not.toHaveBeenCalled(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should set dockedComponentId and props when receiving a valid OpenExtensionSidebarEvent', () => { |
|
|
|
|
const TestComponentWithProps = () => { |
|
|
|
|
const context = useExtensionSidebarContext(); |
|
|
|
|
return ( |
|
|
|
|
<div> |
|
|
|
|
<div data-testid="is-open">{context.isOpen.toString()}</div> |
|
|
|
|
<div data-testid="docked-component-id">{context.dockedComponentId || 'undefined'}</div> |
|
|
|
|
<div data-testid="props">{context.props ? JSON.stringify(context.props) : 'undefined'}</div> |
|
|
|
|
</div> |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
render( |
|
|
|
|
<ExtensionSidebarContextProvider> |
|
|
|
|
<TestComponentWithProps /> |
|
|
|
|
</ExtensionSidebarContextProvider> |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
expect(screen.getByTestId('is-open')).toHaveTextContent('false'); |
|
|
|
|
expect(screen.getByTestId('props')).toHaveTextContent('undefined'); |
|
|
|
|
|
|
|
|
|
expect(subscribeSpy).toHaveBeenCalledWith(OpenExtensionSidebarEvent, expect.any(Function)); |
|
|
|
|
act(() => { |
|
|
|
|
// Get the event subscriber function
|
|
|
|
|
const [[, subscriberFn]] = subscribeSpy.mock.calls; |
|
|
|
|
|
|
|
|
|
// Call it directly with the test event
|
|
|
|
|
subscriberFn( |
|
|
|
|
new OpenExtensionSidebarEvent({ |
|
|
|
|
pluginId: 'grafana-investigations-app', |
|
|
|
|
componentTitle: 'Test Component', |
|
|
|
|
props: { testProp: 'test value' }, |
|
|
|
|
}) |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
expect(screen.getByTestId('is-open')).toHaveTextContent('true'); |
|
|
|
|
expect(screen.getByTestId('props')).toHaveTextContent('{"testProp":"test value"}'); |
|
|
|
|
const expectedComponentId = JSON.stringify({ |
|
|
|
|
pluginId: 'grafana-investigations-app', |
|
|
|
|
componentTitle: 'Test Component', |
|
|
|
|
}); |
|
|
|
|
expect(screen.getByTestId('docked-component-id')).toHaveTextContent(expectedComponentId); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should not open sidebar when receiving an OpenExtensionSidebarEvent with non-permitted plugin', () => { |
|
|
|
|
render( |
|
|
|
|
<ExtensionSidebarContextProvider> |
|
|
|
|
<TestComponent /> |
|
|
|
|
</ExtensionSidebarContextProvider> |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
expect(screen.getByTestId('is-open')).toHaveTextContent('false'); |
|
|
|
|
|
|
|
|
|
act(() => { |
|
|
|
|
// Get the event subscriber function
|
|
|
|
|
const [[, subscriberFn]] = subscribeSpy.mock.calls; |
|
|
|
|
|
|
|
|
|
// Call it directly with the test event for a non-permitted plugin
|
|
|
|
|
subscriberFn( |
|
|
|
|
new OpenExtensionSidebarEvent({ |
|
|
|
|
pluginId: 'non-permitted-plugin', |
|
|
|
|
componentTitle: 'Test Component', |
|
|
|
|
}) |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
expect(screen.getByTestId('is-open')).toHaveTextContent('false'); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should unsubscribe from OpenExtensionSidebarEvent on unmount', () => { |
|
|
|
|
const unsubscribeMock = jest.fn(); |
|
|
|
|
subscribeSpy.mockReturnValue({ |
|
|
|
|
unsubscribe: unsubscribeMock, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const { unmount } = render( |
|
|
|
|
<ExtensionSidebarContextProvider> |
|
|
|
|
<TestComponent /> |
|
|
|
|
</ExtensionSidebarContextProvider> |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
unmount(); |
|
|
|
|
expect(unsubscribeMock).toHaveBeenCalled(); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
describe('Utility Functions', () => { |
|
|
|
|