ShareDrawer: Share Snapshot menu item condition (#89733)

This commit is contained in:
Juan Cabanas 2024-06-26 12:06:10 -03:00 committed by GitHub
parent 06d5850396
commit e6e163eaf5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 11 deletions

View File

@ -2,10 +2,11 @@ import { render, screen } from '@testing-library/react';
import { selectors as e2eSelectors } from '@grafana/e2e-selectors';
import { SceneGridLayout, SceneTimeRange, VizPanel } from '@grafana/scenes';
import { contextSrv } from 'app/core/services/context_srv';
import { config } from '../../../../core/config';
import { DashboardGridItem } from '../../scene/DashboardGridItem';
import { DashboardScene } from '../../scene/DashboardScene';
import { DashboardScene, DashboardSceneState } from '../../scene/DashboardScene';
import ShareMenu from './ShareMenu';
@ -16,26 +17,66 @@ jest.mock('app/core/utils/shortLinks', () => ({
}));
const selector = e2eSelectors.pages.Dashboard.DashNav.newShareButton.menu;
describe('ShareMenu', () => {
afterEach(() => {
jest.resetModules();
jest.clearAllMocks();
});
it('should render menu items', async () => {
Object.defineProperty(contextSrv, 'isSignedIn', {
value: true,
});
config.featureToggles.publicDashboards = true;
config.publicDashboardsEnabled = true;
setup();
config.snapshotEnabled = true;
setup({ meta: { canEdit: true } });
expect(await screen.findByTestId(selector.shareInternally)).toBeInTheDocument();
expect(await screen.findByTestId(selector.shareExternally)).toBeInTheDocument();
expect(await screen.findByTestId(selector.shareSnapshot)).toBeInTheDocument();
});
it('should no share externally when public dashboard is disabled', async () => {
it('should not share externally when public dashboard is disabled', async () => {
config.featureToggles.publicDashboards = false;
config.publicDashboardsEnabled = false;
setup();
expect(await screen.queryByTestId(selector.shareExternally)).not.toBeInTheDocument();
expect(screen.queryByTestId(selector.shareExternally)).not.toBeInTheDocument();
});
describe('ShareSnapshot', () => {
it('should not share snapshot when user is not signed in', async () => {
config.snapshotEnabled = true;
Object.defineProperty(contextSrv, 'isSignedIn', {
value: false,
});
setup({ meta: { canEdit: true } });
expect(screen.queryByTestId(selector.shareSnapshot)).not.toBeInTheDocument();
});
it('should not share snapshot when snapshot is not enabled', async () => {
Object.defineProperty(contextSrv, 'isSignedIn', {
value: true,
});
config.snapshotEnabled = false;
setup({ meta: { canEdit: true } });
expect(screen.queryByTestId(selector.shareSnapshot)).not.toBeInTheDocument();
});
it('should not share snapshot when dashboard cannot edit', async () => {
Object.defineProperty(contextSrv, 'isSignedIn', {
value: true,
});
config.snapshotEnabled = true;
setup({ meta: { canEdit: false } });
expect(screen.queryByTestId(selector.shareSnapshot)).not.toBeInTheDocument();
});
});
});
function setup() {
function setup(overrides?: Partial<DashboardSceneState>) {
const panel = new VizPanel({
title: 'Panel A',
pluginId: 'table',
@ -58,6 +99,7 @@ function setup() {
}),
],
}),
...overrides,
});
render(<ShareMenu dashboard={dashboard} />);

View File

@ -1,6 +1,8 @@
import { selectors as e2eSelectors } from '@grafana/e2e-selectors';
import { config } from '@grafana/runtime';
import { VizPanel } from '@grafana/scenes';
import { Menu } from '@grafana/ui';
import { contextSrv } from 'app/core/core';
import { t } from 'app/core/internationalization';
import { isPublicDashboardsEnabled } from '../../../dashboard/components/ShareModal/SharePublicDashboard/SharePublicDashboardUtils';
@ -58,12 +60,14 @@ export default function ShareMenu({ dashboard, panel }: { dashboard: DashboardSc
onClick={onShareExternallyClick}
/>
)}
<Menu.Item
testId={newShareButtonSelector.shareSnapshot}
label={t('share-dashboard.menu.share-snapshot-title', 'Share snapshot')}
icon="camera"
onClick={onShareSnapshotClick}
/>
{contextSrv.isSignedIn && config.snapshotEnabled && dashboard.canEditDashboard() && (
<Menu.Item
testId={newShareButtonSelector.shareSnapshot}
label={t('share-dashboard.menu.share-snapshot-title', 'Share snapshot')}
icon="camera"
onClick={onShareSnapshotClick}
/>
)}
</Menu>
);
}