From b84aab5add715df7051a53a11c76bc37e8a3d500 Mon Sep 17 00:00:00 2001 From: Josh Hunt Date: Tue, 12 Sep 2023 09:34:43 +0000 Subject: [PATCH] Notifications: Don't show toasts after refreshing (#74712) * user essentials mob! :trident: lastFile:public/app/core/reducers/appNotification.ts * user essentials mob! :trident: --------- Co-authored-by: eledobleefe Co-authored-by: Joao Silva Co-authored-by: Ashley Harrison Co-authored-by: L-M-K-B <48948963+L-M-K-B@users.noreply.github.com> --- .../app/core/reducers/appNotification.test.ts | 66 +++++++++++++++++++ public/app/core/reducers/appNotification.ts | 4 +- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/public/app/core/reducers/appNotification.test.ts b/public/app/core/reducers/appNotification.test.ts index 33a12aa5f46..6094e1ac7bc 100644 --- a/public/app/core/reducers/appNotification.test.ts +++ b/public/app/core/reducers/appNotification.test.ts @@ -178,4 +178,70 @@ describe('notify', () => { expect(result).toEqual(expectedResult); }); + + it('persists notifications to localStorage', () => { + const setItemSpy = jest.spyOn(Storage.prototype, 'setItem'); + + const id1 = '696da53b-6ae7-4824-9e0e-d6a3b54a2c74'; + const id2 = '4477fcd9-246c-45a5-8818-e22a16683dae'; + + const initialState: AppNotificationsState = { + byId: { + [id1]: { + id: id1, + severity: AppNotificationSeverity.Warning, + icon: 'warning', + title: 'test', + text: 'test alert', + showing: false, + timestamp, + }, + }, + lastRead: timestamp - 10, + }; + + appNotificationsReducer( + initialState, + notifyApp({ + id: id2, + severity: AppNotificationSeverity.Error, + icon: 'error', + title: 'test2', + text: 'test alert info 2', + showing: true, + timestamp: 1649802870373, + }) + ); + + expect(setItemSpy).toHaveBeenCalledTimes(1); + + const [calledKey, calledValue] = setItemSpy.mock.calls[0]; + const parsedJsonCalledValue = JSON.parse(calledValue); + expect(calledKey).toBe('notifications'); + + // Assert showing toasts are not still showing after page refresh + expect(parsedJsonCalledValue[id1].showing).toBeFalsy(); // old notification that was false anyway + expect(parsedJsonCalledValue[id2].showing).toBeFalsy(); // new notification that we store as false + + expect(parsedJsonCalledValue).toEqual({ + [id1]: { + id: id1, + severity: AppNotificationSeverity.Warning, + icon: 'warning', + title: 'test', + text: 'test alert', + showing: false, + timestamp, + }, + [id2]: { + id: id2, + severity: AppNotificationSeverity.Error, + icon: 'error', + title: 'test2', + text: 'test alert info 2', + showing: false, + timestamp: 1649802870373, + }, + }); + }); }); diff --git a/public/app/core/reducers/appNotification.ts b/public/app/core/reducers/appNotification.ts index 346fa8a7cb5..5f88fd41e99 100644 --- a/public/app/core/reducers/appNotification.ts +++ b/public/app/core/reducers/appNotification.ts @@ -109,7 +109,9 @@ function serializeNotifications(notifs: Record) { text: cur.text, traceId: cur.traceId, timestamp: cur.timestamp, - showing: cur.showing, + // we don't care about still showing toasts after refreshing + // https://github.com/grafana/grafana/issues/71932 + showing: false, }; return prev;