Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Feb 5, 2020
1 parent 28c5304 commit 33d6440
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ jest.mock('plugins/data', () => {
};
});

jest.mock('../lib/get_default_query_language', () => ({
getDefaultQueryLanguage: () => 'kuery',
}));

import { GaugePanelConfig } from './gauge';

describe('GaugePanelConfig', () => {
Expand Down
55 changes: 55 additions & 0 deletions src/legacy/core_plugins/vis_type_vega/public/__mocks__/services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 { createGetterSetter } from '../../../../../plugins/kibana_utils/common';
import { DataPublicPluginStart } from '../../../../../plugins/data/public';
import { IUiSettingsClient, NotificationsStart, SavedObjectsStart } from 'kibana/public';
import { dataPluginMock } from '../../../../../plugins/data/public/mocks';
import { coreMock } from '../../../../../core/public/mocks';

export const [getData, setData] = createGetterSetter<DataPublicPluginStart>('Data');
setData(dataPluginMock.createStartContract());

export const [getNotifications, setNotifications] = createGetterSetter<NotificationsStart>(
'Notifications'
);
setNotifications(coreMock.createStart().notifications);

export const [getUISettings, setUISettings] = createGetterSetter<IUiSettingsClient>('UISettings');
setUISettings(coreMock.createStart().uiSettings);

export const [getSavedObjects, setSavedObjects] = createGetterSetter<SavedObjectsStart>(
'SavedObjects'
);
setSavedObjects(coreMock.createStart().savedObjects);

export const [getInjectedVars, setInjectedVars] = createGetterSetter<{
esShardTimeout: number;
enableExternalUrls: boolean;
emsTileLayerId: unknown;
}>('InjectedVars');
setInjectedVars({
emsTileLayerId: {},
enableExternalUrls: true,
esShardTimeout: 10000,
});

export const getEsShardTimeout = () => getInjectedVars().esShardTimeout;
export const getEnableExternalUrls = () => getInjectedVars().enableExternalUrls;
export const getEmsTileLayerId = () => getInjectedVars().emsTileLayerId;
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import { cloneDeep } from 'lodash';
import moment from 'moment';
import { EsQueryParser } from './es_query_parser';

jest.mock('../helpers', () => ({
getEsShardTimeout: jest.fn(() => '10000'),
}));

const second = 1000;
const minute = 60 * second;
const hour = 60 * minute;
Expand All @@ -47,6 +43,8 @@ function create(min, max, dashboardCtx) {
return inst;
}

jest.mock('../services');

describe(`EsQueryParser time`, () => {
test(`roundInterval(4s)`, () => {
expect(EsQueryParser._roundInterval(4 * second)).toBe(`1s`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { SearchCache } from './search_cache';
jest.mock('../services');

describe(`SearchCache`, () => {
class FauxEs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { TimeCache } from './time_cache';
jest.mock('../services');

describe(`TimeCache`, () => {
class FauxTimefilter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { cloneDeep } from 'lodash';
import { VegaParser } from './vega_parser';
import { bypassExternalUrlCheck } from '../vega_view/vega_base_view';
jest.mock('../services');

describe(`VegaParser._setDefaultValue`, () => {
function check(spec, expected, ...params) {
Expand Down
12 changes: 6 additions & 6 deletions src/legacy/core_plugins/vis_type_vega/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import {
setNotifications,
setData,
setSavedObjects,
setEsShardTimeout,
setEnableExternalUrls,
setEmsTileLayerId,
setInjectedVars,
setUISettings,
} from './services';

Expand Down Expand Up @@ -67,9 +65,11 @@ export class VegaPlugin implements Plugin<Promise<void>, void> {
core: CoreSetup,
{ data, expressions, visualizations, __LEGACY }: VegaPluginSetupDependencies
) {
setEsShardTimeout(core.injectedMetadata.getInjectedVar('esShardTimeout') as number);
setEnableExternalUrls(core.injectedMetadata.getInjectedVar('enableExternalUrls') as boolean);
setEmsTileLayerId(core.injectedMetadata.getInjectedVar('emsTileLayerId', true));
setInjectedVars({
esShardTimeout: core.injectedMetadata.getInjectedVar('esShardTimeout') as number,
enableExternalUrls: core.injectedMetadata.getInjectedVar('enableExternalUrls') as boolean,
emsTileLayerId: core.injectedMetadata.getInjectedVar('emsTileLayerId', true),
});
setUISettings(core.uiSettings);

const visualizationDependencies: Readonly<VegaVisualizationDependencies> = {
Expand Down
16 changes: 9 additions & 7 deletions src/legacy/core_plugins/vis_type_vega/public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ export const [getSavedObjects, setSavedObjects] = createGetterSetter<SavedObject
'SavedObjects'
);

export const [getEsShardTimeout, setEsShardTimeout] = createGetterSetter<number>('EsShardTimeout');

export const [getEnableExternalUrls, setEnableExternalUrls] = createGetterSetter<boolean>(
'EnableExternalUrls'
);

export const [getEmsTileLayerId, setEmsTileLayerId] = createGetterSetter<unknown>('EmsTileLayerId');
export const [getInjectedVars, setInjectedVars] = createGetterSetter<{
esShardTimeout: number;
enableExternalUrls: boolean;
emsTileLayerId: unknown;
}>('InjectedVars');

export const getEsShardTimeout = () => getInjectedVars().esShardTimeout;
export const getEnableExternalUrls = () => getInjectedVars().enableExternalUrls;
export const getEmsTileLayerId = () => getInjectedVars().emsTileLayerId;
6 changes: 3 additions & 3 deletions src/plugins/kibana_utils/common/create_getter_setter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
export type Get<T> = () => T;
export type Set<T> = (value: T) => void;

export const createGetterSetter = <T>(name: string): [Get<T>, Set<T>] => {
let value: T | undefined;
export const createGetterSetter = <T extends object>(name: string): [Get<T>, Set<T>] => {
let value: T;

const get: Get<T> = () => {
if (value === undefined) throw new Error(`${name} was not set.`);
if (!value) throw new Error(`${name} was not set.`);
return value;
};

Expand Down

0 comments on commit 33d6440

Please sign in to comment.