Skip to content

Commit

Permalink
jest fixes part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
shivindera committed Jan 6, 2022
1 parent 26bb5fc commit 08a6918
Show file tree
Hide file tree
Showing 43 changed files with 298 additions and 350 deletions.
8 changes: 5 additions & 3 deletions src/plugins/data/public/actions/apply_filter_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
*/

import { i18n } from '@kbn/i18n';
import { ThemeServiceSetup } from 'kibana/public';
import { toMountPoint } from '../../../kibana_react/public';
import { Action, createAction, IncompatibleActionError } from '../../../ui_actions/public';
import { getOverlays, getTheme, getIndexPatterns } from '../services';
import { getOverlays, getIndexPatterns } from '../services';
import { applyFiltersPopover } from '../ui/apply_filters';
import { Filter, FilterManager, TimefilterContract, esFilters } from '..';

Expand All @@ -32,7 +33,8 @@ async function isCompatible(context: ApplyGlobalFilterActionContext) {

export function createFilterAction(
filterManager: FilterManager,
timeFilter: TimefilterContract
timeFilter: TimefilterContract,
theme: ThemeServiceSetup
): Action {
return createAction({
type: ACTION_GLOBAL_APPLY_FILTER,
Expand Down Expand Up @@ -78,7 +80,7 @@ export function createFilterAction(
resolve(filterSelection);
}
),
{ theme$: getTheme().theme$ }
{ theme$: theme.theme$ }
),
{
'data-test-subj': 'test',
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
setNotifications,
setOverlays,
setSearchService,
setTheme,
setUiSettings,
} from './services';
import { createSearchBar } from './ui/search_bar/create_search_bar';
Expand Down Expand Up @@ -83,7 +82,6 @@ export class DataPublicPlugin
const startServices = createStartServicesGetter(core.getStartServices);

this.usageCollection = usageCollection;
setTheme(core.theme);

const searchService = this.searchService.setup(core, {
bfetch,
Expand All @@ -100,7 +98,7 @@ export class DataPublicPlugin

uiActions.registerTrigger(applyFilterTrigger);
uiActions.registerAction(
createFilterAction(queryService.filterManager, queryService.timefilter.timefilter)
createFilterAction(queryService.filterManager, queryService.timefilter.timefilter, core.theme)
);

inspector.registerView(
Expand Down
9 changes: 6 additions & 3 deletions src/plugins/data/public/search/fetch/handle_response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { handleResponse } from './handle_response';
import { notificationServiceMock } from '../../../../../core/public/notifications/notifications_service.mock';
import { setNotifications } from '../../services';
import { IKibanaSearchResponse } from 'src/plugins/data/common';
import { themeServiceMock } from 'src/core/public/mocks';

jest.mock('@kbn/i18n', () => {
return {
Expand All @@ -22,6 +23,8 @@ jest.mock('@kbn/i18n', () => {
};
});

const theme = themeServiceMock.createStartContract();

describe('handleResponse', () => {
const notifications = notificationServiceMock.createStartContract();

Expand All @@ -37,7 +40,7 @@ describe('handleResponse', () => {
timed_out: true,
},
} as IKibanaSearchResponse<any>;
const result = handleResponse(request, response);
const result = handleResponse(request, response, theme);
expect(result).toBe(response);
expect(notifications.toasts.addWarning).toBeCalled();
expect((notifications.toasts.addWarning as jest.Mock).mock.calls[0][0].title).toMatch(
Expand All @@ -57,7 +60,7 @@ describe('handleResponse', () => {
},
},
} as IKibanaSearchResponse<any>;
const result = handleResponse(request, response);
const result = handleResponse(request, response, theme);
expect(result).toBe(response);
expect(notifications.toasts.addWarning).toBeCalled();
expect((notifications.toasts.addWarning as jest.Mock).mock.calls[0][0].title).toMatch(
Expand All @@ -70,7 +73,7 @@ describe('handleResponse', () => {
const response = {
rawResponse: {},
} as IKibanaSearchResponse<any>;
const result = handleResponse(request, response);
const result = handleResponse(request, response, theme);
expect(result).toBe(response);
});
});
18 changes: 14 additions & 4 deletions src/plugins/data/public/search/fetch/handle_response.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ import { i18n } from '@kbn/i18n';
import { EuiSpacer } from '@elastic/eui';
import { IKibanaSearchResponse } from 'src/plugins/data/common';
import { ShardFailureOpenModalButton } from '../../ui/shard_failure_modal';
import { ThemeServiceStart } from '../../../../../core/public';
import { toMountPoint } from '../../../../kibana_react/public';
import { getNotifications, getTheme } from '../../services';
import { getNotifications } from '../../services';
import type { SearchRequest } from '..';

export function handleResponse(request: SearchRequest, response: IKibanaSearchResponse) {
export function handleResponse(
request: SearchRequest,
response: IKibanaSearchResponse,
theme: ThemeServiceStart
) {
const { rawResponse } = response;

if (rawResponse.timed_out) {
Expand Down Expand Up @@ -45,9 +50,14 @@ export function handleResponse(request: SearchRequest, response: IKibanaSearchRe
<>
{description}
<EuiSpacer size="s" />
<ShardFailureOpenModalButton request={request.body} response={rawResponse} title={title} />
<ShardFailureOpenModalButton
request={request.body}
response={rawResponse}
theme={theme}
title={title}
/>
</>,
{ theme$: getTheme().theme$ }
{ theme$: theme.theme$ }
);

getNotifications().toasts.addWarning({ title, text });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import type { MockedKeys } from '@kbn/utility-types/jest';
import { CoreSetup, CoreStart } from '../../../../../core/public';
import { coreMock } from '../../../../../core/public/mocks';
import { coreMock, themeServiceMock } from '../../../../../core/public/mocks';
import { IEsSearchRequest } from '../../../common/search';
import { SearchInterceptor } from './search_interceptor';
import { AbortError } from '../../../../kibana_utils/public';
Expand Down Expand Up @@ -120,6 +120,7 @@ describe('SearchInterceptor', () => {
uiSettings: mockCoreSetup.uiSettings,
http: mockCoreSetup.http,
session: sessionService,
theme: themeServiceMock.createSetupContract(),
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
tap,
} from 'rxjs/operators';
import { PublicMethodsOf } from '@kbn/utility-types';
import { CoreSetup, CoreStart, ToastsSetup } from 'kibana/public';
import { CoreSetup, CoreStart, ThemeServiceSetup, ToastsSetup } from 'kibana/public';
import { i18n } from '@kbn/i18n';
import { BatchedFunc, BfetchPublicSetup } from 'src/plugins/bfetch/public';
import {
Expand Down Expand Up @@ -51,7 +51,6 @@ import { ISessionService, SearchSessionState } from '../session';
import { SearchResponseCache } from './search_response_cache';
import { createRequestHash } from './utils';
import { SearchAbortController } from './search_abort_controller';
import { getTheme } from '../../services';

export interface SearchInterceptorDeps {
bfetch: BfetchPublicSetup;
Expand All @@ -61,6 +60,7 @@ export interface SearchInterceptorDeps {
toasts: ToastsSetup;
usageCollector?: SearchUsageCollector;
session: ISessionService;
theme: ThemeServiceSetup;
}

const MAX_CACHE_ITEMS = 50;
Expand Down Expand Up @@ -378,7 +378,7 @@ export class SearchInterceptor {
private showTimeoutErrorToast = (e: SearchTimeoutError, sessionId?: string) => {
this.deps.toasts.addDanger({
title: 'Timed out',
text: toMountPoint(e.getErrorMessage(this.application), { theme$: getTheme().theme$ }),
text: toMountPoint(e.getErrorMessage(this.application), { theme$: this.deps.theme.theme$ }),
});
};

Expand All @@ -394,7 +394,7 @@ export class SearchInterceptor {
{
title: 'Your search session is still running',
text: toMountPoint(SearchSessionIncompleteWarning(this.docLinks), {
theme$: getTheme().theme$,
theme$: this.deps.theme.theme$,
}),
},
{
Expand Down Expand Up @@ -426,14 +426,14 @@ export class SearchInterceptor {
title: i18n.translate('data.search.esErrorTitle', {
defaultMessage: 'Cannot retrieve search results',
}),
text: toMountPoint(e.getErrorMessage(this.application), { theme$: getTheme().theme$ }),
text: toMountPoint(e.getErrorMessage(this.application), { theme$: this.deps.theme.theme$ }),
});
} else if (e.constructor.name === 'HttpFetchError') {
this.deps.toasts.addDanger({
title: i18n.translate('data.search.httpErrorTitle', {
defaultMessage: 'Cannot retrieve your data',
}),
text: toMountPoint(getHttpError(e.message), { theme$: getTheme().theme$ }),
text: toMountPoint(getHttpError(e.message), { theme$: this.deps.theme.theme$ }),
});
} else {
this.deps.toasts.addError(e, {
Expand Down
10 changes: 6 additions & 4 deletions src/plugins/data/public/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
esRawResponse,
} from '../../common/search';
import { AggsService, AggsStartDependencies } from './aggs';
import { IndexPatternsContract } from '..';
import { IKibanaSearchResponse, IndexPatternsContract, SearchRequest } from '..';
import { ISearchInterceptor, SearchInterceptor } from './search_interceptor';
import { SearchUsageCollector, createUsageCollector } from './collectors';
import { UsageCollectionSetup } from '../../../usage_collection/public';
Expand Down Expand Up @@ -88,7 +88,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
constructor(private initializerContext: PluginInitializerContext<ConfigSchema>) {}

public setup(
{ http, getStartServices, notifications, uiSettings }: CoreSetup,
{ http, getStartServices, notifications, uiSettings, theme }: CoreSetup,
{ bfetch, expressions, usageCollection, nowProvider }: SearchServiceSetupDependencies
): ISearchSetup {
this.usageCollector = createUsageCollector(getStartServices, usageCollection);
Expand All @@ -112,6 +112,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
startServices: getStartServices(),
usageCollector: this.usageCollector!,
session: this.sessionService,
theme,
});

expressions.registerFunction(
Expand Down Expand Up @@ -173,7 +174,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
}

public start(
{ http, uiSettings }: CoreStart,
{ http, theme, uiSettings }: CoreStart,
{ fieldFormats, indexPatterns }: SearchServiceStartDependencies
): ISearchStart {
const search = ((request, options = {}) => {
Expand All @@ -186,7 +187,8 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
const searchSourceDependencies: SearchSourceDependencies = {
getConfig: uiSettings.get.bind(uiSettings),
search,
onResponse: handleResponse,
onResponse: (request: SearchRequest, response: IKibanaSearchResponse) =>
handleResponse(request, response, theme),
};

return {
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/data/public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { NotificationsStart, CoreStart, ThemeServiceStart } from 'src/core/public';
import { NotificationsStart, CoreStart } from 'src/core/public';
import { createGetterSetter } from '../../kibana_utils/public';
import { IndexPatternsContract } from './data_views';
import { DataPublicPluginStart } from './types';
Expand All @@ -24,5 +24,3 @@ export const [getIndexPatterns, setIndexPatterns] =

export const [getSearchService, setSearchService] =
createGetterSetter<DataPublicPluginStart['search']>('Search');

export const [getTheme, setTheme] = createGetterSetter<ThemeServiceStart>('Theme');
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from '@elastic/eui';
// @ts-ignore
import { EuiSuperUpdateButton, OnRefreshProps } from '@elastic/eui';
import { ThemeServiceStart } from 'kibana/public';
import { IDataPluginServices, IIndexPattern, TimeRange, TimeHistoryContract, Query } from '../..';
import { useKibana, withKibana } from '../../../../kibana_react/public';
import QueryStringInputUI from './query_string_input';
Expand Down Expand Up @@ -61,6 +62,7 @@ export interface QueryBarTopRowProps {
showAutoRefreshOnly?: boolean;
timeHistory?: TimeHistoryContract;
timeRangeForSuggestionsOverride?: boolean;
theme: ThemeServiceStart;
}

// Needed for React.lazy
Expand Down Expand Up @@ -192,6 +194,7 @@ export default function QueryBarTopRow(props: QueryBarTopRowProps) {
nonKqlMode={props.nonKqlMode}
nonKqlModeHelpText={props.nonKqlModeHelpText}
timeRangeForSuggestionsOverride={props.timeRangeForSuggestionsOverride}
theme={props.theme}
/>
</EuiFlexItem>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {

import { FormattedMessage } from '@kbn/i18n-react';
import { debounce, compact, isEqual, isFunction } from 'lodash';
import { Toast } from 'src/core/public';
import { ThemeServiceStart, Toast } from 'src/core/public';
import { METRIC_TYPE } from '@kbn/analytics';
import { IDataPluginServices, IIndexPattern, Query } from '../..';
import { QuerySuggestion, QuerySuggestionTypes } from '../../autocomplete';
Expand All @@ -39,7 +39,6 @@ import type { PersistedLog } from '../../query';
import type { SuggestionsListSize } from '../typeahead/suggestions_component';
import { SuggestionsComponent } from '..';
import { KIBANA_USER_QUERY_LANGUAGE_KEY, getFieldSubtypeNested } from '../../../common';
import { getTheme } from '../../services';

export interface QueryStringInputProps {
indexPatterns: Array<IIndexPattern | string>;
Expand Down Expand Up @@ -83,6 +82,7 @@ export interface QueryStringInputProps {
* Override whether autocomplete suggestions are restricted by time range.
*/
timeRangeForSuggestionsOverride?: boolean;
theme: ThemeServiceStart;
}

interface Props extends QueryStringInputProps {
Expand Down Expand Up @@ -477,7 +477,7 @@ export default class QueryStringInputUI extends Component<Props, State> {
</EuiFlexItem>
</EuiFlexGroup>
</div>,
{ theme$: getTheme().theme$ }
{ theme$: this.props.theme.theme$ }
),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export function createSearchBar({ core, storage, data, usageCollection }: Statef
isClearable={props.isClearable}
placeholder={props.placeholder}
{...overrideDefaultBehaviors(props)}
theme={core.theme}
/>
</KibanaContextProvider>
);
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/data/public/ui/search_bar/search_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { EuiIconProps } from '@elastic/eui';

import { METRIC_TYPE } from '@kbn/analytics';
import { Query, Filter } from '@kbn/es-query';
import { ThemeServiceStart } from 'kibana/public';
import { withKibana, KibanaReactContextValue } from '../../../../kibana_react/public';

import QueryBarTopRow from '../query_string_input/query_bar_top_row';
Expand Down Expand Up @@ -79,6 +80,7 @@ export interface SearchBarOwnProps {
displayStyle?: 'inPage' | 'detached';
// super update button background fill control
fillSubmitButton?: boolean;
theme: ThemeServiceStart;
}

export type SearchBarProps = SearchBarOwnProps & SearchBarInjectedDeps;
Expand Down Expand Up @@ -391,6 +393,7 @@ class SearchBarUI extends Component<SearchBarProps, State> {
nonKqlMode={this.props.nonKqlMode}
nonKqlModeHelpText={this.props.nonKqlModeHelpText}
timeRangeForSuggestionsOverride={timeRangeForSuggestionsOverride}
theme={this.props.theme}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@

import { openModal } from './shard_failure_open_modal_button.test.mocks';
import React from 'react';
import { themeServiceMock } from 'src/core/public/mocks';
import { mountWithIntl } from '@kbn/test/jest';
import ShardFailureOpenModalButton from './shard_failure_open_modal_button';
import { shardFailureRequest } from './__mocks__/shard_failure_request';
import { shardFailureResponse } from './__mocks__/shard_failure_response';
import { findTestSubject } from '@elastic/eui/lib/test';

const theme = themeServiceMock.createStartContract();

describe('ShardFailureOpenModalButton', () => {
it('triggers the openModal function when "Show details" button is clicked', () => {
const component = mountWithIntl(
<ShardFailureOpenModalButton
request={shardFailureRequest}
response={shardFailureResponse}
theme={theme}
title="test"
/>
);
Expand Down
Loading

0 comments on commit 08a6918

Please sign in to comment.