Skip to content

Commit

Permalink
Fix broken TS types
Browse files Browse the repository at this point in the history
  • Loading branch information
davismcphee committed Jul 19, 2023
1 parent 1265587 commit b3f27a3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
*/

import { ReactElement } from 'react';
import { FilterManager } from '@kbn/data-plugin/public';
import { createFilterManagerMock } from '@kbn/data-plugin/public/query/filter_manager/filter_manager.mock';
import { SearchInput } from '..';
import { getSavedSearchUrl } from '@kbn/saved-search-plugin/public';
import { DiscoverServices } from '../build_services';
import { discoverServiceMock } from '../__mocks__/services';
import { SavedSearchEmbeddable, SearchEmbeddableConfig } from './saved_search_embeddable';
Expand Down Expand Up @@ -52,7 +49,6 @@ const dataViewMock = buildDataViewMock({ name: 'the-data-view', fields: deepMock

describe('saved search embeddable', () => {
let mountpoint: HTMLDivElement;
let filterManagerMock: jest.Mocked<FilterManager>;
let servicesMock: jest.Mocked<DiscoverServices>;

let executeTriggerActions: jest.Mock;
Expand All @@ -68,21 +64,15 @@ describe('saved search embeddable', () => {
searchSource,
viewMode: viewModeMockValue,
};

const url = getSavedSearchUrl(savedSearchMock.id);
const editUrl = `/app/discover${url}`;
const indexPatterns = [dataViewMock];
executeTriggerActions = jest.fn();
const savedSearchEmbeddableConfig: SearchEmbeddableConfig = {
savedSearch: savedSearchMock,
editUrl,
editPath: url,
editable: true,
indexPatterns,
filterManager: filterManagerMock,
services: servicesMock,
executeTriggerActions,
};
const searchInput: SearchInput = {
id: 'mock-embeddable-id',
savedObjectId: savedSearchMock.id,
timeRange: { from: 'now-15m', to: 'now' },
columns: ['message', 'extension'],
rowHeight: 30,
Expand All @@ -91,14 +81,7 @@ describe('saved search embeddable', () => {
if (customTitle) {
searchInput.title = customTitle;
}

executeTriggerActions = jest.fn();

const embeddable = new SavedSearchEmbeddable(
savedSearchEmbeddableConfig,
searchInput,
executeTriggerActions
);
const embeddable = new SavedSearchEmbeddable(savedSearchEmbeddableConfig, searchInput);

// this helps to trigger reload
// eslint-disable-next-line dot-notation
Expand All @@ -111,7 +94,6 @@ describe('saved search embeddable', () => {

beforeEach(() => {
mountpoint = document.createElement('div');
filterManagerMock = createFilterManagerMock();

showFieldStatisticsMockValue = false;
viewModeMockValue = VIEW_MODE.DOCUMENT_LEVEL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jest.mock('@kbn/embeddable-plugin/public', () => {

const input = {
id: 'mock-embeddable-id',
savedObjectId: 'mock-saved-object-id',
timeRange: { from: 'now-15m', to: 'now' },
columns: ['message', 'extension'],
rowHeight: 30,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,39 @@
*/

import { ContactCardEmbeddable } from '@kbn/embeddable-plugin/public/lib/test_samples';

import { ViewSavedSearchAction } from './view_saved_search_action';
import { SavedSearchEmbeddable } from './saved_search_embeddable';
import { createStartContractMock } from '../__mocks__/start_contract';
import { savedSearchMock } from '../__mocks__/saved_search';
import { discoverServiceMock } from '../__mocks__/services';
import { DataView } from '@kbn/data-views-plugin/public';
import { createFilterManagerMock } from '@kbn/data-plugin/public/query/filter_manager/filter_manager.mock';
import { ViewMode } from '@kbn/embeddable-plugin/public';

const applicationMock = createStartContractMock();
const savedSearch = savedSearchMock;
const dataViews = [] as DataView[];
const services = discoverServiceMock;
const filterManager = createFilterManagerMock();
const searchInput = {
timeRange: {
from: '2021-09-15',
to: '2021-09-16',
},
id: '1',
savedObjectId: 'mock-saved-object-id',
viewMode: ViewMode.VIEW,
};
const executeTriggerActions = async (triggerId: string, context: object) => {
return Promise.resolve(undefined);
};
const trigger = { id: 'ACTION_VIEW_SAVED_SEARCH' };
const embeddableConfig = {
savedSearch,
editUrl: '',
editPath: '',
dataViews,
editable: true,
filterManager,
services,
executeTriggerActions,
};

describe('view saved search action', () => {
it('is compatible when embeddable is of type saved search, in view mode && appropriate permissions are set', async () => {
const action = new ViewSavedSearchAction(applicationMock);
const embeddable = new SavedSearchEmbeddable(
embeddableConfig,
searchInput,
executeTriggerActions
);
const embeddable = new SavedSearchEmbeddable(embeddableConfig, searchInput);
expect(await action.isCompatible({ embeddable, trigger })).toBe(true);
});

Expand All @@ -78,7 +66,7 @@ describe('view saved search action', () => {
it('is not visible when in edit mode', async () => {
const action = new ViewSavedSearchAction(applicationMock);
const input = { ...searchInput, viewMode: ViewMode.EDIT };
const embeddable = new SavedSearchEmbeddable(embeddableConfig, input, executeTriggerActions);
const embeddable = new SavedSearchEmbeddable(embeddableConfig, input);
expect(
await action.isCompatible({
embeddable,
Expand All @@ -89,11 +77,7 @@ describe('view saved search action', () => {

it('execute navigates to a saved search', async () => {
const action = new ViewSavedSearchAction(applicationMock);
const embeddable = new SavedSearchEmbeddable(
embeddableConfig,
searchInput,
executeTriggerActions
);
const embeddable = new SavedSearchEmbeddable(embeddableConfig, searchInput);
await action.execute({ embeddable, trigger });
expect(applicationMock.navigateToApp).toHaveBeenCalledWith('discover', {
path: `#/view/${savedSearch.id}`,
Expand Down
15 changes: 13 additions & 2 deletions src/plugins/saved_search/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { of } from 'rxjs';
import { SearchSource, IKibanaSearchResponse } from '@kbn/data-plugin/public';
import { SearchSourceDependencies } from '@kbn/data-plugin/common/search';
import type { SearchResponse } from '@elastic/elasticsearch/lib/api/types';
import type { SavedSearchPublicPluginStart } from './plugin';
import type { SavedSearchAttributeService } from './services/saved_searches';

const createEmptySearchSource = jest.fn(() => {
const deps = {
Expand All @@ -29,7 +31,7 @@ const createEmptySearchSource = jest.fn(() => {
return searchSource;
});

const savedSearchStartMock = () => ({
const savedSearchStartMock = (): SavedSearchPublicPluginStart => ({
get: jest.fn().mockImplementation(() => ({
id: 'savedSearch',
title: 'savedSearchTitle',
Expand All @@ -40,7 +42,16 @@ const savedSearchStartMock = () => ({
searchSource: createEmptySearchSource(),
})),
save: jest.fn(),
find: jest.fn(),
byValue: {
attributeService: {
getInputAsRefType: jest.fn(),
getInputAsValueType: jest.fn(),
inputIsRefType: jest.fn(),
unwrapAttributes: jest.fn(),
wrapAttributes: jest.fn(),
} as unknown as SavedSearchAttributeService,
toSavedSearch: jest.fn(),
},
});

export const savedSearchPluginMock = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { createVisualizeServicesMock } from './mocks';
import { BehaviorSubject } from 'rxjs';
import type { VisualizeServices } from '../types';
import { savedSearchPluginMock } from '@kbn/saved-search-plugin/public/mocks';

const commonSerializedVisMock = {
type: 'area',
Expand Down Expand Up @@ -60,14 +61,12 @@ describe('getVisualizationInstance', () => {
getOutput$: jest.fn(() => subj.asObservable()),
}));
mockServices.savedSearch = {
...savedSearchPluginMock.createStartContract(),
get: jest.fn().mockImplementation(() => ({
id: 'savedSearch',
searchSource: {},
title: 'savedSearchTitle',
})),
getAll: jest.fn(),
getNew: jest.fn().mockImplementation(() => ({})),
save: jest.fn().mockImplementation(() => ({})),
};
});

Expand Down

0 comments on commit b3f27a3

Please sign in to comment.