diff --git a/public/app/features/explore/RichHistory/RichHistoryAddToLibrary.test.tsx b/public/app/features/explore/RichHistory/RichHistoryAddToLibrary.test.tsx
new file mode 100644
index 00000000000..486391fec2e
--- /dev/null
+++ b/public/app/features/explore/RichHistory/RichHistoryAddToLibrary.test.tsx
@@ -0,0 +1,40 @@
+import { screen } from '@testing-library/react';
+import { render } from 'test/test-utils';
+
+import { OrgRole } from '@grafana/data';
+import { contextSrv } from 'app/core/core';
+
+import { QueryLibraryContextProviderMock } from '../QueryLibrary/mocks';
+
+import { RichHistoryAddToLibrary } from './RichHistoryAddToLibrary';
+
+describe('RichHistoryAddToLibrary', () => {
+ it('should render button when save query is enabled', () => {
+ render(
+
+
+
+ );
+
+ expect(screen.getByRole('button', { name: /Save query/i })).toBeInTheDocument();
+ });
+ it('should not render button when save query is disabled', () => {
+ render(
+
+
+
+ );
+
+ expect(screen.queryByRole('button', { name: /Save query/i })).not.toBeInTheDocument();
+ });
+ it('should not render button when user has Viewer role', () => {
+ contextSrv.user.orgRole = OrgRole.Viewer;
+ render(
+
+
+
+ );
+
+ expect(screen.queryByRole('button', { name: /Save query/i })).not.toBeInTheDocument();
+ });
+});
diff --git a/public/app/features/explore/RichHistory/RichHistoryAddToLibrary.tsx b/public/app/features/explore/RichHistory/RichHistoryAddToLibrary.tsx
index 1c56a91e854..c89764d4aae 100644
--- a/public/app/features/explore/RichHistory/RichHistoryAddToLibrary.tsx
+++ b/public/app/features/explore/RichHistory/RichHistoryAddToLibrary.tsx
@@ -4,6 +4,7 @@ import { t } from '@grafana/i18n';
import { reportInteraction } from '@grafana/runtime';
import { DataQuery } from '@grafana/schema';
import { Button } from '@grafana/ui';
+import { contextSrv } from 'app/core/services/context_srv';
import { useDispatch, useSelector } from 'app/types/store';
import { useQueryLibraryContext } from '../QueryLibrary/QueryLibraryContext';
@@ -30,6 +31,10 @@ export const RichHistoryAddToLibrary = ({ query }: Props) => {
const buttonLabel = t('explore.rich-history-card.add-to-library', 'Save query');
+ if (contextSrv.hasRole('Viewer')) {
+ return null;
+ }
+
return queryLibraryEnabled && !hasBeenSaved ? (
<>
>
- ) : undefined;
+ ) : null;
};
diff --git a/public/app/features/explore/SecondaryActions.test.tsx b/public/app/features/explore/SecondaryActions.test.tsx
index 4081b7b2efd..32a92adfea8 100644
--- a/public/app/features/explore/SecondaryActions.test.tsx
+++ b/public/app/features/explore/SecondaryActions.test.tsx
@@ -104,4 +104,32 @@ describe('SecondaryActions', () => {
await user.click(screen.getByRole('button', { name: /Query inspector/i }));
expect(onClickQueryInspector).toBeCalledTimes(1);
});
+
+ it('should render add from saved queries button when saved queries is enabled', () => {
+ render(
+
+
+
+ );
+
+ expect(screen.getByRole('button', { name: /Add from saved queries/i })).toBeInTheDocument();
+ });
+
+ it('should not render add from saved queries button when saved queries is disabled', () => {
+ render(
+
+
+
+ );
+
+ expect(screen.queryByRole('button', { name: /Add from saved queries/i })).not.toBeInTheDocument();
+ });
});