Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/disabled attribute #39

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@grafana/aws-sdk",
"version": "0.0.43",
"version": "0.0.44",
"description": "Common AWS features for grafana",
"main": "dist/index.js",
"publishConfig": {
Expand Down
62 changes: 42 additions & 20 deletions src/sql/QueryEditor/QueryEditorHeader.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import '@testing-library/jest-dom/extend-expect';
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import { render } from '@testing-library/react';
import { mockQuery } from './__mocks__/query';
import { Props as QueryEditorHeaderProps, QueryEditorHeader } from './QueryEditorHeader';
import { DataQuery, DataSourceApi, DataSourceJsonData } from '@grafana/data';
import { RunQueryButtons } from '@grafana/async-query-data';
import { Button } from '@grafana/ui';
import { DataQuery, DataSourceApi, DataSourceJsonData, LoadingState, PanelData } from '@grafana/data';

const props: QueryEditorHeaderProps<DataSourceApi, DataQuery, DataSourceJsonData> = {
query: mockQuery,
Expand All @@ -14,25 +11,50 @@ const props: QueryEditorHeaderProps<DataSourceApi, DataQuery, DataSourceJsonData
enableRunButton: false,
datasource: {} as DataSourceApi,
};
jest.mock('@grafana/async-query-data', () => ({
RunQueryButtons: jest.fn(() => <div>Buttons</div>),
}));
jest.mock('@grafana/ui', () => ({
...jest.requireActual('@grafana/ui'),
Button: jest.fn(() => <div>Button</div>),
}));

describe('QueryEditorHeader', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should display RunQueryButtons if showAsyncQueryButtons prop is true', async () => {
render(<QueryEditorHeader {...props} showAsyncQueryButtons={true} />);
expect(RunQueryButtons).toHaveBeenCalled();
render(<QueryEditorHeader {...props} showAsyncQueryButtons={true} cancel={jest.fn()} />);
const runButton = screen.getByRole('button', { name: 'Run query' });
const stopButton = screen.getByRole('button', { name: 'Stop query' });
expect(runButton).toBeInTheDocument();
expect(stopButton).toBeInTheDocument();
});
it('should display the run button if showAsyncQueryButtons prop is false', async () => {
it('should display just the run button if showAsyncQueryButtons prop is false', async () => {
render(<QueryEditorHeader {...props} showAsyncQueryButtons={false} />);
expect(Button).toHaveBeenCalled();
expect(RunQueryButtons).not.toHaveBeenCalled();
const runButton = screen.getByRole('button', { name: 'Run query' });
const stopButton = screen.queryByRole('button', { name: 'Stop query' });
expect(runButton).toBeInTheDocument();
expect(stopButton).not.toBeInTheDocument();
});

it('run button should be disabled if enableButton prop is false', () => {
render(<QueryEditorHeader {...props} enableRunButton={false} showAsyncQueryButtons={false} />);
const runButton = screen.getByRole('button', { name: 'Run query' });
expect(runButton).toBeDisabled();
});

it('run button should be disabled if query is loading', () => {
render(
<QueryEditorHeader
{...props}
enableRunButton={true}
data={{ ...props.data, state: LoadingState.Loading } as PanelData}
showAsyncQueryButtons={false}
/>
);
const runButton = screen.getByRole('button', { name: 'Run query' });
expect(runButton).toBeDisabled();
});

it('should run queries when the run button is clicked', () => {
const onRunQuery = jest.fn();
render(
<QueryEditorHeader {...props} onRunQuery={onRunQuery} showAsyncQueryButtons={false} enableRunButton={true} />
);
const runButton = screen.getByRole('button', { name: 'Run query' });
expect(runButton).toBeInTheDocument();
fireEvent.click(runButton);
expect(onRunQuery).toBeCalledTimes(1);
});
});
2 changes: 1 addition & 1 deletion src/sql/QueryEditor/QueryEditorHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function QueryEditorHeader<
size="sm"
onClick={onRunQuery}
icon={data?.state === LoadingState.Loading ? 'fa fa-spinner' : undefined}
disabled={data?.state === LoadingState.Loading}
disabled={data?.state === LoadingState.Loading || !enableRunButton}
>
Run query
</Button>
Expand Down