Files
grafana/public/test/specs/helpers.ts

222 lines
5.7 KiB
TypeScript
Raw Normal View History

import each from 'lodash/each';
import template from 'lodash/template';
2017-10-11 16:32:05 +02:00
import config from 'app/core/config';
import { dateMath } from '@grafana/data';
import { angularMocks, sinon } from '../lib/common';
import { PanelModel } from 'app/features/dashboard/state/PanelModel';
import { RawTimeRange } from '@grafana/data';
import { PanelPluginMeta } from '@grafana/data';
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
2017-10-11 16:32:05 +02:00
export function ControllerTestContext(this: any) {
const self = this;
2017-10-11 16:32:05 +02:00
this.datasource = {};
this.$element = {};
this.$sanitize = {};
2017-10-11 16:32:05 +02:00
this.annotationsSrv = {};
this.contextSrv = {};
2017-10-11 16:32:05 +02:00
this.timeSrv = new TimeSrvStub();
this.templateSrv = TemplateSrvStub();
2017-10-11 16:32:05 +02:00
this.datasourceSrv = {
getMetricSources: () => {},
get: () => {
2017-10-11 16:32:05 +02:00
return {
TimePicker: New time picker dropdown & custom range UI (#16811) * feat: Add new picker to DashNavTimeControls * chore: noImplicitAny limit reached * chore: noImplicityAny fix * chore: Add momentUtc helper to avoid the isUtc conditionals * chore: Move getRaw from Explore's time picker to grafana/ui utils and rename to getRawRange * feat: Use helper functions to convert utc to browser time * fix: Dont Select current value when pressing tab when using Time Picker * fix: Add tabIndex to time range inputs so tab works smoothly and prevent mouseDown event to propagate to react-select * fix: Add spacing to custom range labels * fix: Updated snapshot * fix: Re-adding getRaw() temporary to fix the build * fix: Disable scroll event in Popper when we're using the TimePicker so the popup wont "follow" the menu * fix: Move all "Last xxxx" quick ranges to the menu and show a "UTC" text when applicable * fix: Add zoom functionality * feat: Add logic to mark selected option as active * fix: Add tooltip to zoom button * fix: lint fix after rebase * chore: Remove old time picker from DashNav * TimePicker: minor design update * chore: Move all time picker quick ranges to the menu * fix: Remove the popover border-right, since the quick ranges are gone * chore: Remove function not in use * Fix: Close time picker on resize event * Fix: Remove border bottom * Fix: Use fa icons on prev/next arrows * Fix: Pass ref from TimePicker to TimePickerOptionGroup so the popover will align as it should * Fix: time picker ui adjustments to get better touch area on buttons * Fix: Dont increase line height on large screens * TimePicker: style updates * Fix: Add more prominent colors for selected dates and fade out dates in previous/next month * TimePicker: style updates2 * TimePicker: Big refactorings and style changes * Removed use of Popper not sure we need that here? * Made active selected item in the list have the "selected" checkmark * Changed design of popover * Changed design of and implementation of the Custom selection in the dropdown it did not feel like a item you could select like the rest now the list is just a normal list * TimePicker: Refactoring & style changes * TimePicker: use same date format everywhere * TimePicker: Calendar style updates * TimePicker: fixed unit test * fixed unit test * TimeZone: refactoring time zone type * TimePicker: refactoring * TimePicker: finally to UTC to work * TimePicker: better way to handle calendar utc dates * TimePicker: Fixed tooltip issues * Updated snapshot * TimePicker: moved tooltip from DashNavControls into TimePicker
2019-06-24 14:39:59 +02:00
then: (callback: (ds: any) => void) => {
2017-10-11 16:32:05 +02:00
callback(self.datasource);
},
};
},
};
this.isUtc = false;
2017-10-11 16:32:05 +02:00
this.providePhase = (mocks: any) => {
return angularMocks.module(($provide: any) => {
$provide.value('contextSrv', self.contextSrv);
2017-10-11 16:32:05 +02:00
$provide.value('datasourceSrv', self.datasourceSrv);
$provide.value('annotationsSrv', self.annotationsSrv);
$provide.value('timeSrv', self.timeSrv);
$provide.value('templateSrv', self.templateSrv);
$provide.value('$element', self.$element);
$provide.value('$sanitize', self.$sanitize);
each(mocks, (value: any, key: any) => {
2017-10-11 16:32:05 +02:00
$provide.value(key, value);
});
});
};
this.createPanelController = (Ctrl: any) => {
return angularMocks.inject(($controller: any, $rootScope: GrafanaRootScope, $location: any, $browser: any) => {
self.scope = $rootScope.$new();
self.$location = $location;
self.$browser = $browser;
self.panel = new PanelModel({ type: 'test' });
self.dashboard = { meta: {} };
self.isUtc = false;
self.dashboard.getTimezone = () => {
return self.isUtc ? 'utc' : 'browser';
};
$rootScope.appEvent = sinon.spy();
$rootScope.onAppEvent = sinon.spy();
$rootScope.colors = [];
2017-10-11 16:32:05 +02:00
for (let i = 0; i < 50; i++) {
$rootScope.colors.push('#' + i);
2017-10-11 16:32:05 +02:00
}
config.panels['test'] = { info: {} } as PanelPluginMeta;
self.ctrl = $controller(
Ctrl,
{ $scope: self.scope },
{
panel: self.panel,
dashboard: self.dashboard,
}
);
});
};
2017-10-11 16:32:05 +02:00
this.createControllerPhase = (controllerName: string) => {
return angularMocks.inject(($controller: any, $rootScope: GrafanaRootScope, $location: any, $browser: any) => {
self.scope = $rootScope.$new();
self.$location = $location;
self.$browser = $browser;
self.scope.contextSrv = {};
self.scope.panel = {};
self.scope.dashboard = { meta: {} };
self.scope.dashboardMeta = {};
self.scope.dashboardViewState = DashboardViewStateStub();
self.scope.appEvent = sinon.spy();
self.scope.onAppEvent = sinon.spy();
$rootScope.colors = [];
for (let i = 0; i < 50; i++) {
$rootScope.colors.push('#' + i);
}
self.scope.skipDataOnInit = true;
self.scope.skipAutoInit = true;
self.controller = $controller(controllerName, {
$scope: self.scope,
});
});
2017-10-11 16:32:05 +02:00
};
this.setIsUtc = (isUtc: any = false) => {
self.isUtc = isUtc;
};
2017-10-11 16:32:05 +02:00
}
export function ServiceTestContext(this: any) {
const self = this;
self.templateSrv = TemplateSrvStub();
2017-10-11 16:32:05 +02:00
self.timeSrv = new TimeSrvStub();
self.datasourceSrv = {};
self.backendSrv = {};
self.$routeParams = {};
this.providePhase = (mocks: any) => {
return angularMocks.module(($provide: any) => {
each(mocks, (key: string) => {
2017-10-11 16:32:05 +02:00
$provide.value(key, self[key]);
});
});
};
this.createService = (name: string) => {
// @ts-ignore
return angularMocks.inject(
($rootScope: GrafanaRootScope, $httpBackend: any, $injector: any, $location: any, $timeout: any) => {
self.$rootScope = $rootScope;
self.$httpBackend = $httpBackend;
self.$location = $location;
self.$rootScope.onAppEvent = () => {};
self.$rootScope.appEvent = () => {};
self.$timeout = $timeout;
self.service = $injector.get(name);
}
);
2017-10-11 16:32:05 +02:00
};
}
export function DashboardViewStateStub(this: any) {
this.registerPanel = () => {};
2017-10-11 16:32:05 +02:00
}
export class TimeSrvStub {
time: RawTimeRange;
constructor() {
this.time = { from: 'now-1h', to: 'now' };
}
init() {}
timeRange(parse: boolean) {
2017-10-11 16:32:05 +02:00
if (parse === false) {
return this.time;
}
return {
from: dateMath.parse(this.time.from, false),
to: dateMath.parse(this.time.to, true),
};
}
2017-10-11 16:32:05 +02:00
setTime(time: any) {
2017-10-11 16:32:05 +02:00
this.time = time;
}
2017-10-11 16:32:05 +02:00
}
export class ContextSrvStub {
isGrafanaVisible = jest.fn();
getValidInterval() {
return '10s';
}
hasRole() {
2017-10-11 16:32:05 +02:00
return true;
}
2017-10-11 16:32:05 +02:00
}
export function TemplateSrvStub(this: any) {
2017-10-11 16:32:05 +02:00
this.variables = [];
this.templateSettings = { interpolate: /\[\[([\s\S]+?)\]\]/g };
2017-10-11 16:32:05 +02:00
this.data = {};
this.replace = (text: string) => {
return template(text, this.templateSettings)(this.data);
2017-10-11 16:32:05 +02:00
};
this.init = () => {};
this.getAdhocFilters = (): any => {
2017-10-11 16:32:05 +02:00
return [];
};
this.fillVariableValuesForUrl = () => {};
this.updateIndex = () => {};
this.variableExists = () => {
2017-10-11 16:32:05 +02:00
return false;
};
this.variableInitialized = () => {};
this.highlightVariablesAsHtml = (str: string) => {
2017-10-11 16:32:05 +02:00
return str;
};
this.setGrafanaVariable = function(name: string, value: string) {
2017-10-11 16:32:05 +02:00
this.data[name] = value;
};
}
const allDeps = {
2018-07-03 11:55:23 +02:00
ContextSrvStub,
TemplateSrvStub,
TimeSrvStub,
ControllerTestContext,
ServiceTestContext,
DashboardViewStateStub,
2017-10-11 16:32:05 +02:00
};
// for legacy
export default allDeps;