Skip to content

Commit

Permalink
add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Mar 10, 2021
1 parent 5e8fe9e commit 793ce95
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 10 deletions.
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 src/core/server/rendering/bootstrap/bootstrap_renderer.test.ts
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),
});
});
});
11 changes: 2 additions & 9 deletions src/core/server/rendering/bootstrap/bootstrap_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
*/

import { createHash } from 'crypto';
import * as UiSharedDeps from '@kbn/ui-shared-deps';
import { PackageInfo } from '@kbn/config';
import { UiPlugins } from '../../plugins';
import { IUiSettingsClient } from '../../ui_settings';
import { HttpAuth, KibanaRequest } from '../../http';
import { getPluginsBundlePaths } from './get_plugin_bundle_paths';
import { getJsDependencyPaths } from './get_js_dependency_paths';
import { getThemeTag } from './get_theme_tag';
import { renderTemplate } from './render_template';

Expand Down Expand Up @@ -72,14 +72,7 @@ export const bootstrapRendererFactory: BootstrapRendererFactory = ({
regularBundlePath,
});

const jsDependencyPaths = [
...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),
];
const jsDependencyPaths = getJsDependencyPaths(regularBundlePath, bundlePaths);

// These paths should align with the bundle routes configured in
// src/optimize/bundles_route/bundles_route.ts
Expand Down
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 src/core/server/rendering/bootstrap/get_js_dependency_paths.ts
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),
];
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { UiPlugins } from '../../plugins';

interface PluginInfo {
export interface PluginInfo {
publicPath: string;
bundlePath: string;
}
Expand Down

0 comments on commit 793ce95

Please sign in to comment.