AnnoListPanel: Redirect to correct time on click (#41738)

This commit is contained in:
Uchechukwu Obasi
2021-11-16 12:58:58 +01:00
committed by GitHub
parent 0e76f347c3
commit 0da6ac2efa
2 changed files with 22 additions and 17 deletions
@@ -8,6 +8,7 @@ import { backendSrv } from '../../../core/services/backend_srv';
import userEvent from '@testing-library/user-event';
import { silenceConsoleOutput } from '../../../../test/core/utils/silenceConsoleOutput';
import { setDashboardSrv } from '../../../features/dashboard/services/DashboardSrv';
import { locationService } from '@grafana/runtime';
jest.mock('@grafana/runtime', () => ({
...((jest.requireActual('@grafana/runtime') as unknown) as object),
@@ -17,7 +18,7 @@ jest.mock('@grafana/runtime', () => ({
const defaultOptions: AnnoOptions = {
limit: 10,
navigateAfter: '10m',
navigateBefore: '20m',
navigateBefore: '10m',
navigateToPanel: true,
onlyFromThisDashboard: true,
onlyInTimeRange: false,
@@ -27,7 +28,7 @@ const defaultOptions: AnnoOptions = {
tags: ['tag A', 'tag B'],
};
const defaultResult: AnnotationEvent = {
const defaultResult: any = {
text: 'Result text',
userId: 1,
login: 'Result login',
@@ -37,6 +38,8 @@ const defaultResult: AnnotationEvent = {
time: Date.UTC(2021, 0, 1, 0, 0, 0, 0),
panelId: 13,
dashboardId: 14, // deliberately different from panelId
id: 14,
url: '/d/asdkjhajksd/some-dash',
};
async function setupTestContext({
@@ -51,6 +54,7 @@ async function setupTestContext({
const dash: any = { id: 1, formatDate: (time: number) => new Date(time).toISOString() };
const dashSrv: any = { getCurrent: () => dash };
setDashboardSrv(dashSrv);
const pushSpy = jest.spyOn(locationService, 'push');
const props: Props = {
data: { state: LoadingState.Done, timeRange: getDefaultTimeRange(), series: [] },
@@ -82,7 +86,7 @@ async function setupTestContext({
const { rerender } = render(<AnnoListPanel {...props} />);
await waitFor(() => expect(getMock).toHaveBeenCalledTimes(1));
return { props, rerender, getMock };
return { props, rerender, getMock, pushSpy };
}
describe('AnnoListPanel', () => {
@@ -192,14 +196,16 @@ describe('AnnoListPanel', () => {
describe('and the user clicks on the annotation', () => {
it('then it should navigate to the dashboard connected to the annotation', async () => {
const { getMock } = await setupTestContext();
const { getMock, pushSpy } = await setupTestContext();
getMock.mockClear();
expect(screen.getByText(/result text/i)).toBeInTheDocument();
userEvent.click(screen.getByText(/result text/i));
await waitFor(() => expect(getMock).toHaveBeenCalledTimes(1));
expect(getMock).toHaveBeenCalledTimes(1);
expect(getMock).toHaveBeenCalledWith('/api/search', { dashboardIds: 14 });
expect(pushSpy).toHaveBeenCalledTimes(1);
expect(pushSpy).toHaveBeenCalledWith('/d/asdkjhajksd/some-dash?from=1609458600000&to=1609459800000');
});
});
@@ -129,7 +129,7 @@ export class AnnoListPanel extends PureComponent<Props, State> {
});
}
onAnnoClick = (anno: AnnotationEvent) => {
onAnnoClick = async (anno: AnnotationEvent) => {
if (!anno.time) {
return;
}
@@ -152,17 +152,16 @@ export class AnnoListPanel extends PureComponent<Props, State> {
return;
}
getBackendSrv()
.get('/api/search', { dashboardIds: anno.dashboardId })
.then((res: any[]) => {
if (res && res.length && res[0].id === anno.dashboardId) {
const dash = res[0];
const newUrl = locationUtil.stripBaseFromUrl(dash.url);
locationService.push(newUrl);
return;
}
appEvents.emit(AppEvents.alertWarning, ['Unknown Dashboard: ' + anno.dashboardId]);
});
const result = await getBackendSrv().get('/api/search', { dashboardIds: anno.dashboardId });
if (result && result.length && result[0].id === anno.dashboardId) {
const dash = result[0];
const url = new URL(dash.url, window.location.origin);
url.searchParams.set('from', params.from);
url.searchParams.set('to', params.to);
locationService.push(locationUtil.stripBaseFromUrl(url.toString()));
return;
}
appEvents.emit(AppEvents.alertWarning, ['Unknown Dashboard: ' + anno.dashboardId]);
};
_timeOffset(time: number, offset: string, subtract = false): number {