AlertTabCtrl.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { AlertTabCtrl } from './AlertTabCtrl';
  2. interface Args {
  3. notifications?: Array<{ uid?: string; id?: number; isDefault: boolean }>;
  4. }
  5. function setupTestContext({ notifications = [] }: Args = {}) {
  6. const panel = {
  7. alert: { notifications },
  8. options: [],
  9. title: 'Testing Alerts',
  10. };
  11. const $scope = {
  12. ctrl: {
  13. panel,
  14. render: jest.fn(),
  15. },
  16. };
  17. const dashboardSrv: any = {};
  18. const uiSegmentSrv: any = {};
  19. const datasourceSrv: any = {};
  20. const controller = new AlertTabCtrl($scope, dashboardSrv, uiSegmentSrv, datasourceSrv);
  21. controller.notifications = notifications;
  22. controller.alertNotifications = [];
  23. controller.initModel();
  24. return { controller };
  25. }
  26. describe('AlertTabCtrl', () => {
  27. describe('when removeNotification is called with an uid', () => {
  28. it('then the correct notifier should be removed', () => {
  29. const { controller } = setupTestContext({
  30. notifications: [
  31. { id: 1, uid: 'one', isDefault: true },
  32. { id: 2, uid: 'two', isDefault: false },
  33. ],
  34. });
  35. expect(controller.alert.notifications).toEqual([
  36. { id: 1, uid: 'one', isDefault: true, iconClass: 'bell' },
  37. { id: 2, uid: 'two', isDefault: false, iconClass: 'bell' },
  38. ]);
  39. expect(controller.alertNotifications).toEqual([
  40. { id: 2, uid: 'two', isDefault: false, iconClass: 'bell' },
  41. { id: 1, uid: 'one', isDefault: true, iconClass: 'bell' },
  42. ]);
  43. controller.removeNotification({ uid: 'one' });
  44. expect(controller.alert.notifications).toEqual([{ id: 2, uid: 'two', isDefault: false, iconClass: 'bell' }]);
  45. expect(controller.alertNotifications).toEqual([{ id: 2, uid: 'two', isDefault: false, iconClass: 'bell' }]);
  46. });
  47. });
  48. describe('when removeNotification is called with an id', () => {
  49. it('then the correct notifier should be removed', () => {
  50. const { controller } = setupTestContext({
  51. notifications: [
  52. { id: 1, uid: 'one', isDefault: true },
  53. { id: 2, uid: 'two', isDefault: false },
  54. ],
  55. });
  56. expect(controller.alert.notifications).toEqual([
  57. { id: 1, uid: 'one', isDefault: true, iconClass: 'bell' },
  58. { id: 2, uid: 'two', isDefault: false, iconClass: 'bell' },
  59. ]);
  60. expect(controller.alertNotifications).toEqual([
  61. { id: 2, uid: 'two', isDefault: false, iconClass: 'bell' },
  62. { id: 1, uid: 'one', isDefault: true, iconClass: 'bell' },
  63. ]);
  64. controller.removeNotification({ id: 2 });
  65. expect(controller.alert.notifications).toEqual([{ id: 1, uid: 'one', isDefault: true, iconClass: 'bell' }]);
  66. expect(controller.alertNotifications).toEqual([{ id: 1, uid: 'one', isDefault: true, iconClass: 'bell' }]);
  67. });
  68. });
  69. });