-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e8fe9e
commit 793ce95
Showing
6 changed files
with
327 additions
and
10 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/core/server/rendering/bootstrap/bootstrap_renderer.test.mocks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export const renderTemplateMock = jest.fn(); | ||
jest.doMock('./render_template', () => ({ | ||
renderTemplate: renderTemplateMock, | ||
})); | ||
|
||
export const getThemeTagMock = jest.fn(); | ||
jest.doMock('./get_theme_tag', () => ({ | ||
getThemeTag: getThemeTagMock, | ||
})); | ||
|
||
export const getPluginsBundlePathsMock = jest.fn(); | ||
jest.doMock('./get_plugin_bundle_paths', () => ({ | ||
getPluginsBundlePaths: getPluginsBundlePathsMock, | ||
})); | ||
|
||
export const getJsDependencyPathsMock = jest.fn(); | ||
jest.doMock('./get_js_dependency_paths', () => ({ | ||
getJsDependencyPaths: getJsDependencyPathsMock, | ||
})); |
241 changes: 241 additions & 0 deletions
241
src/core/server/rendering/bootstrap/bootstrap_renderer.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { | ||
renderTemplateMock, | ||
getPluginsBundlePathsMock, | ||
getThemeTagMock, | ||
getJsDependencyPathsMock, | ||
} from './bootstrap_renderer.test.mocks'; | ||
|
||
import { PackageInfo } from '@kbn/config'; | ||
import { UiPlugins } from '../../plugins'; | ||
import { httpServiceMock } from '../../http/http_service.mock'; | ||
import { httpServerMock } from '../../http/http_server.mocks'; | ||
import { AuthStatus } from '../../http'; | ||
import { uiSettingsServiceMock } from '../../ui_settings/ui_settings_service.mock'; | ||
import { bootstrapRendererFactory, BootstrapRenderer } from './bootstrap_renderer'; | ||
|
||
const createPackageInfo = (parts: Partial<PackageInfo> = {}): PackageInfo => ({ | ||
branch: 'master', | ||
buildNum: 42, | ||
buildSha: 'buildSha', | ||
dist: false, | ||
version: '8.0.0', | ||
...parts, | ||
}); | ||
|
||
const createUiPlugins = (): UiPlugins => ({ | ||
public: new Map(), | ||
internal: new Map(), | ||
browserConfigs: new Map(), | ||
}); | ||
|
||
describe('bootstrapRenderer', () => { | ||
let auth: ReturnType<typeof httpServiceMock.createAuth>; | ||
let uiSettingsClient: ReturnType<typeof uiSettingsServiceMock.createClient>; | ||
let renderer: BootstrapRenderer; | ||
let uiPlugins: UiPlugins; | ||
let packageInfo: PackageInfo; | ||
|
||
beforeEach(() => { | ||
auth = httpServiceMock.createAuth(); | ||
uiSettingsClient = uiSettingsServiceMock.createClient(); | ||
uiPlugins = createUiPlugins(); | ||
packageInfo = createPackageInfo(); | ||
|
||
getThemeTagMock.mockReturnValue('v8light'); | ||
getPluginsBundlePathsMock.mockReturnValue(new Map()); | ||
renderTemplateMock.mockReturnValue('__rendered__'); | ||
getJsDependencyPathsMock.mockReturnValue([]); | ||
|
||
renderer = bootstrapRendererFactory({ | ||
auth, | ||
packageInfo, | ||
uiPlugins, | ||
serverBasePath: '/base-path', | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
getThemeTagMock.mockReset(); | ||
getPluginsBundlePathsMock.mockReset(); | ||
renderTemplateMock.mockReset(); | ||
getJsDependencyPathsMock.mockReset(); | ||
}); | ||
|
||
describe('when the auth status is `authenticated`', () => { | ||
beforeEach(() => { | ||
auth.get.mockReturnValue({ | ||
status: 'authenticated' as AuthStatus, | ||
state: {}, | ||
}); | ||
}); | ||
|
||
it('calls uiSettingsClient.get with the correct parameters', async () => { | ||
const request = httpServerMock.createKibanaRequest(); | ||
|
||
await renderer({ | ||
request, | ||
uiSettingsClient, | ||
}); | ||
|
||
expect(uiSettingsClient.get).toHaveBeenCalledTimes(2); | ||
expect(uiSettingsClient.get).toHaveBeenCalledWith('theme:darkMode'); | ||
expect(uiSettingsClient.get).toHaveBeenCalledWith('theme:version'); | ||
}); | ||
|
||
it('calls getThemeTag with the correct parameters', async () => { | ||
uiSettingsClient.get.mockImplementation((settingName) => { | ||
return Promise.resolve(settingName === 'theme:darkMode' ? true : 'v8'); | ||
}); | ||
|
||
const request = httpServerMock.createKibanaRequest(); | ||
|
||
await renderer({ | ||
request, | ||
uiSettingsClient, | ||
}); | ||
|
||
expect(getThemeTagMock).toHaveBeenCalledTimes(1); | ||
expect(getThemeTagMock).toHaveBeenCalledWith({ | ||
themeVersion: 'v8', | ||
darkMode: true, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when the auth status is `unknown`', () => { | ||
beforeEach(() => { | ||
auth.get.mockReturnValue({ | ||
status: 'unknown' as AuthStatus, | ||
state: {}, | ||
}); | ||
}); | ||
|
||
it('calls uiSettingsClient.get with the correct parameters', async () => { | ||
const request = httpServerMock.createKibanaRequest(); | ||
|
||
await renderer({ | ||
request, | ||
uiSettingsClient, | ||
}); | ||
|
||
expect(uiSettingsClient.get).toHaveBeenCalledTimes(2); | ||
expect(uiSettingsClient.get).toHaveBeenCalledWith('theme:darkMode'); | ||
expect(uiSettingsClient.get).toHaveBeenCalledWith('theme:version'); | ||
}); | ||
|
||
it('calls getThemeTag with the correct parameters', async () => { | ||
uiSettingsClient.get.mockImplementation((settingName) => { | ||
return Promise.resolve(settingName === 'theme:darkMode' ? true : 'v8'); | ||
}); | ||
|
||
const request = httpServerMock.createKibanaRequest(); | ||
|
||
await renderer({ | ||
request, | ||
uiSettingsClient, | ||
}); | ||
|
||
expect(getThemeTagMock).toHaveBeenCalledTimes(1); | ||
expect(getThemeTagMock).toHaveBeenCalledWith({ | ||
themeVersion: 'v8', | ||
darkMode: true, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when the auth status is `unauthenticated`', () => { | ||
beforeEach(() => { | ||
auth.get.mockReturnValue({ | ||
status: 'unauthenticated' as AuthStatus, | ||
state: {}, | ||
}); | ||
}); | ||
|
||
it('does not call uiSettingsClient.get', async () => { | ||
const request = httpServerMock.createKibanaRequest(); | ||
|
||
await renderer({ | ||
request, | ||
uiSettingsClient, | ||
}); | ||
|
||
expect(uiSettingsClient.get).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls getThemeTag with the default parameters', async () => { | ||
const request = httpServerMock.createKibanaRequest(); | ||
|
||
await renderer({ | ||
request, | ||
uiSettingsClient, | ||
}); | ||
|
||
expect(getThemeTagMock).toHaveBeenCalledTimes(1); | ||
expect(getThemeTagMock).toHaveBeenCalledWith({ | ||
themeVersion: 'v7', | ||
darkMode: false, | ||
}); | ||
}); | ||
}); | ||
|
||
it('calls getPluginsBundlePaths with the correct parameters', async () => { | ||
const request = httpServerMock.createKibanaRequest(); | ||
|
||
await renderer({ | ||
request, | ||
uiSettingsClient, | ||
}); | ||
|
||
expect(getPluginsBundlePathsMock).toHaveBeenCalledTimes(1); | ||
expect(getPluginsBundlePathsMock).toHaveBeenCalledWith({ | ||
uiPlugins, | ||
regularBundlePath: '/base-path/42/bundles', | ||
}); | ||
}); | ||
|
||
// here | ||
it('calls getJsDependencyPaths with the correct parameters', async () => { | ||
const pluginsBundlePaths = new Map<string, unknown>(); | ||
|
||
getPluginsBundlePathsMock.mockReturnValue(pluginsBundlePaths); | ||
const request = httpServerMock.createKibanaRequest(); | ||
|
||
await renderer({ | ||
request, | ||
uiSettingsClient, | ||
}); | ||
|
||
expect(getJsDependencyPathsMock).toHaveBeenCalledTimes(1); | ||
expect(getJsDependencyPathsMock).toHaveBeenCalledWith( | ||
'/base-path/42/bundles', | ||
pluginsBundlePaths | ||
); | ||
}); | ||
|
||
it('calls renderTemplate with the correct parameters', async () => { | ||
getThemeTagMock.mockReturnValue('customThemeTag'); | ||
getJsDependencyPathsMock.mockReturnValue(['path-1', 'path-2']); | ||
|
||
const request = httpServerMock.createKibanaRequest(); | ||
|
||
await renderer({ | ||
request, | ||
uiSettingsClient, | ||
}); | ||
|
||
expect(renderTemplateMock).toHaveBeenCalledTimes(1); | ||
expect(renderTemplateMock).toHaveBeenCalledWith({ | ||
themeTag: 'customThemeTag', | ||
jsDependencyPaths: ['path-1', 'path-2'], | ||
publicPathMap: expect.any(String), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/core/server/rendering/bootstrap/get_js_dependency_paths.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getJsDependencyPaths } from './get_js_dependency_paths'; | ||
import type { PluginInfo } from './get_plugin_bundle_paths'; | ||
|
||
describe('getJsDependencyPaths', () => { | ||
it('returns the correct list of paths', () => { | ||
const bundlePaths = new Map<string, PluginInfo>(); | ||
bundlePaths.set('plugin1', { | ||
bundlePath: 'plugin1/bundle-path.js', | ||
publicPath: 'plugin1/public-path', | ||
}); | ||
bundlePaths.set('plugin2', { | ||
bundlePath: 'plugin2/bundle-path.js', | ||
publicPath: 'plugin2/public-path', | ||
}); | ||
|
||
expect(getJsDependencyPaths('/regular-bundle-path', bundlePaths)).toEqual([ | ||
'/regular-bundle-path/kbn-ui-shared-deps/[email protected]', | ||
'/regular-bundle-path/kbn-ui-shared-deps/kbn-ui-shared-deps.js', | ||
'/regular-bundle-path/core/core.entry.js', | ||
'plugin1/bundle-path.js', | ||
'plugin2/bundle-path.js', | ||
]); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
src/core/server/rendering/bootstrap/get_js_dependency_paths.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import * as UiSharedDeps from '@kbn/ui-shared-deps'; | ||
import type { PluginInfo } from './get_plugin_bundle_paths'; | ||
|
||
export const getJsDependencyPaths = ( | ||
regularBundlePath: string, | ||
bundlePaths: Map<string, PluginInfo> | ||
) => { | ||
return [ | ||
...UiSharedDeps.jsDepFilenames.map( | ||
(filename) => `${regularBundlePath}/kbn-ui-shared-deps/${filename}` | ||
), | ||
`${regularBundlePath}/kbn-ui-shared-deps/${UiSharedDeps.jsFilename}`, | ||
`${regularBundlePath}/core/core.entry.js`, | ||
...[...bundlePaths.values()].map((plugin) => plugin.bundlePath), | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters