Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Sep 30, 2020
1 parent e5d9a63 commit 17f6167
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 7 deletions.
50 changes: 50 additions & 0 deletions src/core/public/kbn_bootstrap.test.mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { applicationServiceMock } from './application/application_service.mock';
import { fatalErrorsServiceMock } from './fatal_errors/fatal_errors_service.mock';
export const fatalErrorMock = fatalErrorsServiceMock.createSetupContract();
export const coreSystemMock = {
setup: jest.fn().mockResolvedValue({
fatalErrors: fatalErrorMock,
}),
start: jest.fn().mockResolvedValue({
application: applicationServiceMock.createInternalStartContract(),
}),
};
jest.doMock('./core_system', () => ({
CoreSystem: jest.fn().mockImplementation(() => coreSystemMock),
}));

export const apmSystem = {
setup: jest.fn().mockResolvedValue(undefined),
start: jest.fn().mockResolvedValue(undefined),
};
export const ApmSystemConstructor = jest.fn().mockImplementation(() => apmSystem);
jest.doMock('./apm_system', () => ({
ApmSystem: ApmSystemConstructor,
}));

export const i18nLoad = jest.fn().mockResolvedValue(undefined);
jest.doMock('@kbn/i18n', () => ({
i18n: {
...jest.requireActual('@kbn/i18n').i18n,
load: i18nLoad,
},
}));
54 changes: 54 additions & 0 deletions src/core/public/kbn_bootstrap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { apmSystem, fatalErrorMock, i18nLoad } from './kbn_bootstrap.test.mocks';
import { __kbnBootstrap__ } from './';

describe('kbn_bootstrap', () => {
beforeAll(() => {
const metadata = {
i18n: { translationsUrl: 'http://localhost' },
vars: { apmConfig: null },
};
// eslint-disable-next-line no-unsanitized/property
document.body.innerHTML = `<kbn-injected-metadata data=${JSON.stringify(metadata)}>
</kbn-injected-metadata>`;
});

beforeEach(() => {
jest.clearAllMocks();
});

it('does not report a fatal error if apm load fails', async () => {
apmSystem.setup.mockRejectedValueOnce(new Error('reason'));
const consoleSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => undefined);

await __kbnBootstrap__();

expect(fatalErrorMock.add).toHaveBeenCalledTimes(0);
expect(consoleSpy).toHaveBeenCalledTimes(1);
});

it('reports a fatal error if i18n load fails', async () => {
i18nLoad.mockRejectedValueOnce(new Error('reason'));

await __kbnBootstrap__();

expect(fatalErrorMock.add).toHaveBeenCalledTimes(1);
});
});
15 changes: 8 additions & 7 deletions src/core/public/kbn_bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ export async function __kbnBootstrap__() {
document.querySelector('kbn-injected-metadata')!.getAttribute('data')!
);

let i18nError: Error | undefined;
const apmSystem = new ApmSystem(injectedMetadata.vars.apmConfig);
await apmSystem.setup();

let i18nError: Error | undefined;
try {
await i18n.load(injectedMetadata.i18n.translationsUrl);
} catch (error) {
i18nError = error;
}
await Promise.all([
// eslint-disable-next-line no-console
apmSystem.setup().catch(console.warn),
i18n.load(injectedMetadata.i18n.translationsUrl).catch((error) => {
i18nError = error;
}),
]);

const coreSystem = new CoreSystem({
injectedMetadata,
Expand Down

0 comments on commit 17f6167

Please sign in to comment.