diff --git a/packages/core/plugins/core-plugins-browser-mocks/src/plugins_service.mock.ts b/packages/core/plugins/core-plugins-browser-mocks/src/plugins_service.mock.ts index 56ea09aed483c..8342476961267 100644 --- a/packages/core/plugins/core-plugins-browser-mocks/src/plugins_service.mock.ts +++ b/packages/core/plugins/core-plugins-browser-mocks/src/plugins_service.mock.ts @@ -10,6 +10,7 @@ import type { PublicMethodsOf } from '@kbn/utility-types'; import { loggerMock } from '@kbn/logging-mocks'; import type { PluginInitializerContext } from '@kbn/core-plugins-browser'; import type { PluginsService, PluginsServiceSetup } from '@kbn/core-plugins-browser-internal'; +import type { BuildFlavor } from '@kbn/config/src/types'; const createSetupContractMock = () => { const setupContract: jest.Mocked = { @@ -27,7 +28,10 @@ const createStartContractMock = () => { return startContract as PluginsServiceSetup; }; -const createPluginInitializerContextMock = (config: unknown = {}) => { +const createPluginInitializerContextMock = ( + config: unknown = {}, + { buildFlavor = 'serverless' }: { buildFlavor?: BuildFlavor } = {} +) => { const mock: PluginInitializerContext = { opaqueId: Symbol(), env: { @@ -43,7 +47,7 @@ const createPluginInitializerContextMock = (config: unknown = {}) => { buildSha: 'buildSha', dist: false, buildDate: new Date('2023-05-15T23:12:09.000Z'), - buildFlavor: 'serverless', + buildFlavor, }, }, logger: loggerMock.create(), diff --git a/packages/core/plugins/core-plugins-browser-mocks/tsconfig.json b/packages/core/plugins/core-plugins-browser-mocks/tsconfig.json index 6b14fa13dd8b5..6413be0063abe 100644 --- a/packages/core/plugins/core-plugins-browser-mocks/tsconfig.json +++ b/packages/core/plugins/core-plugins-browser-mocks/tsconfig.json @@ -16,6 +16,7 @@ "@kbn/logging-mocks", "@kbn/core-plugins-browser-internal", "@kbn/core-plugins-browser", + "@kbn/config", ], "exclude": [ "target/**/*", diff --git a/x-pack/plugins/spaces/public/plugin.test.ts b/x-pack/plugins/spaces/public/plugin.test.ts index 91e1a21752959..2e81e6c039668 100644 --- a/x-pack/plugins/spaces/public/plugin.test.ts +++ b/x-pack/plugins/spaces/public/plugin.test.ts @@ -17,10 +17,14 @@ import { SpacesPlugin } from './plugin'; describe('Spaces plugin', () => { describe('#setup', () => { - it('should register the spaces API and the space selector app', () => { + it('should register the space selector app when buildFlavor is traditional', () => { const coreSetup = coreMock.createSetup(); + const mockInitializerContext = coreMock.createPluginInitializerContext( + {}, + { buildFlavor: 'traditional' } + ); - const plugin = new SpacesPlugin(coreMock.createPluginInitializerContext()); + const plugin = new SpacesPlugin(mockInitializerContext); plugin.setup(coreSetup, {}); expect(coreSetup.application.register).toHaveBeenCalledWith( @@ -33,7 +37,23 @@ describe('Spaces plugin', () => { ); }); - it('should register the management and feature catalogue sections when the management and home plugins are both available', () => { + it('should not register the space selector app when buildFlavor is serverless', () => { + const coreSetup = coreMock.createSetup(); + + const plugin = new SpacesPlugin(coreMock.createPluginInitializerContext()); + plugin.setup(coreSetup, {}); + + expect(coreSetup.application.register).not.toHaveBeenCalledWith( + expect.objectContaining({ + id: 'space_selector', + chromeless: true, + appRoute: '/spaces/space_selector', + mount: expect.any(Function), + }) + ); + }); + + it('should register the management and feature catalogue sections when the management and home plugins are both available when buildFlavor is traditional', () => { const coreSetup = coreMock.createSetup(); const home = homePluginMock.createSetupContract(); @@ -43,7 +63,12 @@ describe('Spaces plugin', () => { management.sections.section.kibana = mockSection; - const plugin = new SpacesPlugin(coreMock.createPluginInitializerContext()); + const mockInitializerContext = coreMock.createPluginInitializerContext( + {}, + { buildFlavor: 'traditional' } + ); + + const plugin = new SpacesPlugin(mockInitializerContext); plugin.setup(coreSetup, { management, home, @@ -62,19 +87,66 @@ describe('Spaces plugin', () => { }) ); }); + + it('should not register spaces in the management plugin or the feature catalog when the management and home plugins are both available when buildFlavor is serverless', () => { + const coreSetup = coreMock.createSetup(); + const home = homePluginMock.createSetupContract(); + + const management = managementPluginMock.createSetupContract(); + const mockSection = createManagementSectionMock(); + mockSection.registerApp = jest.fn(); + + management.sections.section.kibana = mockSection; + + const plugin = new SpacesPlugin(coreMock.createPluginInitializerContext()); + plugin.setup(coreSetup, { + management, + home, + }); + + expect(mockSection.registerApp).not.toHaveBeenCalledWith( + expect.objectContaining({ id: 'spaces' }) + ); + + expect(home.featureCatalogue.register).not.toHaveBeenCalledWith( + expect.objectContaining({ + category: 'admin', + icon: 'spacesApp', + id: 'spaces', + showOnHomePage: false, + }) + ); + }); }); describe('#start', () => { - it('should register the spaces nav control', () => { + it('should register the spaces nav control when buildFlavor is traditional', () => { const coreSetup = coreMock.createSetup(); const coreStart = coreMock.createStart(); - const plugin = new SpacesPlugin(coreMock.createPluginInitializerContext()); + const mockInitializerContext = coreMock.createPluginInitializerContext( + {}, + { buildFlavor: 'traditional' } + ); + + const plugin = new SpacesPlugin(mockInitializerContext); plugin.setup(coreSetup, {}); plugin.start(coreStart); expect(coreStart.chrome.navControls.registerLeft).toHaveBeenCalled(); }); + + it('should not register the spaces nav control when buildFlavor is serverless', () => { + const coreSetup = coreMock.createSetup(); + const coreStart = coreMock.createStart(); + + const plugin = new SpacesPlugin(coreMock.createPluginInitializerContext()); + plugin.setup(coreSetup, {}); + + plugin.start(coreStart); + + expect(coreStart.chrome.navControls.registerLeft).not.toHaveBeenCalled(); + }); }); }); diff --git a/x-pack/plugins/spaces/public/plugin.tsx b/x-pack/plugins/spaces/public/plugin.tsx index 33cbcc3a47227..28a54e7768f3e 100644 --- a/x-pack/plugins/spaces/public/plugin.tsx +++ b/x-pack/plugins/spaces/public/plugin.tsx @@ -45,9 +45,11 @@ export class SpacesPlugin implements Plugin(); + this.isServerless = this.initializerContext.env.packageInfo.buildFlavor === 'serverless'; } public setup(core: CoreSetup, plugins: PluginsSetup) { @@ -61,31 +63,35 @@ export class SpacesPlugin implements Plugin this.spacesManager.getActiveSpace(), }; - if (plugins.home) { - plugins.home.featureCatalogue.register(createSpacesFeatureCatalogueEntry()); - } - - if (plugins.management) { - this.managementService = new ManagementService(); - this.managementService.setup({ - management: plugins.management, + if (!this.isServerless) { + if (plugins.home) { + plugins.home.featureCatalogue.register(createSpacesFeatureCatalogueEntry()); + } + + if (plugins.management) { + this.managementService = new ManagementService(); + this.managementService.setup({ + management: plugins.management, + getStartServices: core.getStartServices, + spacesManager: this.spacesManager, + config: this.config, + }); + } + + spaceSelectorApp.create({ getStartServices: core.getStartServices, + application: core.application, spacesManager: this.spacesManager, - config: this.config, }); } - spaceSelectorApp.create({ - getStartServices: core.getStartServices, - application: core.application, - spacesManager: this.spacesManager, - }); - return {}; } public start(core: CoreStart) { - initSpacesNavControl(this.spacesManager, core); + if (!this.isServerless) { + initSpacesNavControl(this.spacesManager, core); + } return this.spacesApi; }