Skip to content

Commit

Permalink
additional testing
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego committed Feb 3, 2020
1 parent 95d390e commit 73cc266
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 2 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/security/common/licensing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
*/

export { SecurityLicenseService, SecurityLicense } from './license_service';

export { SecurityLicenseFeatures } from './license_features';
2 changes: 1 addition & 1 deletion x-pack/plugins/security/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SecurityPlugin, SecurityPluginSetup, SecurityPluginStart } from './plug
export { SecurityPluginSetup, SecurityPluginStart };
export { SessionInfo } from './types';
export { AuthenticatedUser } from '../common/model';
export { SecurityLicense } from '../common/licensing';
export { SecurityLicense, SecurityLicenseFeatures } from '../common/licensing';

export const plugin: PluginInitializer<SecurityPluginSetup, SecurityPluginStart> = () =>
new SecurityPlugin();
2 changes: 2 additions & 0 deletions x-pack/plugins/security/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

import { authenticationMock } from './authentication/index.mock';
import { createSessionTimeoutMock } from './session/session_timeout.mock';
import { licenseMock } from '../common/licensing/index.mock';

function createSetupMock() {
return {
authc: authenticationMock.createSetup(),
sessionTimeout: createSessionTimeoutMock(),
license: licenseMock.create(),
};
}

Expand Down
137 changes: 137 additions & 0 deletions x-pack/plugins/spaces/public/management/spaces_management_app.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* 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.
*/

jest.mock('./spaces_grid', () => ({
SpacesGridPage: (props: any) => `Spaces Page: ${JSON.stringify(props)}`,
}));

jest.mock('./edit_space', () => ({
ManageSpacePage: (props: any) => {
if (props.spacesManager && props.onLoadSpace) {
props.spacesManager.getSpace().then((space: any) => props.onLoadSpace(space));
}
return `Spaces Edit Page: ${JSON.stringify(props)}`;
},
}));

import { spacesManagementApp } from './spaces_management_app';

import { coreMock } from '../../../../../src/core/public/mocks';
import { securityMock } from '../../../security/public/mocks';
import { spacesManagerMock } from '../spaces_manager/mocks';
import { SecurityLicenseFeatures } from '../../../security/public';

async function mountApp(basePath: string, spaceId?: string) {
const container = document.createElement('div');
const setBreadcrumbs = jest.fn();

const spacesManager = spacesManagerMock.create();
if (spaceId) {
spacesManager.getSpace.mockResolvedValue({
id: spaceId,
name: `space with id ${spaceId}`,
disabledFeatures: [],
});
}

const securityLicense = securityMock.createSetup().license;
securityLicense.getFeatures.mockReturnValue({
showLinks: true,
} as SecurityLicenseFeatures);

const unmount = await spacesManagementApp
.create({
spacesManager,
securityLicense,
getStartServices: coreMock.createSetup().getStartServices as any,
})
.mount({ basePath, element: container, setBreadcrumbs });

return { unmount, container, setBreadcrumbs };
}

describe('spacesManagementApp', () => {
it('create() returns proper management app descriptor', () => {
expect(
spacesManagementApp.create({
spacesManager: spacesManagerMock.create(),
securityLicense: securityMock.createSetup().license,
getStartServices: coreMock.createSetup().getStartServices as any,
})
).toMatchInlineSnapshot(`
Object {
"id": "spaces",
"mount": [Function],
"order": 10,
"title": "Spaces",
}
`);
});

it('mount() works for the `grid` page', async () => {
const basePath = '/some-base-path/spaces';
window.location.hash = basePath;

const { setBreadcrumbs, container, unmount } = await mountApp(basePath);

expect(setBreadcrumbs).toHaveBeenCalledTimes(1);
expect(setBreadcrumbs).toHaveBeenCalledWith([{ href: `#${basePath}`, text: 'Spaces' }]);
expect(container).toMatchInlineSnapshot(`
<div>
Spaces Page: {"capabilities":{"catalogue":{},"management":{},"navLinks":{}},"http":{"basePath":{"basePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"spacesManager":{"onActiveSpaceChange$":{"_isScalar":false}},"securityEnabled":true}
</div>
`);

unmount();

expect(container).toMatchInlineSnapshot(`<div />`);
});

it('mount() works for the `create space` page', async () => {
const basePath = '/some-base-path/spaces';
window.location.hash = `${basePath}/create`;

const { setBreadcrumbs, container, unmount } = await mountApp(basePath);

expect(setBreadcrumbs).toHaveBeenCalledTimes(1);
expect(setBreadcrumbs).toHaveBeenCalledWith([
{ href: `#${basePath}`, text: 'Spaces' },
{ text: 'Create' },
]);
expect(container).toMatchInlineSnapshot(`
<div>
Spaces Edit Page: {"capabilities":{"catalogue":{},"management":{},"navLinks":{}},"http":{"basePath":{"basePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"spacesManager":{"onActiveSpaceChange$":{"_isScalar":false}},"securityEnabled":true}
</div>
`);

unmount();

expect(container).toMatchInlineSnapshot(`<div />`);
});

it('mount() works for the `edit space` page', async () => {
const basePath = '/some-base-path/spaces';
const spaceId = 'some-space';
window.location.hash = `${basePath}/edit/${spaceId}`;

const { setBreadcrumbs, container, unmount } = await mountApp(basePath, spaceId);

expect(setBreadcrumbs).toHaveBeenCalledTimes(1);
expect(setBreadcrumbs).toHaveBeenCalledWith([
{ href: `#${basePath}`, text: 'Spaces' },
{ href: `#/some-base-path/spaces/edit/${spaceId}`, text: `space with id some-space` },
]);
expect(container).toMatchInlineSnapshot(`
<div>
Spaces Edit Page: {"capabilities":{"catalogue":{},"management":{},"navLinks":{}},"http":{"basePath":{"basePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"spacesManager":{"onActiveSpaceChange$":{"_isScalar":false}},"spaceId":"some-space","securityEnabled":true}
</div>
`);

unmount();

expect(container).toMatchInlineSnapshot(`<div />`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const spacesManagementApp = Object.freeze({
<Route path="/create">
<CreateSpacePageWithBreadcrumbs />
</Route>
<Route path="/edit/:spaceId?">
<Route path="/edit/:spaceId">
<EditSpacePageWithBreadcrumbs />
</Route>
</Switch>
Expand Down

0 comments on commit 73cc266

Please sign in to comment.