|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +jest.mock('./spaces_grid', () => ({ |
| 8 | + SpacesGridPage: (props: any) => `Spaces Page: ${JSON.stringify(props)}`, |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('./edit_space', () => ({ |
| 12 | + ManageSpacePage: (props: any) => { |
| 13 | + if (props.spacesManager && props.onLoadSpace) { |
| 14 | + props.spacesManager.getSpace().then((space: any) => props.onLoadSpace(space)); |
| 15 | + } |
| 16 | + return `Spaces Edit Page: ${JSON.stringify(props)}`; |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +import { spacesManagementApp } from './spaces_management_app'; |
| 21 | + |
| 22 | +import { coreMock } from '../../../../../src/core/public/mocks'; |
| 23 | +import { securityMock } from '../../../security/public/mocks'; |
| 24 | +import { spacesManagerMock } from '../spaces_manager/mocks'; |
| 25 | +import { SecurityLicenseFeatures } from '../../../security/public'; |
| 26 | + |
| 27 | +async function mountApp(basePath: string, spaceId?: string) { |
| 28 | + const container = document.createElement('div'); |
| 29 | + const setBreadcrumbs = jest.fn(); |
| 30 | + |
| 31 | + const spacesManager = spacesManagerMock.create(); |
| 32 | + if (spaceId) { |
| 33 | + spacesManager.getSpace.mockResolvedValue({ |
| 34 | + id: spaceId, |
| 35 | + name: `space with id ${spaceId}`, |
| 36 | + disabledFeatures: [], |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + const securityLicense = securityMock.createSetup().license; |
| 41 | + securityLicense.getFeatures.mockReturnValue({ |
| 42 | + showLinks: true, |
| 43 | + } as SecurityLicenseFeatures); |
| 44 | + |
| 45 | + const unmount = await spacesManagementApp |
| 46 | + .create({ |
| 47 | + spacesManager, |
| 48 | + securityLicense, |
| 49 | + getStartServices: coreMock.createSetup().getStartServices as any, |
| 50 | + }) |
| 51 | + .mount({ basePath, element: container, setBreadcrumbs }); |
| 52 | + |
| 53 | + return { unmount, container, setBreadcrumbs }; |
| 54 | +} |
| 55 | + |
| 56 | +describe('spacesManagementApp', () => { |
| 57 | + it('create() returns proper management app descriptor', () => { |
| 58 | + expect( |
| 59 | + spacesManagementApp.create({ |
| 60 | + spacesManager: spacesManagerMock.create(), |
| 61 | + securityLicense: securityMock.createSetup().license, |
| 62 | + getStartServices: coreMock.createSetup().getStartServices as any, |
| 63 | + }) |
| 64 | + ).toMatchInlineSnapshot(` |
| 65 | + Object { |
| 66 | + "id": "spaces", |
| 67 | + "mount": [Function], |
| 68 | + "order": 10, |
| 69 | + "title": "Spaces", |
| 70 | + } |
| 71 | + `); |
| 72 | + }); |
| 73 | + |
| 74 | + it('mount() works for the `grid` page', async () => { |
| 75 | + const basePath = '/some-base-path/spaces'; |
| 76 | + window.location.hash = basePath; |
| 77 | + |
| 78 | + const { setBreadcrumbs, container, unmount } = await mountApp(basePath); |
| 79 | + |
| 80 | + expect(setBreadcrumbs).toHaveBeenCalledTimes(1); |
| 81 | + expect(setBreadcrumbs).toHaveBeenCalledWith([{ href: `#${basePath}`, text: 'Spaces' }]); |
| 82 | + expect(container).toMatchInlineSnapshot(` |
| 83 | + <div> |
| 84 | + Spaces Page: {"capabilities":{"catalogue":{},"management":{},"navLinks":{}},"http":{"basePath":{"basePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"spacesManager":{"onActiveSpaceChange$":{"_isScalar":false}},"securityEnabled":true} |
| 85 | + </div> |
| 86 | + `); |
| 87 | + |
| 88 | + unmount(); |
| 89 | + |
| 90 | + expect(container).toMatchInlineSnapshot(`<div />`); |
| 91 | + }); |
| 92 | + |
| 93 | + it('mount() works for the `create space` page', async () => { |
| 94 | + const basePath = '/some-base-path/spaces'; |
| 95 | + window.location.hash = `${basePath}/create`; |
| 96 | + |
| 97 | + const { setBreadcrumbs, container, unmount } = await mountApp(basePath); |
| 98 | + |
| 99 | + expect(setBreadcrumbs).toHaveBeenCalledTimes(1); |
| 100 | + expect(setBreadcrumbs).toHaveBeenCalledWith([ |
| 101 | + { href: `#${basePath}`, text: 'Spaces' }, |
| 102 | + { text: 'Create' }, |
| 103 | + ]); |
| 104 | + expect(container).toMatchInlineSnapshot(` |
| 105 | + <div> |
| 106 | + Spaces Edit Page: {"capabilities":{"catalogue":{},"management":{},"navLinks":{}},"http":{"basePath":{"basePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"spacesManager":{"onActiveSpaceChange$":{"_isScalar":false}},"securityEnabled":true} |
| 107 | + </div> |
| 108 | + `); |
| 109 | + |
| 110 | + unmount(); |
| 111 | + |
| 112 | + expect(container).toMatchInlineSnapshot(`<div />`); |
| 113 | + }); |
| 114 | + |
| 115 | + it('mount() works for the `edit space` page', async () => { |
| 116 | + const basePath = '/some-base-path/spaces'; |
| 117 | + const spaceId = 'some-space'; |
| 118 | + window.location.hash = `${basePath}/edit/${spaceId}`; |
| 119 | + |
| 120 | + const { setBreadcrumbs, container, unmount } = await mountApp(basePath, spaceId); |
| 121 | + |
| 122 | + expect(setBreadcrumbs).toHaveBeenCalledTimes(1); |
| 123 | + expect(setBreadcrumbs).toHaveBeenCalledWith([ |
| 124 | + { href: `#${basePath}`, text: 'Spaces' }, |
| 125 | + { href: `#/some-base-path/spaces/edit/${spaceId}`, text: `space with id some-space` }, |
| 126 | + ]); |
| 127 | + expect(container).toMatchInlineSnapshot(` |
| 128 | + <div> |
| 129 | + Spaces Edit Page: {"capabilities":{"catalogue":{},"management":{},"navLinks":{}},"http":{"basePath":{"basePath":""},"anonymousPaths":{}},"notifications":{"toasts":{}},"spacesManager":{"onActiveSpaceChange$":{"_isScalar":false}},"spaceId":"some-space","securityEnabled":true} |
| 130 | + </div> |
| 131 | + `); |
| 132 | + |
| 133 | + unmount(); |
| 134 | + |
| 135 | + expect(container).toMatchInlineSnapshot(`<div />`); |
| 136 | + }); |
| 137 | +}); |
0 commit comments