Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { render } from '@testing-library/react';

import { mfeDispatch, mfeGetStoreState, updateRenderingDashboardUID } from 'app/store/configureMfeStore';

import { getDashboardModel } from '../../../../../test/helpers/getDashboardModel';
import { getTimeSrv } from '../../services/TimeSrv';
import { DashboardModel } from '../../state/DashboardModel';
import { PanelModel } from '../../state/PanelModel';

import { DashNavTimeControls } from './DashNavTimeControls';

jest.unmock('@grafana/scenes');
jest.unmock('@grafana/data');
jest.unmock('@grafana/runtime');

describe('DashNavTimeControls', () => {
let dashboardModel: DashboardModel;

Expand All @@ -32,6 +39,80 @@ describe('DashNavTimeControls', () => {
dashboardModel = getDashboardModel(json);
});

afterEach(() => {
jest.restoreAllMocks();
mfeDispatch(updateRenderingDashboardUID(''));
});

it('refreshes the embedded toolbar owner rather than a stale singleton model', async () => {
dashboardModel.uid = 'toolbar-owner';
mfeDispatch(updateRenderingDashboardUID('other-dashboard'));
const singletonRefresh = jest.spyOn(getTimeSrv(), 'refreshTimeModel').mockImplementation(() => {});
const ownerRefresh = jest.spyOn(dashboardModel, 'timeRangeUpdated').mockImplementation(() => {
expect(mfeGetStoreState().fnGlobalReducer.renderingDashboardUID).toBe('toolbar-owner');
return Promise.resolve();
});
const controls = new DashNavTimeControls({
dashboard: dashboardModel,
isFnDashboard: true,
onChangeTimeZone: jest.fn(),
});

await controls.onRefresh();

expect(ownerRefresh).toHaveBeenCalledTimes(1);
expect(singletonRefresh).not.toHaveBeenCalled();
expect(mfeGetStoreState().fnGlobalReducer.renderingDashboardUID).toBe('other-dashboard');
});

it('restores the previous owner when embedded refresh dispatch throws', () => {
dashboardModel.uid = 'toolbar-owner';
mfeDispatch(updateRenderingDashboardUID('other-dashboard'));
jest.spyOn(dashboardModel, 'timeRangeUpdated').mockImplementation(() => {
throw new Error('dispatch failed');
});
const controls = new DashNavTimeControls({
dashboard: dashboardModel,
isFnDashboard: true,
onChangeTimeZone: jest.fn(),
});

expect(() => controls.onRefresh()).toThrow('dispatch failed');
expect(mfeGetStoreState().fnGlobalReducer.renderingDashboardUID).toBe('other-dashboard');
});

it('restores ownership before an asynchronous refresh settles and preserves its rejection', async () => {
dashboardModel.uid = 'toolbar-owner';
mfeDispatch(updateRenderingDashboardUID('other-dashboard'));
let rejectRefresh!: (error: Error) => void;
const pending = new Promise<void>((_, reject) => {
rejectRefresh = reject;
});
jest.spyOn(dashboardModel, 'timeRangeUpdated').mockReturnValue(pending);
const controls = new DashNavTimeControls({
dashboard: dashboardModel,
isFnDashboard: true,
onChangeTimeZone: jest.fn(),
});

const result = controls.onRefreshClick();
expect(result).toBe(pending);
expect(mfeGetStoreState().fnGlobalReducer.renderingDashboardUID).toBe('other-dashboard');
rejectRefresh(new Error('refresh failed'));
await expect(result).rejects.toThrow('refresh failed');
});

it('keeps standalone refresh on the time service', async () => {
const singletonRefresh = jest.spyOn(getTimeSrv(), 'refreshTimeModel').mockImplementation(() => {});
const ownerRefresh = jest.spyOn(dashboardModel, 'timeRangeUpdated');
const controls = new DashNavTimeControls({ dashboard: dashboardModel, onChangeTimeZone: jest.fn() });

await controls.onRefresh();

expect(singletonRefresh).toHaveBeenCalledTimes(1);
expect(ownerRefresh).not.toHaveBeenCalled();
});

it('renders RefreshPicker with run button in panel view', () => {
const container = render(
<DashNavTimeControls dashboard={dashboardModel} onChangeTimeZone={jest.fn()} key="time-controls" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Component } from 'react';
import { Unsubscribable } from 'rxjs';

import { dateMath, TimeRange, TimeZone } from '@grafana/data';
import { dateMath, rangeUtil, TimeRange, TimeZone } from '@grafana/data';
import { config, TimeRangeUpdatedEvent } from '@grafana/runtime';
import { defaultIntervals, getWeekStart, RefreshPicker } from '@grafana/ui';
import { TimePickerWithHistory } from 'app/core/components/TimePicker/TimePickerWithHistory';
import { appEvents } from 'app/core/core';
import { t } from 'app/core/internationalization';
import { AutoRefreshInterval } from 'app/core/services/context_srv';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { mfeDispatch, mfeGetStoreState, updateRenderingDashboardUID } from 'app/store/configureMfeStore';

import { ShiftTimeEvent, ShiftTimeEventDirection, ZoomOutEvent } from '../../../../types/events';
import { DashboardModel } from '../../state';
Expand Down Expand Up @@ -40,6 +41,21 @@ export class DashNavTimeControls extends Component<Props> {
};

onRefresh = () => {
if (this.props.isFnDashboard) {
const { dashboard } = this.props;
const previousOwner = mfeGetStoreState().fnGlobalReducer.renderingDashboardUID;
try {
// Bind the synchronous thunk dispatch to this toolbar's dashboard, not
// a singleton left pointing at a different (or closed) portal. Restore
// immediately: the thunk retains its own dispatch/getState after await.
mfeDispatch(updateRenderingDashboardUID(dashboard.uid));
return dashboard.timeRangeUpdated(
rangeUtil.convertRawToRange(dashboard.time, dashboard.getTimezone(), dashboard.fiscalYearStartMonth)
);
} finally {
mfeDispatch(updateRenderingDashboardUID(previousOwner));
}
}
getTimeSrv().refreshTimeModel();
return Promise.resolve();
};
Expand Down Expand Up @@ -92,7 +108,7 @@ export class DashNavTimeControls extends Component<Props> {
if (this.props.onToolbarRefreshClick) {
this.props.onToolbarRefreshClick();
}
this.onRefresh();
return this.onRefresh();
};

render() {
Expand Down
Loading