From 83dc1250bb5f157188abd247310847d05fcc90be Mon Sep 17 00:00:00 2001 From: Levente Balogh Date: Thu, 3 Apr 2025 16:13:32 +0200 Subject: [PATCH] Extensions: Fix a bug with the observable APIs (#103367) tests(getPluginExtensions): add more tests for the observable apis --- .../extensions/getPluginExtensions.test.tsx | 91 ++++++++++++++++++- .../plugins/extensions/getPluginExtensions.ts | 6 +- 2 files changed, 92 insertions(+), 5 deletions(-) diff --git a/public/app/features/plugins/extensions/getPluginExtensions.test.tsx b/public/app/features/plugins/extensions/getPluginExtensions.test.tsx index b32f8f93320..e4f2ea1d762 100644 --- a/public/app/features/plugins/extensions/getPluginExtensions.test.tsx +++ b/public/app/features/plugins/extensions/getPluginExtensions.test.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { first, take } from 'rxjs'; +import { first, firstValueFrom, take } from 'rxjs'; import { type PluginExtensionAddedLinkConfig, @@ -700,6 +700,50 @@ describe('getObservablePluginLinks()', () => { expect(links[0].type).toBe(PluginExtensionTypes.link); }); }); + + it('should be possible to get the last value from the observable', async () => { + const observable = getObservablePluginLinks({ extensionPointId }); + const links = await firstValueFrom(observable); + + expect(links).toHaveLength(1); + expect(links[0].pluginId).toBe(pluginId); + expect(links[0].type).toBe(PluginExtensionTypes.link); + }); + + it('should be possible to receive the last state of the registry', async () => { + // Register a new link + pluginExtensionRegistries.addedLinksRegistry.register({ + pluginId, + configs: [ + { + title: 'Link 2', + description: 'Link 2 description', + path: `/a/${pluginId}/declare-incident`, + targets: extensionPointId, + configure: jest.fn().mockReturnValue({}), + }, + ], + }); + + const observable = getObservablePluginLinks({ extensionPointId }); + const links = await firstValueFrom(observable); + + expect(links).toHaveLength(2); + expect(links[0].pluginId).toBe(pluginId); + expect(links[0].type).toBe(PluginExtensionTypes.link); + expect(links[1].pluginId).toBe(pluginId); + expect(links[1].type).toBe(PluginExtensionTypes.link); + }); + + it('should receive an empty array if there are no links', async () => { + pluginExtensionRegistries.addedLinksRegistry = new AddedLinksRegistry(); + pluginExtensionRegistries.addedComponentsRegistry = new AddedComponentsRegistry(); + + const observable = getObservablePluginLinks({ extensionPointId }).pipe(first()); + const links = await firstValueFrom(observable); + + expect(links).toHaveLength(0); + }); }); describe('getObservablePluginComponents()', () => { @@ -747,4 +791,49 @@ describe('getObservablePluginComponents()', () => { expect(components[0].type).toBe(PluginExtensionTypes.component); }); }); + + it('should be possible to get the last value from the observable', async () => { + const observable = getObservablePluginComponents({ extensionPointId }); + const components = await firstValueFrom(observable); + + expect(components).toHaveLength(1); + expect(components[0].pluginId).toBe(pluginId); + expect(components[0].type).toBe(PluginExtensionTypes.component); + }); + + it('should be possible to receive the last state of the registry', async () => { + // Register a new component + pluginExtensionRegistries.addedComponentsRegistry.register({ + pluginId, + configs: [ + { + title: 'Component 2', + description: 'Component 2 description', + targets: extensionPointId, + component: () => { + return
Hello world2!
; + }, + }, + ], + }); + + const observable = getObservablePluginComponents({ extensionPointId }); + const components = await firstValueFrom(observable); + + expect(components).toHaveLength(2); + expect(components[0].pluginId).toBe(pluginId); + expect(components[0].type).toBe(PluginExtensionTypes.component); + expect(components[1].pluginId).toBe(pluginId); + expect(components[1].type).toBe(PluginExtensionTypes.component); + }); + + it('should receive an empty array if there are no components', async () => { + pluginExtensionRegistries.addedLinksRegistry = new AddedLinksRegistry(); + pluginExtensionRegistries.addedComponentsRegistry = new AddedComponentsRegistry(); + + const observable = getObservablePluginComponents({ extensionPointId }).pipe(first()); + const components = await firstValueFrom(observable); + + expect(components).toHaveLength(0); + }); }); diff --git a/public/app/features/plugins/extensions/getPluginExtensions.ts b/public/app/features/plugins/extensions/getPluginExtensions.ts index 9012a3c662e..aed7866e9b2 100644 --- a/public/app/features/plugins/extensions/getPluginExtensions.ts +++ b/public/app/features/plugins/extensions/getPluginExtensions.ts @@ -54,15 +54,13 @@ export const getObservablePluginExtensions = ( export const getObservablePluginLinks: GetObservablePluginLinks = (options) => { return getObservablePluginExtensions(options).pipe( - map((value) => value.extensions.filter((extension) => extension.type === PluginExtensionTypes.link)), - filter((extensions) => extensions.length > 0) + map((value) => value.extensions.filter((extension) => extension.type === PluginExtensionTypes.link)) ); }; export const getObservablePluginComponents: GetObservablePluginComponents = (options) => { return getObservablePluginExtensions(options).pipe( - map((value) => value.extensions.filter((extension) => extension.type === PluginExtensionTypes.component)), - filter((extensions) => extensions.length > 0) + map((value) => value.extensions.filter((extension) => extension.type === PluginExtensionTypes.component)) ); };