Skip to content

Commit

Permalink
additional testing
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego committed Jan 31, 2020
1 parent 602b11e commit 03a122f
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 4 deletions.
36 changes: 36 additions & 0 deletions src/plugins/home/public/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { featureCatalogueRegistryMock } from '../services/feature_catalogue/feature_catalogue_registry.mock';
import { environmentServiceMock } from '../services/environment/environment.mock';

const createSetupContract = () => ({
featureCatalogue: featureCatalogueRegistryMock.createSetup(),
environment: environmentServiceMock.createSetup(),
});

const createStartContract = () => ({
featureCatalogue: featureCatalogueRegistryMock.createStart(),
environment: environmentServiceMock.createStart(),
});

export const homePluginMock = {
createSetupContract,
createStartContract,
};
63 changes: 63 additions & 0 deletions x-pack/plugins/spaces/public/management/management_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { coreMock } from 'src/core/public/mocks';
import { spacesManagerMock } from '../spaces_manager/mocks';
import { managementPluginMock } from '../../../../../src/plugins/management/public/mocks';
import { ManagementSection } from 'src/plugins/management/public';
import { Capabilities } from 'kibana/public';

describe('ManagementService', () => {
describe('#setup', () => {
Expand Down Expand Up @@ -54,6 +55,68 @@ describe('ManagementService', () => {
});
});

describe('#start', () => {
it('disables the spaces management page if the user is not authorized', () => {
const mockSpacesManagementPage = { disable: jest.fn() };
const mockKibanaSection = ({
registerApp: jest.fn(),
getApp: jest
.fn()
.mockImplementation(id => (id === 'spaces' ? mockSpacesManagementPage : undefined)),
} as unknown) as ManagementSection;

const deps = {
management: managementPluginMock.createSetupContract(),
getStartServices: coreMock.createSetup().getStartServices,
spacesManager: spacesManagerMock.create(),
};

deps.management.sections.getSection.mockImplementation(id => {
if (id === 'kibana') return mockKibanaSection;
throw new Error(`unexpected getSection call: ${id}`);
});

const service = new ManagementService();
service.setup(deps);

const capabilities = ({ spaces: { manage: false } } as unknown) as Capabilities;
service.start({ capabilities });

expect(mockKibanaSection.registerApp).toHaveBeenCalledTimes(1);
expect(mockSpacesManagementPage.disable).toHaveBeenCalledTimes(1);
});

it('does not disable the spaces management page if the user is authorized', () => {
const mockSpacesManagementPage = { disable: jest.fn() };
const mockKibanaSection = ({
registerApp: jest.fn(),
getApp: jest
.fn()
.mockImplementation(id => (id === 'spaces' ? mockSpacesManagementPage : undefined)),
} as unknown) as ManagementSection;

const deps = {
management: managementPluginMock.createSetupContract(),
getStartServices: coreMock.createSetup().getStartServices,
spacesManager: spacesManagerMock.create(),
};

deps.management.sections.getSection.mockImplementation(id => {
if (id === 'kibana') return mockKibanaSection;
throw new Error(`unexpected getSection call: ${id}`);
});

const service = new ManagementService();
service.setup(deps);

const capabilities = ({ spaces: { manage: true } } as unknown) as Capabilities;
service.start({ capabilities });

expect(mockKibanaSection.registerApp).toHaveBeenCalledTimes(1);
expect(mockSpacesManagementPage.disable).toHaveBeenCalledTimes(0);
});
});

describe('#stop', () => {
it('disables the spaces management page', () => {
const mockSpacesManagementPage = { disable: jest.fn() };
Expand Down
72 changes: 72 additions & 0 deletions x-pack/plugins/spaces/public/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { coreMock } from 'src/core/public/mocks';
import { SpacesPlugin } from './plugin';
import { homePluginMock } from '../../../../src/plugins/home/public/mocks';
import { managementPluginMock } from '../../../../src/plugins/management/public/mocks';

describe('Spaces plugin', () => {
describe('#setup', () => {
it('should register the space selector app', () => {
const coreSetup = coreMock.createSetup();

const plugin = new SpacesPlugin();
plugin.setup(coreSetup, {});

expect(coreSetup.application.register).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', () => {
const coreSetup = coreMock.createSetup();

const registerApp = jest.fn();

const home = homePluginMock.createSetupContract();

const management = managementPluginMock.createSetupContract();
management.sections.getSection.mockReturnValue({ registerApp });

const plugin = new SpacesPlugin();
plugin.setup(coreSetup, {
management,
home,
});

expect(registerApp).toHaveBeenCalledWith(expect.objectContaining({ id: 'spaces' }));

expect(home.featureCatalogue.register).toHaveBeenCalledWith(
expect.objectContaining({
category: 'admin',
icon: 'spacesApp',
id: 'spaces',
showOnHomePage: true,
})
);
});
});

describe('#start', () => {
it('should register the spaces nav control', () => {
const coreSetup = coreMock.createSetup();
const coreStart = coreMock.createStart();

const plugin = new SpacesPlugin();
plugin.setup(coreSetup, {});

plugin.start(coreStart, {});

expect(coreStart.chrome.navControls.registerLeft).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default function({ getPageObjects, getService }: FtrProviderContext) {

it(`doesn't display Spaces management section`, async () => {
await PageObjects.settings.navigateTo();
await testSubjects.existOrFail('objects'); // this ensures we've gotten to the management page
await testSubjects.existOrFail('management-landing'); // this ensures we've gotten to the management page
await testSubjects.missingOrFail('spaces');
});

Expand All @@ -150,7 +150,7 @@ export default function({ getPageObjects, getService }: FtrProviderContext) {
shouldLoginIfPrompted: false,
});

await testSubjects.existOrFail('homeApp');
await testSubjects.existOrFail('management-landing');
});

it(`can't navigate to create new space page`, async () => {
Expand All @@ -159,7 +159,7 @@ export default function({ getPageObjects, getService }: FtrProviderContext) {
shouldLoginIfPrompted: false,
});

await testSubjects.existOrFail('homeApp');
await testSubjects.existOrFail('management-landing');
});

it(`can't navigate to edit space page`, async () => {
Expand All @@ -172,7 +172,7 @@ export default function({ getPageObjects, getService }: FtrProviderContext) {
}
);

await testSubjects.existOrFail('homeApp');
await testSubjects.existOrFail('management-landing');
});
});
});
Expand Down

0 comments on commit 03a122f

Please sign in to comment.