From 73cc2663389f2de9cda354d2fbe36aaeff335ae7 Mon Sep 17 00:00:00 2001 From: Larry Gregory Date: Mon, 3 Feb 2020 11:39:56 -0500 Subject: [PATCH] additional testing --- .../security/common/licensing/index.ts | 2 + x-pack/plugins/security/public/index.ts | 2 +- x-pack/plugins/security/public/mocks.ts | 2 + .../management/spaces_management_app.test.tsx | 137 ++++++++++++++++++ .../management/spaces_management_app.tsx | 2 +- 5 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 x-pack/plugins/spaces/public/management/spaces_management_app.test.tsx diff --git a/x-pack/plugins/security/common/licensing/index.ts b/x-pack/plugins/security/common/licensing/index.ts index 9ddbe86167367..e8efae3dc6a6b 100644 --- a/x-pack/plugins/security/common/licensing/index.ts +++ b/x-pack/plugins/security/common/licensing/index.ts @@ -5,3 +5,5 @@ */ export { SecurityLicenseService, SecurityLicense } from './license_service'; + +export { SecurityLicenseFeatures } from './license_features'; diff --git a/x-pack/plugins/security/public/index.ts b/x-pack/plugins/security/public/index.ts index 58a802341882d..712f49afd306e 100644 --- a/x-pack/plugins/security/public/index.ts +++ b/x-pack/plugins/security/public/index.ts @@ -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 = () => new SecurityPlugin(); diff --git a/x-pack/plugins/security/public/mocks.ts b/x-pack/plugins/security/public/mocks.ts index 3c0c59d10abd1..33c1d1446afba 100644 --- a/x-pack/plugins/security/public/mocks.ts +++ b/x-pack/plugins/security/public/mocks.ts @@ -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(), }; } diff --git a/x-pack/plugins/spaces/public/management/spaces_management_app.test.tsx b/x-pack/plugins/spaces/public/management/spaces_management_app.test.tsx new file mode 100644 index 0000000000000..b19ef995283da --- /dev/null +++ b/x-pack/plugins/spaces/public/management/spaces_management_app.test.tsx @@ -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(` +
+ Spaces Page: {"capabilities":{"catalogue":{},"management":{},"navLinks":{}},"http":{"basePath":{"basePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"spacesManager":{"onActiveSpaceChange$":{"_isScalar":false}},"securityEnabled":true} +
+ `); + + unmount(); + + expect(container).toMatchInlineSnapshot(`
`); + }); + + 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(` +
+ Spaces Edit Page: {"capabilities":{"catalogue":{},"management":{},"navLinks":{}},"http":{"basePath":{"basePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"spacesManager":{"onActiveSpaceChange$":{"_isScalar":false}},"securityEnabled":true} +
+ `); + + unmount(); + + expect(container).toMatchInlineSnapshot(`
`); + }); + + 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(` +
+ Spaces Edit Page: {"capabilities":{"catalogue":{},"management":{},"navLinks":{}},"http":{"basePath":{"basePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"spacesManager":{"onActiveSpaceChange$":{"_isScalar":false}},"spaceId":"some-space","securityEnabled":true} +
+ `); + + unmount(); + + expect(container).toMatchInlineSnapshot(`
`); + }); +}); diff --git a/x-pack/plugins/spaces/public/management/spaces_management_app.tsx b/x-pack/plugins/spaces/public/management/spaces_management_app.tsx index 4f9654f76b312..a524d7f9829de 100644 --- a/x-pack/plugins/spaces/public/management/spaces_management_app.tsx +++ b/x-pack/plugins/spaces/public/management/spaces_management_app.tsx @@ -113,7 +113,7 @@ export const spacesManagementApp = Object.freeze({ - +