Skip to content

Commit

Permalink
fix jest test mock
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkime committed Jun 5, 2020
1 parent cb9e775 commit 5de1b21
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 47 deletions.
6 changes: 1 addition & 5 deletions src/plugins/data/common/index_patterns/fields/field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
*/

import { Field } from './field';
import { IndexPattern } from '../index_patterns';
// import { notificationServiceMock } from '../../../../../core/public/mocks';
// import { FieldFormatsStart } from '../../field_formats';
import { IndexPattern } from '..';
import { FieldFormatMethods } from '../types';
import { KBN_FIELD_TYPES } from '../../../common';

Expand All @@ -37,7 +35,6 @@ describe('Field', function () {
{
fieldFormats: {} as FieldFormatMethods,
onNotification: () => {},
// toastNotifications: notificationServiceMock.createStartContract().toasts,
}
);
}
Expand Down Expand Up @@ -219,7 +216,6 @@ describe('Field', function () {
const field = new Field({ fieldFormatMap: { name: {} } } as IndexPattern, fieldValues, false, {
fieldFormats: {} as FieldFormatMethods,
onNotification: () => {},
// toastNotifications: notificationServiceMock.createStartContract().toasts,
});
expect(flatten(field)).toMatchSnapshot();
});
Expand Down
13 changes: 4 additions & 9 deletions src/plugins/data/common/index_patterns/fields/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
*/

import { i18n } from '@kbn/i18n';
// import { ToastsStart } from 'kibana/public';
// @ts-ignore
import { ObjDefine } from './obj_define';
import { IIndexPattern } from '../../types';
import { IndexPattern } from '../index_patterns';
import {
IFieldType,
getKbnFieldType,
Expand Down Expand Up @@ -55,21 +54,17 @@ export class Field implements IFieldType {
scripted?: boolean;
subType?: IFieldSubType;
displayName?: string;
indexPattern?: IIndexPattern;
indexPattern?: IndexPattern;
readFromDocValues?: boolean;
format: any;
$$spec: FieldSpec;
conflictDescriptions?: Record<string, string[]>;

constructor(
indexPattern: IIndexPattern,
indexPattern: IndexPattern,
spec: FieldSpec | Field,
shortDotsEnable: boolean,
{
fieldFormats,
onNotification,
}: // toastNotifications
FieldDependencies
{ fieldFormats, onNotification }: FieldDependencies
) {
// unwrap old instances of Field
if (spec instanceof Field) spec = spec.$$spec;
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/data/common/index_patterns/fields/field_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { findIndex } from 'lodash';
import { IIndexPattern } from '../../types';
import { IndexPattern } from '../index_patterns';
import { IFieldType } from '../../../common';
import { Field, FieldSpec } from './field';
import { FieldFormatMethods, OnNotification } from '../types';
Expand All @@ -39,7 +39,7 @@ export interface IIndexPatternFieldList extends Array<Field> {
}

export type CreateIndexPatternFieldList = (
indexPattern: IIndexPattern,
indexPattern: IndexPattern,
specs?: FieldSpec[],
shortDotsEnable?: boolean
) => IIndexPatternFieldList;
Expand All @@ -51,7 +51,7 @@ export const getIndexPatternFieldListCreator = ({
class FieldList extends Array<Field> implements IIndexPatternFieldList {
private byName: FieldMap = new Map();
private groups: Map<Field['type'], FieldMap> = new Map();
private indexPattern: IIndexPattern;
private indexPattern: IndexPattern;
private shortDotsEnable: boolean;
private setByName = (field: Field) => this.byName.set(field.name, field);
private setByGroup = (field: Field) => {
Expand All @@ -62,7 +62,7 @@ export const getIndexPatternFieldListCreator = ({
};
private removeByGroup = (field: IFieldType) => this.groups.get(field.type)!.delete(field.name);

constructor(indexPattern: IIndexPattern, specs: FieldSpec[] = [], shortDotsEnable = false) {
constructor(indexPattern: IndexPattern, specs: FieldSpec[] = [], shortDotsEnable = false) {
super();
this.indexPattern = indexPattern;
this.shortDotsEnable = shortDotsEnable;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/index_patterns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@

export * from './fields';
export * from './types';
export * from './index_patterns';
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* under the License.
*/

import { GetFieldsOptions, IIndexPatternsApiClient, IndexPattern } from '.';
import { IndexPattern } from './index_pattern';
import { GetFieldsOptions, IIndexPatternsApiClient } from './index_patterns_api_client';

/** @internal */
export const createFieldsFetcher = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,18 @@
*/

import { contains } from 'lodash';
// import React from 'react';
// import { History } from 'history';
// import { i18n } from '@kbn/i18n';
// import { EuiCallOut } from '@elastic/eui';
import { CoreStart } from 'kibana/public';
// import { toMountPoint } from '../../../../kibana_react/public';
import { IndexPatternsContract } from './index_patterns';

// export type EnsureDefaultIndexPattern = (history: History) => Promise<unknown> | undefined;

export type EnsureDefaultIndexPattern = () => Promise<unknown> | undefined;

export const createEnsureDefaultIndexPattern = (
core: CoreStart,
onRedirectNoIndexPattern: (core: CoreStart) => void
) => {
// let bannerId: string;
// let timeoutId: NodeJS.Timeout | undefined;

/**
* Checks whether a default index pattern is set and exists and defines
* one otherwise.
*
* If there are no index patterns, redirect to management page and show
* banner. In this case the promise returned from this function will never
* resolve to wait for the URL change to happen.
*/
return async function ensureDefaultIndexPattern(this: IndexPatternsContract) {
const patterns = await this.getIds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import { Field } from '../fields';

import { FieldFormatMethods } from '../types';

jest.mock('../../../../kibana_utils/public', () => {
const originalModule = jest.requireActual('../../../../kibana_utils/public');
jest.mock('../../../../kibana_utils/common', () => {
const originalModule = jest.requireActual('../../../../kibana_utils/common');

return {
...originalModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,22 +523,10 @@ export class IndexPattern implements IIndexPattern {
// const { toasts } = getNotifications();

if (err instanceof IndexPatternMissingIndices) {
// toasts.addDanger((err as any).message);
this.onNotification({ title: (err as any).message, color: 'danger' });
return [];
}

/*
toasts.addError(err, {
title: i18n.translate('data.indexPatterns.fetchFieldErrorTitle', {
defaultMessage: 'Error fetching fields for index pattern {title} (ID: {id})',
values: {
id: this.id,
title: this.title,
},
}),
});
*/
this.onNotification({
title: i18n.translate('data.indexPatterns.fetchFieldErrorTitle', {
defaultMessage: 'Error fetching fields for index pattern {title} (ID: {id})',
Expand All @@ -547,6 +535,8 @@ export class IndexPattern implements IIndexPattern {
title: this.title,
},
}),
// todo
// message: err,
color: 'danger',
}); // was addError
});
Expand Down

0 comments on commit 5de1b21

Please sign in to comment.