diff --git a/.betterer.results b/.betterer.results index 8c3e603edaf..90751562676 100644 --- a/.betterer.results +++ b/.betterer.results @@ -8125,10 +8125,6 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "2"], [0, 0, 0, "Unexpected any. Specify a different type.", "3"] ], - "public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilder.test.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"] - ], "public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilder.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] ], diff --git a/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilder.test.tsx b/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilder.test.tsx index 43e40e85adf..855aa243ff8 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilder.test.tsx +++ b/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilder.test.tsx @@ -2,7 +2,14 @@ import { getByText, render, screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import React from 'react'; -import { LoadingState, MutableDataFrame, PanelData, TimeRange } from '@grafana/data'; +import { + DataSourceInstanceSettings, + DataSourcePluginMeta, + LoadingState, + MutableDataFrame, + PanelData, + TimeRange, +} from '@grafana/data'; import { PrometheusDatasource } from '../../datasource'; import PromQlLanguageProvider from '../../language_provider'; @@ -11,6 +18,7 @@ import { getLabelSelects } from '../testUtils'; import { PromVisualQuery } from '../types'; import { PromQueryBuilder } from './PromQueryBuilder'; +import { EXPLAIN_LABEL_FILTER_CONTENT } from './PromQueryBuilderExplained'; const defaultQuery: PromVisualQuery = { metric: 'random_metric', @@ -183,28 +191,69 @@ describe('PromQueryBuilder', () => { await userEvent.click(screen.getByText('histogram_metric_sum')); await waitFor(() => expect(screen.getAllByText(/hint:/)).toHaveLength(2)); }); + + it('shows explain section when showExplain is true', async () => { + const { datasource } = createDatasource(); + const props = createProps(datasource); + props.showExplain = true; + render( + + ); + expect(await screen.findByText(EXPLAIN_LABEL_FILTER_CONTENT)).toBeInTheDocument(); + }); + + it('does not show explain section when showExplain is false', async () => { + const { datasource } = createDatasource(); + const props = createProps(datasource); + render( + + ); + expect(await screen.queryByText(EXPLAIN_LABEL_FILTER_CONTENT)).not.toBeInTheDocument(); + }); }); -function setup(query: PromVisualQuery = defaultQuery, data?: PanelData) { +function createDatasource() { const languageProvider = new EmptyLanguageProviderMock() as unknown as PromQlLanguageProvider; const datasource = new PrometheusDatasource( { url: '', jsonData: {}, - meta: {} as any, - } as any, + meta: {} as DataSourcePluginMeta, + } as DataSourceInstanceSettings, undefined, undefined, languageProvider ); - const props = { + return { datasource, languageProvider }; +} + +function createProps(datasource: PrometheusDatasource, data?: PanelData) { + return { datasource, onRunQuery: () => {}, onChange: () => {}, data, showExplain: false, }; +} +function setup(query: PromVisualQuery = defaultQuery, data?: PanelData) { + const { datasource, languageProvider } = createDatasource(); + const props = createProps(datasource, data); const { container } = render(); return { languageProvider, datasource, container }; } diff --git a/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilder.tsx b/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilder.tsx index cc148bb9948..52a8ef0be25 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilder.tsx +++ b/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilder.tsx @@ -20,6 +20,7 @@ import { PromVisualQuery } from '../types'; import { MetricSelect } from './MetricSelect'; import { NestedQueryList } from './NestedQueryList'; +import { EXPLAIN_LABEL_FILTER_CONTENT } from './PromQueryBuilderExplained'; export interface Props { query: PromVisualQuery; @@ -115,7 +116,7 @@ export const PromQueryBuilder = React.memo((props) => { stepNumber={1} title={} > - Fetch all series matching metric name and label filters. + {EXPLAIN_LABEL_FILTER_CONTENT} )} diff --git a/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilderExplained.tsx b/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilderExplained.tsx index 43f7255bdf4..c15fbc03505 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilderExplained.tsx +++ b/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilderExplained.tsx @@ -10,6 +10,8 @@ import { OperationListExplained } from '../shared/OperationListExplained'; import { RawQuery } from '../shared/RawQuery'; import { PromVisualQuery } from '../types'; +export const EXPLAIN_LABEL_FILTER_CONTENT = 'Fetch all series matching metric name and label filters.'; + export interface Props { query: string; } @@ -24,7 +26,7 @@ export const PromQueryBuilderExplained = React.memo(({ query }) => { stepNumber={1} title={} > - Fetch all series matching metric name and label filters. + {EXPLAIN_LABEL_FILTER_CONTENT} stepNumber={2} diff --git a/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryCodeEditor.test.tsx b/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryCodeEditor.test.tsx new file mode 100644 index 00000000000..d42d6bb8881 --- /dev/null +++ b/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryCodeEditor.test.tsx @@ -0,0 +1,57 @@ +import { render, screen } from '@testing-library/react'; +import React from 'react'; + +import { DataSourceInstanceSettings, DataSourcePluginMeta } from '@grafana/data'; + +import { PrometheusDatasource } from '../../datasource'; +import PromQlLanguageProvider from '../../language_provider'; +import { EmptyLanguageProviderMock } from '../../language_provider.mock'; + +import { EXPLAIN_LABEL_FILTER_CONTENT } from './PromQueryBuilderExplained'; +import { PromQueryCodeEditor } from './PromQueryCodeEditor'; + +jest.mock('../../components/monaco-query-field/MonacoQueryFieldWrapper', () => { + const fakeQueryField = () =>
prometheus query field
; + return { MonacoQueryFieldWrapper: fakeQueryField }; +}); + +function createDatasource() { + const languageProvider = new EmptyLanguageProviderMock() as unknown as PromQlLanguageProvider; + const datasource = new PrometheusDatasource( + { + url: '', + jsonData: {}, + meta: {} as DataSourcePluginMeta, + } as DataSourceInstanceSettings, + undefined, + undefined, + languageProvider + ); + return { datasource, languageProvider }; +} + +function createProps(datasource: PrometheusDatasource) { + return { + datasource, + onRunQuery: () => {}, + onChange: () => {}, + showExplain: false, + }; +} + +describe('PromQueryCodeEditor', () => { + it('shows explain section when showExplain is true', async () => { + const { datasource } = createDatasource(); + const props = createProps(datasource); + props.showExplain = true; + render(); + expect(await screen.findByText(EXPLAIN_LABEL_FILTER_CONTENT)).toBeInTheDocument(); + }); + + it('does not show explain section when showExplain is false', async () => { + const { datasource } = createDatasource(); + const props = createProps(datasource); + render(); + expect(await screen.queryByText(EXPLAIN_LABEL_FILTER_CONTENT)).not.toBeInTheDocument(); + }); +}); diff --git a/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryEditorSelector.test.tsx b/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryEditorSelector.test.tsx index 9fdce7957a6..6417f8c698d 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryEditorSelector.test.tsx +++ b/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryEditorSelector.test.tsx @@ -9,6 +9,7 @@ import { EmptyLanguageProviderMock } from '../../language_provider.mock'; import { PromQuery } from '../../types'; import { QueryEditorMode } from '../shared/types'; +import { EXPLAIN_LABEL_FILTER_CONTENT } from './PromQueryBuilderExplained'; import { PromQueryEditorSelector } from './PromQueryEditorSelector'; // We need to mock this because it seems jest has problem importing monaco in tests @@ -108,6 +109,13 @@ describe('PromQueryEditorSelector', () => { expect(screen.getByLabelText('selector').textContent).toBe('my_metric'); }); + it('Can enable explain', async () => { + renderWithMode(QueryEditorMode.Builder); + expect(screen.queryByText(EXPLAIN_LABEL_FILTER_CONTENT)).not.toBeInTheDocument(); + screen.getByLabelText('Explain').click(); + expect(await screen.findByText(EXPLAIN_LABEL_FILTER_CONTENT)).toBeInTheDocument(); + }); + it('changes to code mode', async () => { const { onChange } = renderWithMode(QueryEditorMode.Builder); await switchToMode(QueryEditorMode.Code);