The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/features/dashboard-scene/sharing/ShareButton/ShareMenu.test.tsx

106 lines
3.3 KiB

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, DashboardSceneState } from '../../scene/DashboardScene';
import ShareMenu from './ShareMenu';
const createAndCopyDashboardShortLinkMock = jest.fn();
jest.mock('app/core/utils/shortLinks', () => ({
...jest.requireActual('app/core/utils/shortLinks'),
createAndCopyDashboardShortLink: () => createAndCopyDashboardShortLinkMock(),
}));
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;
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 not share externally when public dashboard is disabled', async () => {
config.featureToggles.publicDashboards = false;
config.publicDashboardsEnabled = false;
setup();
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(overrides?: Partial<DashboardSceneState>) {
const panel = new VizPanel({
title: 'Panel A',
pluginId: 'table',
key: 'panel-12',
});
const dashboard = new DashboardScene({
title: 'hello',
uid: 'dash-1',
$timeRange: new SceneTimeRange({}),
body: new SceneGridLayout({
children: [
new DashboardGridItem({
key: 'griditem-1',
x: 0,
y: 0,
width: 10,
height: 12,
body: panel,
}),
],
}),
...overrides,
});
render(<ShareMenu dashboard={dashboard} />);
}