diff --git a/public/app/features/dashboard-scene/sharing/ShareButton/ShareMenu.test.tsx b/public/app/features/dashboard-scene/sharing/ShareButton/ShareMenu.test.tsx index 0da03f45dda..525ece5f205 100644 --- a/public/app/features/dashboard-scene/sharing/ShareButton/ShareMenu.test.tsx +++ b/public/app/features/dashboard-scene/sharing/ShareButton/ShareMenu.test.tsx @@ -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) { const panel = new VizPanel({ title: 'Panel A', pluginId: 'table', @@ -58,6 +99,7 @@ function setup() { }), ], }), + ...overrides, }); render(); diff --git a/public/app/features/dashboard-scene/sharing/ShareButton/ShareMenu.tsx b/public/app/features/dashboard-scene/sharing/ShareButton/ShareMenu.tsx index 9ccf3be0654..6177ca3e94b 100644 --- a/public/app/features/dashboard-scene/sharing/ShareButton/ShareMenu.tsx +++ b/public/app/features/dashboard-scene/sharing/ShareButton/ShareMenu.tsx @@ -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} /> )} - + {contextSrv.isSignedIn && config.snapshotEnabled && dashboard.canEditDashboard() && ( + + )} ); }