Skip to content

Commit ddbcf42

Browse files
authored
Add isQueryValid prop for RunQueryButtons (#3)
* Add isQueryValid prop for RunQueryButtons * add test
1 parent f915256 commit ddbcf42

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/RunQueryButtons.test.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
4+
import { DataQuery } from '@grafana/data';
5+
import { RunQueryButtons, RunQueryButtonsProps } from './RunQueryButtons';
6+
7+
const getDefaultProps = (overrides: Partial<RunQueryButtonsProps<DataQuery>>) => {
8+
return {
9+
onRunQuery: jest.fn(),
10+
onCancelQuery: jest.fn(),
11+
isQueryValid: jest.fn(),
12+
query: { refId: 'refId' },
13+
...overrides,
14+
};
15+
};
16+
17+
describe('RunQueryButtons', () => {
18+
it('disable the run button if the query is invalid', () => {
19+
const props = getDefaultProps({ isQueryValid: jest.fn().mockReturnValue(false) });
20+
render(<RunQueryButtons {...props} />);
21+
const runButton = screen.getByRole('button', { name: 'Run' });
22+
expect(runButton).toBeDisabled();
23+
});
24+
25+
it('run button should be enabled if the query is valid', () => {
26+
const props = getDefaultProps({ isQueryValid: jest.fn().mockReturnValue(true) });
27+
render(<RunQueryButtons {...props} />);
28+
const runButton = screen.getByRole('button', { name: 'Run' });
29+
expect(runButton).not.toBeDisabled();
30+
});
31+
});

src/RunQueryButtons.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import React, { useEffect, useState } from 'react';
22
import { HorizontalGroup, Button, Spinner } from '@grafana/ui';
33
import { DataQuery, LoadingState } from '@grafana/data';
44

5-
interface RunQueryButtonsProps<TQuery extends DataQuery> {
5+
export interface RunQueryButtonsProps<TQuery extends DataQuery> {
66
onRunQuery: () => void;
77
onCancelQuery: (query: TQuery) => void;
8+
isQueryValid: (query: TQuery) => boolean;
89
query: TQuery;
910
state?: LoadingState;
1011
}
@@ -36,9 +37,11 @@ export const RunQueryButtons = <TQuery extends DataQuery>(props: RunQueryButtons
3637
setStopping(true);
3738
};
3839

40+
const isQueryValid = props.isQueryValid(props.query);
41+
3942
return (
4043
<HorizontalGroup>
41-
<Button icon={running ? undefined : 'play'} disabled={running} onClick={onRunQuery}>
44+
<Button icon={running ? undefined : 'play'} disabled={running || !isQueryValid} onClick={onRunQuery}>
4245
{running && !stopping ? (
4346
<HorizontalGroup>
4447
<Spinner /> Running

0 commit comments

Comments
 (0)