2022-04-06 06:49:25 -05:00
|
|
|
import { within } from '@testing-library/dom';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import { fromPairs } from 'lodash';
|
|
|
|
import React from 'react';
|
2022-02-23 12:05:38 -06:00
|
|
|
import { Provider } from 'react-redux';
|
|
|
|
import { Route, Router } from 'react-router-dom';
|
|
|
|
|
|
|
|
import { DataSourceApi, DataSourceInstanceSettings, QueryEditorProps, ScopedVars } from '@grafana/data';
|
|
|
|
import { locationService, setDataSourceSrv, setEchoSrv } from '@grafana/runtime';
|
|
|
|
import { GrafanaRoute } from 'app/core/navigation/GrafanaRoute';
|
|
|
|
import { Echo } from 'app/core/services/echo/Echo';
|
|
|
|
import { configureStore } from 'app/store/configureStore';
|
|
|
|
|
|
|
|
import { LokiDatasource } from '../../../../plugins/datasource/loki/datasource';
|
|
|
|
import { LokiQuery } from '../../../../plugins/datasource/loki/types';
|
2022-04-06 06:49:25 -05:00
|
|
|
import { ExploreId } from '../../../../types';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { initialUserState } from '../../../profile/state/reducers';
|
|
|
|
import Wrapper from '../../Wrapper';
|
2022-02-23 12:05:38 -06:00
|
|
|
|
|
|
|
type DatasourceSetup = { settings: DataSourceInstanceSettings; api: DataSourceApi };
|
|
|
|
|
|
|
|
type SetupOptions = {
|
|
|
|
// default true
|
|
|
|
clearLocalStorage?: boolean;
|
|
|
|
datasources?: DatasourceSetup[];
|
|
|
|
urlParams?: { left: string; right?: string };
|
|
|
|
searchParams?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function setupExplore(options?: SetupOptions): {
|
|
|
|
datasources: { [name: string]: DataSourceApi };
|
2022-04-07 04:27:58 -05:00
|
|
|
store: ReturnType<typeof configureStore>;
|
2022-02-23 12:05:38 -06:00
|
|
|
unmount: () => void;
|
2022-04-06 06:49:25 -05:00
|
|
|
container: HTMLElement;
|
2022-02-23 12:05:38 -06:00
|
|
|
} {
|
|
|
|
// Clear this up otherwise it persists data source selection
|
|
|
|
// TODO: probably add test for that too
|
|
|
|
if (options?.clearLocalStorage !== false) {
|
|
|
|
window.localStorage.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create this here so any mocks are recreated on setup and don't retain state
|
|
|
|
const defaultDatasources: DatasourceSetup[] = [
|
|
|
|
makeDatasourceSetup(),
|
|
|
|
makeDatasourceSetup({ name: 'elastic', id: 2 }),
|
|
|
|
];
|
|
|
|
|
|
|
|
const dsSettings = options?.datasources || defaultDatasources;
|
|
|
|
|
|
|
|
setDataSourceSrv({
|
|
|
|
getList(): DataSourceInstanceSettings[] {
|
|
|
|
return dsSettings.map((d) => d.settings);
|
|
|
|
},
|
|
|
|
getInstanceSettings(name: string) {
|
|
|
|
return dsSettings.map((d) => d.settings).find((x) => x.name === name || x.uid === name);
|
|
|
|
},
|
|
|
|
get(name?: string | null, scopedVars?: ScopedVars): Promise<DataSourceApi> {
|
|
|
|
return Promise.resolve(
|
|
|
|
(name ? dsSettings.find((d) => d.api.name === name || d.api.uid === name) : dsSettings[0])!.api
|
|
|
|
);
|
|
|
|
},
|
|
|
|
} as any);
|
|
|
|
|
|
|
|
setEchoSrv(new Echo());
|
|
|
|
|
|
|
|
const store = configureStore();
|
|
|
|
store.getState().user = {
|
|
|
|
...initialUserState,
|
|
|
|
orgId: 1,
|
|
|
|
timeZone: 'utc',
|
|
|
|
};
|
|
|
|
|
|
|
|
store.getState().navIndex = {
|
|
|
|
explore: {
|
|
|
|
id: 'explore',
|
|
|
|
text: 'Explore',
|
|
|
|
subTitle: 'Explore your data',
|
|
|
|
icon: 'compass',
|
|
|
|
url: '/explore',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
locationService.push({ pathname: '/explore', search: options?.searchParams });
|
|
|
|
|
|
|
|
if (options?.urlParams) {
|
|
|
|
locationService.partial(options.urlParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
const route = { component: Wrapper };
|
|
|
|
|
2022-04-06 06:49:25 -05:00
|
|
|
const { unmount, container } = render(
|
2022-02-23 12:05:38 -06:00
|
|
|
<Provider store={store}>
|
|
|
|
<Router history={locationService.getHistory()}>
|
|
|
|
<Route path="/explore" exact render={(props) => <GrafanaRoute {...props} route={route as any} />} />
|
|
|
|
</Router>
|
|
|
|
</Provider>
|
|
|
|
);
|
|
|
|
|
2022-04-06 06:49:25 -05:00
|
|
|
return { datasources: fromPairs(dsSettings.map((d) => [d.api.name, d.api])), store, unmount, container };
|
2022-02-23 12:05:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeDatasourceSetup({ name = 'loki', id = 1 }: { name?: string; id?: number } = {}): DatasourceSetup {
|
|
|
|
const meta: any = {
|
|
|
|
info: {
|
|
|
|
logos: {
|
|
|
|
small: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
id: id.toString(),
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
settings: {
|
|
|
|
id,
|
|
|
|
uid: name,
|
|
|
|
type: 'logs',
|
|
|
|
name,
|
|
|
|
meta,
|
|
|
|
access: 'proxy',
|
|
|
|
jsonData: {},
|
|
|
|
},
|
|
|
|
api: {
|
|
|
|
components: {
|
|
|
|
QueryEditor(props: QueryEditorProps<LokiDatasource, LokiQuery>) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<input
|
|
|
|
aria-label="query"
|
|
|
|
defaultValue={props.query.expr}
|
|
|
|
onChange={(event) => {
|
|
|
|
props.onChange({ ...props.query, expr: event.target.value });
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{name} Editor input: {props.query.expr}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: name,
|
|
|
|
uid: name,
|
|
|
|
query: jest.fn(),
|
2022-04-06 06:49:25 -05:00
|
|
|
getRef: jest.fn().mockReturnValue(name),
|
2022-02-23 12:05:38 -06:00
|
|
|
meta,
|
|
|
|
} as any,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-06 06:49:25 -05:00
|
|
|
export const waitForExplore = async (exploreId: ExploreId = ExploreId.left) => {
|
|
|
|
return await withinExplore(exploreId).findByText(/Editor/i);
|
2022-02-23 12:05:38 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
export const tearDown = () => {
|
|
|
|
window.localStorage.clear();
|
|
|
|
};
|
2022-04-06 06:49:25 -05:00
|
|
|
|
|
|
|
export const withinExplore = (exploreId: ExploreId) => {
|
|
|
|
const container = screen.getAllByTestId('data-testid Explore');
|
|
|
|
return within(container[exploreId === ExploreId.left ? 0 : 1]);
|
|
|
|
};
|