chore: Cleaning up implicit anys in manage_dashboard.ts and manage_dashboard.test.ts

progress: #14714
This commit is contained in:
Hugo Häggmark 2019-03-15 08:47:52 +01:00 committed by Hugo Häggmark
parent aa4b593dfa
commit 96af051cb2
2 changed files with 137 additions and 75 deletions

View File

@ -1,7 +1,30 @@
// @ts-ignore
import _ from 'lodash'; import _ from 'lodash';
import coreModule from 'app/core/core_module'; import coreModule from 'app/core/core_module';
import appEvents from 'app/core/app_events'; import appEvents from 'app/core/app_events';
import { SearchSrv } from 'app/core/services/search_srv'; import { SearchSrv } from 'app/core/services/search_srv';
import { BackendSrv } from 'app/core/services/backend_srv';
import { NavModelSrv } from 'app/core/nav_model_srv';
import { ContextSrv } from 'app/core/services/context_srv';
export interface Section {
id: number;
uid: string;
title: string;
expanded: false;
items: any[];
url: string;
icon: string;
score: number;
checked: boolean;
hideHeader: boolean;
toggle: Function;
}
export interface FoldersAndDashboardUids {
folderUids: string[];
dashboardUids: string[];
}
class Query { class Query {
query: string; query: string;
@ -14,7 +37,7 @@ class Query {
} }
export class ManageDashboardsCtrl { export class ManageDashboardsCtrl {
sections: any[]; sections: Section[];
query: Query; query: Query;
navModel: any; navModel: any;
@ -45,7 +68,12 @@ export class ManageDashboardsCtrl {
hasEditPermissionInFolders: boolean; hasEditPermissionInFolders: boolean;
/** @ngInject */ /** @ngInject */
constructor(private backendSrv, navModelSrv, private searchSrv: SearchSrv, private contextSrv) { constructor(
private backendSrv: BackendSrv,
navModelSrv: NavModelSrv,
private searchSrv: SearchSrv,
private contextSrv: ContextSrv
) {
this.isEditor = this.contextSrv.isEditor; this.isEditor = this.contextSrv.isEditor;
this.hasEditPermissionInFolders = this.contextSrv.hasEditPermissionInFolders; this.hasEditPermissionInFolders = this.contextSrv.hasEditPermissionInFolders;
@ -73,7 +101,7 @@ export class ManageDashboardsCtrl {
refreshList() { refreshList() {
return this.searchSrv return this.searchSrv
.search(this.query) .search(this.query)
.then(result => { .then((result: Section[]) => {
return this.initDashboardList(result); return this.initDashboardList(result);
}) })
.then(() => { .then(() => {
@ -81,7 +109,7 @@ export class ManageDashboardsCtrl {
return; return;
} }
return this.backendSrv.getFolderByUid(this.folderUid).then(folder => { return this.backendSrv.getFolderByUid(this.folderUid).then((folder: any) => {
this.canSave = folder.canSave; this.canSave = folder.canSave;
if (!this.canSave) { if (!this.canSave) {
this.hasEditPermissionInFolders = false; this.hasEditPermissionInFolders = false;
@ -90,7 +118,7 @@ export class ManageDashboardsCtrl {
}); });
} }
initDashboardList(result: any) { initDashboardList(result: Section[]) {
this.canMove = false; this.canMove = false;
this.canDelete = false; this.canDelete = false;
this.selectAllChecked = false; this.selectAllChecked = false;
@ -128,25 +156,25 @@ export class ManageDashboardsCtrl {
this.canDelete = selectedDashboards > 0 || selectedFolders > 0; this.canDelete = selectedDashboards > 0 || selectedFolders > 0;
} }
getFoldersAndDashboardsToDelete() { getFoldersAndDashboardsToDelete(): FoldersAndDashboardUids {
const selectedDashboards = { const selectedDashboards: FoldersAndDashboardUids = {
folders: [], folderUids: [],
dashboards: [], dashboardUids: [],
}; };
for (const section of this.sections) { for (const section of this.sections) {
if (section.checked && section.id !== 0) { if (section.checked && section.id !== 0) {
selectedDashboards.folders.push(section.uid); selectedDashboards.folderUids.push(section.uid);
} else { } else {
const selected = _.filter(section.items, { checked: true }); const selected = _.filter(section.items, { checked: true });
selectedDashboards.dashboards.push(..._.map(selected, 'uid')); selectedDashboards.dashboardUids.push(..._.map(selected, 'uid'));
} }
} }
return selectedDashboards; return selectedDashboards;
} }
getFolderIds(sections) { getFolderIds(sections: Section[]) {
const ids = []; const ids = [];
for (const s of sections) { for (const s of sections) {
if (s.checked) { if (s.checked) {
@ -158,8 +186,8 @@ export class ManageDashboardsCtrl {
delete() { delete() {
const data = this.getFoldersAndDashboardsToDelete(); const data = this.getFoldersAndDashboardsToDelete();
const folderCount = data.folders.length; const folderCount = data.folderUids.length;
const dashCount = data.dashboards.length; const dashCount = data.dashboardUids.length;
let text = 'Do you want to delete the '; let text = 'Do you want to delete the ';
let text2; let text2;
@ -179,12 +207,12 @@ export class ManageDashboardsCtrl {
icon: 'fa-trash', icon: 'fa-trash',
yesText: 'Delete', yesText: 'Delete',
onConfirm: () => { onConfirm: () => {
this.deleteFoldersAndDashboards(data.folders, data.dashboards); this.deleteFoldersAndDashboards(data.folderUids, data.dashboardUids);
}, },
}); });
} }
private deleteFoldersAndDashboards(folderUids, dashboardUids) { private deleteFoldersAndDashboards(folderUids: string[], dashboardUids: string[]) {
this.backendSrv.deleteFoldersAndDashboards(folderUids, dashboardUids).then(() => { this.backendSrv.deleteFoldersAndDashboards(folderUids, dashboardUids).then(() => {
this.refreshList(); this.refreshList();
}); });
@ -219,13 +247,13 @@ export class ManageDashboardsCtrl {
} }
initTagFilter() { initTagFilter() {
return this.searchSrv.getDashboardTags().then(results => { return this.searchSrv.getDashboardTags().then((results: any) => {
this.tagFilterOptions = [{ term: 'Filter By Tag', disabled: true }].concat(results); this.tagFilterOptions = [{ term: 'Filter By Tag', disabled: true }].concat(results);
this.selectedTagFilter = this.tagFilterOptions[0]; this.selectedTagFilter = this.tagFilterOptions[0];
}); });
} }
filterByTag(tag) { filterByTag(tag: any) {
if (_.indexOf(this.query.tag, tag) === -1) { if (_.indexOf(this.query.tag, tag) === -1) {
this.query.tag.push(tag); this.query.tag.push(tag);
} }
@ -243,7 +271,7 @@ export class ManageDashboardsCtrl {
return res; return res;
} }
removeTag(tag, evt) { removeTag(tag: any, evt: Event) {
this.query.tag = _.without(this.query.tag, tag); this.query.tag = _.without(this.query.tag, tag);
this.refreshList(); this.refreshList();
if (evt) { if (evt) {
@ -269,7 +297,7 @@ export class ManageDashboardsCtrl {
section.checked = this.selectAllChecked; section.checked = this.selectAllChecked;
} }
section.items = _.map(section.items, item => { section.items = _.map(section.items, (item: any) => {
item.checked = this.selectAllChecked; item.checked = this.selectAllChecked;
return item; return item;
}); });

View File

@ -1,12 +1,39 @@
import { ManageDashboardsCtrl } from 'app/core/components/manage_dashboards/manage_dashboards'; // @ts-ignore
import { SearchSrv } from 'app/core/services/search_srv';
import q from 'q'; import q from 'q';
import {
ManageDashboardsCtrl,
Section,
FoldersAndDashboardUids,
} from 'app/core/components/manage_dashboards/manage_dashboards';
import { SearchSrv } from 'app/core/services/search_srv';
import { BackendSrv } from '../services/backend_srv';
import { NavModelSrv } from '../nav_model_srv';
import { ContextSrv } from '../services/context_srv';
const mockSection = (overides?: object): Section => {
const defaultSection: Section = {
id: 0,
items: [],
checked: false,
expanded: false,
hideHeader: false,
icon: '',
score: 0,
title: 'Some Section',
toggle: jest.fn(),
uid: 'someuid',
url: '/some/url/',
};
return { ...defaultSection, ...overides };
};
describe('ManageDashboards', () => { describe('ManageDashboards', () => {
let ctrl; let ctrl: ManageDashboardsCtrl;
describe('when browsing dashboards', () => { describe('when browsing dashboards', () => {
beforeEach(() => { beforeEach(() => {
const tags: any[] = [];
const response = [ const response = [
{ {
id: 410, id: 410,
@ -18,11 +45,11 @@ describe('ManageDashboards', () => {
title: 'Dashboard Test', title: 'Dashboard Test',
url: 'dashboard/db/dashboard-test', url: 'dashboard/db/dashboard-test',
icon: 'fa fa-folder', icon: 'fa fa-folder',
tags: [], tags,
isStarred: false, isStarred: false,
}, },
], ],
tags: [], tags,
isStarred: false, isStarred: false,
}, },
{ {
@ -37,11 +64,11 @@ describe('ManageDashboards', () => {
title: 'Dashboard Test', title: 'Dashboard Test',
url: 'dashboard/db/dashboard-test', url: 'dashboard/db/dashboard-test',
icon: 'fa fa-folder', icon: 'fa fa-folder',
tags: [], tags,
isStarred: false, isStarred: false,
}, },
], ],
tags: [], tags,
isStarred: false, isStarred: false,
}, },
]; ];
@ -61,6 +88,7 @@ describe('ManageDashboards', () => {
describe('when browsing dashboards for a folder', () => { describe('when browsing dashboards for a folder', () => {
beforeEach(() => { beforeEach(() => {
const tags: any[] = [];
const response = [ const response = [
{ {
id: 410, id: 410,
@ -72,11 +100,11 @@ describe('ManageDashboards', () => {
title: 'Dashboard Test', title: 'Dashboard Test',
url: 'dashboard/db/dashboard-test', url: 'dashboard/db/dashboard-test',
icon: 'fa fa-folder', icon: 'fa fa-folder',
tags: [], tags,
isStarred: false, isStarred: false,
}, },
], ],
tags: [], tags,
isStarred: false, isStarred: false,
}, },
]; ];
@ -92,6 +120,7 @@ describe('ManageDashboards', () => {
describe('when searching dashboards', () => { describe('when searching dashboards', () => {
beforeEach(() => { beforeEach(() => {
const tags: any[] = [];
const response = [ const response = [
{ {
checked: false, checked: false,
@ -103,7 +132,7 @@ describe('ManageDashboards', () => {
title: 'Dashboard Test', title: 'Dashboard Test',
url: 'dashboard/db/dashboard-test', url: 'dashboard/db/dashboard-test',
icon: 'fa fa-folder', icon: 'fa fa-folder',
tags: [], tags,
isStarred: false, isStarred: false,
folderId: 410, folderId: 410,
folderUid: 'uid', folderUid: 'uid',
@ -115,7 +144,7 @@ describe('ManageDashboards', () => {
title: 'Dashboard Test', title: 'Dashboard Test',
url: 'dashboard/db/dashboard-test', url: 'dashboard/db/dashboard-test',
icon: 'fa fa-folder', icon: 'fa fa-folder',
tags: [], tags,
folderId: 499, folderId: 499,
isStarred: false, isStarred: false,
}, },
@ -245,7 +274,7 @@ describe('ManageDashboards', () => {
}); });
describe('when selecting dashboards', () => { describe('when selecting dashboards', () => {
let ctrl; let ctrl: ManageDashboardsCtrl;
beforeEach(() => { beforeEach(() => {
ctrl = createCtrlWithStubs([]); ctrl = createCtrlWithStubs([]);
@ -254,16 +283,16 @@ describe('ManageDashboards', () => {
describe('and no dashboards are selected', () => { describe('and no dashboards are selected', () => {
beforeEach(() => { beforeEach(() => {
ctrl.sections = [ ctrl.sections = [
{ mockSection({
id: 1, id: 1,
items: [{ id: 2, checked: false }], items: [{ id: 2, checked: false }],
checked: false, checked: false,
}, }),
{ mockSection({
id: 0, id: 0,
items: [{ id: 3, checked: false }], items: [{ id: 3, checked: false }],
checked: false, checked: false,
}, }),
]; ];
ctrl.selectionChanged(); ctrl.selectionChanged();
}); });
@ -302,16 +331,16 @@ describe('ManageDashboards', () => {
describe('and all folders and dashboards are selected', () => { describe('and all folders and dashboards are selected', () => {
beforeEach(() => { beforeEach(() => {
ctrl.sections = [ ctrl.sections = [
{ mockSection({
id: 1, id: 1,
items: [{ id: 2, checked: true }], items: [{ id: 2, checked: true }],
checked: true, checked: true,
}, }),
{ mockSection({
id: 0, id: 0,
items: [{ id: 3, checked: true }], items: [{ id: 3, checked: true }],
checked: true, checked: true,
}, }),
]; ];
ctrl.selectionChanged(); ctrl.selectionChanged();
}); });
@ -350,18 +379,18 @@ describe('ManageDashboards', () => {
describe('and one dashboard in root is selected', () => { describe('and one dashboard in root is selected', () => {
beforeEach(() => { beforeEach(() => {
ctrl.sections = [ ctrl.sections = [
{ mockSection({
id: 1, id: 1,
title: 'folder', title: 'folder',
items: [{ id: 2, checked: false }], items: [{ id: 2, checked: false }],
checked: false, checked: false,
}, }),
{ mockSection({
id: 0, id: 0,
title: 'General', title: 'General',
items: [{ id: 3, checked: true }], items: [{ id: 3, checked: true }],
checked: false, checked: false,
}, }),
]; ];
ctrl.selectionChanged(); ctrl.selectionChanged();
}); });
@ -378,18 +407,18 @@ describe('ManageDashboards', () => {
describe('and one child dashboard is selected', () => { describe('and one child dashboard is selected', () => {
beforeEach(() => { beforeEach(() => {
ctrl.sections = [ ctrl.sections = [
{ mockSection({
id: 1, id: 1,
title: 'folder', title: 'folder',
items: [{ id: 2, checked: true }], items: [{ id: 2, checked: true }],
checked: false, checked: false,
}, }),
{ mockSection({
id: 0, id: 0,
title: 'General', title: 'General',
items: [{ id: 3, checked: false }], items: [{ id: 3, checked: false }],
checked: false, checked: false,
}, }),
]; ];
ctrl.selectionChanged(); ctrl.selectionChanged();
@ -407,18 +436,18 @@ describe('ManageDashboards', () => {
describe('and one child dashboard and one dashboard is selected', () => { describe('and one child dashboard and one dashboard is selected', () => {
beforeEach(() => { beforeEach(() => {
ctrl.sections = [ ctrl.sections = [
{ mockSection({
id: 1, id: 1,
title: 'folder', title: 'folder',
items: [{ id: 2, checked: true }], items: [{ id: 2, checked: true }],
checked: false, checked: false,
}, }),
{ mockSection({
id: 0, id: 0,
title: 'General', title: 'General',
items: [{ id: 3, checked: true }], items: [{ id: 3, checked: true }],
checked: false, checked: false,
}, }),
]; ];
ctrl.selectionChanged(); ctrl.selectionChanged();
@ -436,24 +465,24 @@ describe('ManageDashboards', () => {
describe('and one child dashboard and one folder is selected', () => { describe('and one child dashboard and one folder is selected', () => {
beforeEach(() => { beforeEach(() => {
ctrl.sections = [ ctrl.sections = [
{ mockSection({
id: 1, id: 1,
title: 'folder', title: 'folder',
items: [{ id: 2, checked: false }], items: [{ id: 2, checked: false }],
checked: true, checked: true,
}, }),
{ mockSection({
id: 3, id: 3,
title: 'folder', title: 'folder',
items: [{ id: 4, checked: true }], items: [{ id: 4, checked: true }],
checked: false, checked: false,
}, }),
{ mockSection({
id: 0, id: 0,
title: 'General', title: 'General',
items: [{ id: 3, checked: false }], items: [{ id: 3, checked: false }],
checked: false, checked: false,
}, }),
]; ];
ctrl.selectionChanged(); ctrl.selectionChanged();
@ -470,55 +499,55 @@ describe('ManageDashboards', () => {
}); });
describe('when deleting dashboards', () => { describe('when deleting dashboards', () => {
let toBeDeleted: any; let toBeDeleted: FoldersAndDashboardUids;
beforeEach(() => { beforeEach(() => {
ctrl = createCtrlWithStubs([]); ctrl = createCtrlWithStubs([]);
ctrl.sections = [ ctrl.sections = [
{ mockSection({
id: 1, id: 1,
uid: 'folder', uid: 'folder',
title: 'folder', title: 'folder',
items: [{ id: 2, checked: true, uid: 'folder-dash' }], items: [{ id: 2, checked: true, uid: 'folder-dash' }],
checked: true, checked: true,
}, }),
{ mockSection({
id: 3, id: 3,
title: 'folder-2', title: 'folder-2',
items: [{ id: 3, checked: true, uid: 'folder-2-dash' }], items: [{ id: 3, checked: true, uid: 'folder-2-dash' }],
checked: false, checked: false,
uid: 'folder-2', uid: 'folder-2',
}, }),
{ mockSection({
id: 0, id: 0,
title: 'General', title: 'General',
items: [{ id: 3, checked: true, uid: 'root-dash' }], items: [{ id: 3, checked: true, uid: 'root-dash' }],
checked: true, checked: true,
}, }),
]; ];
toBeDeleted = ctrl.getFoldersAndDashboardsToDelete(); toBeDeleted = ctrl.getFoldersAndDashboardsToDelete();
}); });
it('should return 1 folder', () => { it('should return 1 folder', () => {
expect(toBeDeleted.folders.length).toEqual(1); expect(toBeDeleted.folderUids.length).toEqual(1);
}); });
it('should return 2 dashboards', () => { it('should return 2 dashboards', () => {
expect(toBeDeleted.dashboards.length).toEqual(2); expect(toBeDeleted.dashboardUids.length).toEqual(2);
}); });
it('should filter out children if parent is checked', () => { it('should filter out children if parent is checked', () => {
expect(toBeDeleted.folders[0]).toEqual('folder'); expect(toBeDeleted.folderUids[0]).toEqual('folder');
}); });
it('should not filter out children if parent not is checked', () => { it('should not filter out children if parent not is checked', () => {
expect(toBeDeleted.dashboards[0]).toEqual('folder-2-dash'); expect(toBeDeleted.dashboardUids[0]).toEqual('folder-2-dash');
}); });
it('should not filter out children if parent is checked and root', () => { it('should not filter out children if parent is checked and root', () => {
expect(toBeDeleted.dashboards[1]).toEqual('root-dash'); expect(toBeDeleted.dashboardUids[1]).toEqual('root-dash');
}); });
}); });
@ -527,19 +556,19 @@ describe('ManageDashboards', () => {
ctrl = createCtrlWithStubs([]); ctrl = createCtrlWithStubs([]);
ctrl.sections = [ ctrl.sections = [
{ mockSection({
id: 1, id: 1,
title: 'folder', title: 'folder',
items: [{ id: 2, checked: true, uid: 'dash' }], items: [{ id: 2, checked: true, uid: 'dash' }],
checked: false, checked: false,
uid: 'folder', uid: 'folder',
}, }),
{ mockSection({
id: 0, id: 0,
title: 'General', title: 'General',
items: [{ id: 3, checked: true, uid: 'dash-2' }], items: [{ id: 3, checked: true, uid: 'dash-2' }],
checked: false, checked: false,
}, }),
]; ];
}); });
@ -562,5 +591,10 @@ function createCtrlWithStubs(searchResponse: any, tags?: any) {
}, },
}; };
return new ManageDashboardsCtrl({}, { getNav: () => {} }, searchSrvStub as SearchSrv, { isEditor: true }); return new ManageDashboardsCtrl(
{} as BackendSrv,
{ getNav: () => {} } as NavModelSrv,
searchSrvStub as SearchSrv,
{ isEditor: true } as ContextSrv
);
} }