Skip to content

Commit 3a698ab

Browse files
authored
Make onCancelQuery optional (#5)
* Make onCancelQuery optional * add test to check stop button is rendered
1 parent 875503d commit 3a698ab

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

src/RunQueryButtons.test.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { render, screen } from '@testing-library/react';
44
import { DataQuery } from '@grafana/data';
55
import { RunQueryButtons, RunQueryButtonsProps } from './RunQueryButtons';
66

7-
const getDefaultProps = (overrides: Partial<RunQueryButtonsProps<DataQuery>>) => {
7+
const getDefaultProps = (overrides?: Partial<RunQueryButtonsProps<DataQuery>>) => {
88
return {
99
onRunQuery: jest.fn(),
1010
onCancelQuery: jest.fn(),
@@ -28,4 +28,22 @@ describe('RunQueryButtons', () => {
2828
const runButton = screen.getByRole('button', { name: 'Run' });
2929
expect(runButton).not.toBeDisabled();
3030
});
31+
32+
it('only renders the `Run` button if onCancelQuery is undefined', () => {
33+
const props = getDefaultProps({ onCancelQuery: undefined });
34+
render(<RunQueryButtons {...props} />);
35+
const runButton = screen.getByRole('button', { name: 'Run' });
36+
expect(runButton).toBeInTheDocument();
37+
const stopButton = screen.queryByRole('button', { name: 'Stop' });
38+
expect(stopButton).not.toBeInTheDocument();
39+
});
40+
41+
it('renders the `Run` and `Stop` buttons if onCancelQuery defined', () => {
42+
const props = getDefaultProps();
43+
render(<RunQueryButtons {...props} />);
44+
const runButton = screen.getByRole('button', { name: 'Run' });
45+
expect(runButton).toBeInTheDocument();
46+
const stopButton = screen.queryByRole('button', { name: 'Stop' });
47+
expect(stopButton).toBeInTheDocument();
48+
});
3149
});

src/RunQueryButtons.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DataQuery, LoadingState } from '@grafana/data';
44

55
export interface RunQueryButtonsProps<TQuery extends DataQuery> {
66
onRunQuery: () => void;
7-
onCancelQuery: (query: TQuery) => void;
7+
onCancelQuery?: (query: TQuery) => void;
88
isQueryValid: (query: TQuery) => boolean;
99
query: TQuery;
1010
state?: LoadingState;
@@ -32,10 +32,12 @@ export const RunQueryButtons = <TQuery extends DataQuery>(props: RunQueryButtons
3232
props.onRunQuery();
3333
};
3434

35-
const onCancelQuery = () => {
36-
props.onCancelQuery(lastQuery);
37-
setStopping(true);
38-
};
35+
const onCancelQuery = props.onCancelQuery
36+
? () => {
37+
props.onCancelQuery?.(lastQuery);
38+
setStopping(true);
39+
}
40+
: undefined;
3941

4042
const isQueryValid = props.isQueryValid(props.query);
4143

@@ -50,15 +52,17 @@ export const RunQueryButtons = <TQuery extends DataQuery>(props: RunQueryButtons
5052
'Run'
5153
)}
5254
</Button>
53-
<Button icon={running ? undefined : 'square-shape'} disabled={!running || stopping} onClick={onCancelQuery}>
54-
{stopping ? (
55-
<HorizontalGroup>
56-
<Spinner /> Stopping
57-
</HorizontalGroup>
58-
) : (
59-
'Stop'
60-
)}
61-
</Button>
55+
{onCancelQuery && (
56+
<Button icon={running ? undefined : 'square-shape'} disabled={!running || stopping} onClick={onCancelQuery}>
57+
{stopping ? (
58+
<HorizontalGroup>
59+
<Spinner /> Stopping
60+
</HorizontalGroup>
61+
) : (
62+
'Stop'
63+
)}
64+
</Button>
65+
)}
6266
</HorizontalGroup>
6367
);
6468
};

0 commit comments

Comments
 (0)