From f43d834a5949dc25d50d05c48046ed1d82b367bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Tue, 9 Feb 2021 15:29:50 +0100 Subject: [PATCH] Alerting: Fixes so notification channels are properly deleted (#31040) --- .../features/alerting/AlertTabCtrl.test.ts | 81 +++++++++++++++++++ public/app/features/alerting/AlertTabCtrl.ts | 6 +- 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 public/app/features/alerting/AlertTabCtrl.test.ts diff --git a/public/app/features/alerting/AlertTabCtrl.test.ts b/public/app/features/alerting/AlertTabCtrl.test.ts new file mode 100644 index 00000000000..6b02d1a4d7e --- /dev/null +++ b/public/app/features/alerting/AlertTabCtrl.test.ts @@ -0,0 +1,81 @@ +import { AlertTabCtrl } from './AlertTabCtrl'; + +interface Args { + notifications?: Array<{ uid?: string; id?: number; isDefault: boolean }>; +} + +function setupTestContext({ notifications = [] }: Args = {}) { + const panel = { + alert: { notifications }, + options: [], + title: 'Testing Alerts', + }; + const $scope = { + ctrl: { + panel, + render: jest.fn(), + }, + }; + const dashboardSrv: any = {}; + const uiSegmentSrv: any = {}; + const datasourceSrv: any = {}; + + const controller = new AlertTabCtrl($scope, dashboardSrv, uiSegmentSrv, datasourceSrv); + controller.notifications = notifications; + controller.alertNotifications = []; + controller.initModel(); + + return { controller }; +} + +describe('AlertTabCtrl', () => { + describe('when removeNotification is called with an uid', () => { + it('then the correct notifier should be removed', () => { + const { controller } = setupTestContext({ + notifications: [ + { id: 1, uid: 'one', isDefault: true }, + { id: 2, uid: 'two', isDefault: false }, + ], + }); + + expect(controller.alert.notifications).toEqual([ + { id: 1, uid: 'one', isDefault: true, iconClass: 'bell' }, + { id: 2, uid: 'two', isDefault: false, iconClass: 'bell' }, + ]); + expect(controller.alertNotifications).toEqual([ + { id: 2, uid: 'two', isDefault: false, iconClass: 'bell' }, + { id: 1, uid: 'one', isDefault: true, iconClass: 'bell' }, + ]); + + controller.removeNotification({ uid: 'one' }); + + expect(controller.alert.notifications).toEqual([{ id: 2, uid: 'two', isDefault: false, iconClass: 'bell' }]); + expect(controller.alertNotifications).toEqual([{ id: 2, uid: 'two', isDefault: false, iconClass: 'bell' }]); + }); + }); + + describe('when removeNotification is called with an id', () => { + it('then the correct notifier should be removed', () => { + const { controller } = setupTestContext({ + notifications: [ + { id: 1, uid: 'one', isDefault: true }, + { id: 2, uid: 'two', isDefault: false }, + ], + }); + + expect(controller.alert.notifications).toEqual([ + { id: 1, uid: 'one', isDefault: true, iconClass: 'bell' }, + { id: 2, uid: 'two', isDefault: false, iconClass: 'bell' }, + ]); + expect(controller.alertNotifications).toEqual([ + { id: 2, uid: 'two', isDefault: false, iconClass: 'bell' }, + { id: 1, uid: 'one', isDefault: true, iconClass: 'bell' }, + ]); + + controller.removeNotification({ id: 2 }); + + expect(controller.alert.notifications).toEqual([{ id: 1, uid: 'one', isDefault: true, iconClass: 'bell' }]); + expect(controller.alertNotifications).toEqual([{ id: 1, uid: 'one', isDefault: true, iconClass: 'bell' }]); + }); + }); +}); diff --git a/public/app/features/alerting/AlertTabCtrl.ts b/public/app/features/alerting/AlertTabCtrl.ts index 8745eeb7d56..66608f7ae06 100644 --- a/public/app/features/alerting/AlertTabCtrl.ts +++ b/public/app/features/alerting/AlertTabCtrl.ts @@ -165,10 +165,10 @@ export class AlertTabCtrl { } removeNotification(an: any) { - // remove notifiers refeered to by id and uid to support notifiers added + // remove notifiers referred to by id and uid to support notifiers added // before and after we added support for uid - _.remove(this.alert.notifications, (n: any) => n.uid === an.uid); - _.remove(this.alertNotifications, (n: any) => n.uid === an.uid); + _.remove(this.alert.notifications, (n: any) => n.uid === an.uid || n.id === an.id); + _.remove(this.alertNotifications, (n: any) => n.uid === an.uid || n.id === an.id); } addAlertRuleTag() {