Types: Adds type safety to appEvents (#19418)

* Types: Add type safety to appEvents
This commit is contained in:
kay delaney
2019-10-14 09:27:47 +01:00
committed by GitHub
parent e7c37cc316
commit 99411bf37a
138 changed files with 991 additions and 508 deletions

View File

@@ -3,6 +3,7 @@ import { HistoryListCtrl } from './HistoryListCtrl';
import { versions, compare, restore } from './__mocks__/history';
// @ts-ignore
import $q from 'q';
import { CoreEvents } from 'app/types';
describe('HistoryListCtrl', () => {
const RESTORE_ID = 4;
@@ -118,9 +119,9 @@ describe('HistoryListCtrl', () => {
historyListCtrl.resetFromSource = jest.fn();
});
it('should listen for the `dashboard-saved` appEvent', () => {
it('should listen for the `dashboardSaved` appEvent', () => {
expect($rootScope.onAppEvent).toHaveBeenCalledTimes(1);
expect($rootScope.onAppEvent.mock.calls[0][0]).toBe('dashboard-saved');
expect($rootScope.onAppEvent.mock.calls[0][0]).toBe(CoreEvents.dashboardSaved);
});
it('should call `onDashboardSaved` when the appEvent is received', () => {
@@ -292,7 +293,7 @@ describe('HistoryListCtrl', () => {
it('should display a modal allowing the user to restore or cancel', () => {
expect($rootScope.appEvent).toHaveBeenCalledTimes(1);
expect($rootScope.appEvent.mock.calls[0][0]).toBe('confirm-modal');
expect($rootScope.appEvent.mock.calls[0][0]).toBe(CoreEvents.showConfirmModal);
});
describe('and restore fails to fetch', () => {

View File

@@ -4,7 +4,9 @@ import angular, { ILocationService, IQService } from 'angular';
import locationUtil from 'app/core/utils/location_util';
import { DashboardModel } from '../../state/DashboardModel';
import { HistoryListOpts, RevisionsModel, CalculateDiffOptions, HistorySrv } from './HistorySrv';
import { dateTime, toUtc, DateTimeInput } from '@grafana/data';
import { dateTime, toUtc, DateTimeInput, AppEvents } from '@grafana/data';
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
import { CoreEvents } from 'app/types';
export class HistoryListCtrl {
appending: boolean;
@@ -25,7 +27,7 @@ export class HistoryListCtrl {
/** @ngInject */
constructor(
private $route: any,
private $rootScope: any,
private $rootScope: GrafanaRootScope,
private $location: ILocationService,
private $q: IQService,
private historySrv: HistorySrv,
@@ -40,7 +42,7 @@ export class HistoryListCtrl {
this.start = 0;
this.canCompare = false;
this.$rootScope.onAppEvent('dashboard-saved', this.onDashboardSaved.bind(this), $scope);
this.$rootScope.onAppEvent(CoreEvents.dashboardSaved, this.onDashboardSaved.bind(this), $scope);
this.resetFromSource();
}
@@ -56,7 +58,7 @@ export class HistoryListCtrl {
}
dismiss() {
this.$rootScope.appEvent('hide-dash-editor');
this.$rootScope.appEvent(CoreEvents.hideDashEditor);
}
addToLog() {
@@ -172,7 +174,7 @@ export class HistoryListCtrl {
}
restore(version: number) {
this.$rootScope.appEvent('confirm-modal', {
this.$rootScope.appEvent(CoreEvents.showConfirmModal, {
title: 'Restore version',
text: '',
text2: `Are you sure you want to restore the dashboard to version ${version}? All unsaved changes will be lost.`,
@@ -189,7 +191,7 @@ export class HistoryListCtrl {
.then((response: any) => {
this.$location.url(locationUtil.stripBaseFromUrl(response.url)).replace();
this.$route.reload();
this.$rootScope.appEvent('alert-success', ['Dashboard restored', 'Restored from version ' + version]);
this.$rootScope.appEvent(AppEvents.alertSuccess, ['Dashboard restored', 'Restored from version ' + version]);
})
.catch(() => {
this.mode = 'list';