From 3cbca06eefb275ea052d811c5655427a31cb128d Mon Sep 17 00:00:00 2001 From: Harjot Gill Date: Sat, 26 Sep 2026 15:49:08 -0700 Subject: [PATCH] fix(mfe): bind toolbar refresh to its dashboard owner --- .../DashNav/DashNavTimeControls.test.tsx | 81 +++++++++++++++++++ .../DashNav/DashNavTimeControls.tsx | 20 ++++- 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/public/app/features/dashboard/components/DashNav/DashNavTimeControls.test.tsx b/public/app/features/dashboard/components/DashNav/DashNavTimeControls.test.tsx index 2ce08413581fc..4f3fd58c11ec1 100644 --- a/public/app/features/dashboard/components/DashNav/DashNavTimeControls.test.tsx +++ b/public/app/features/dashboard/components/DashNav/DashNavTimeControls.test.tsx @@ -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; @@ -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((_, 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( diff --git a/public/app/features/dashboard/components/DashNav/DashNavTimeControls.tsx b/public/app/features/dashboard/components/DashNav/DashNavTimeControls.tsx index bccf5ce4ac5ea..af60d63b02786 100644 --- a/public/app/features/dashboard/components/DashNav/DashNavTimeControls.tsx +++ b/public/app/features/dashboard/components/DashNav/DashNavTimeControls.tsx @@ -1,7 +1,7 @@ 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'; @@ -9,6 +9,7 @@ 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'; @@ -40,6 +41,21 @@ export class DashNavTimeControls extends Component { }; 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(); }; @@ -92,7 +108,7 @@ export class DashNavTimeControls extends Component { if (this.props.onToolbarRefreshClick) { this.props.onToolbarRefreshClick(); } - this.onRefresh(); + return this.onRefresh(); }; render() {