Commit 8666c77c by Tobias Skarhed Committed by Torkel Ödegaard

Karma to Jest: time_srv (#12303)

* Begin Karma to Jest: time_srv

* Remove experimental fix

* location search stubs for each test

* Fix issue where time service was not available for other tests
parent 5bf72fc9
import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common';
import '../annotations_srv';
import helpers from 'test/specs/helpers';
import 'app/features/dashboard/time_srv';
describe('AnnotationsSrv', function() {
var ctx = new helpers.ServiceTestContext();
beforeEach(angularMocks.module('grafana.core'));
beforeEach(angularMocks.module('grafana.services'));
beforeEach(ctx.createService('timeSrv'));
beforeEach(() => {
ctx.createService('annotationsSrv');
});
describe('When translating the query result', () => {
const annotationSource = {
datasource: '-- Grafana --',
......
import { TimeSrv } from '../time_srv';
import '../time_srv';
import moment from 'moment';
describe('timeSrv', function() {
var rootScope = {
$on: jest.fn(),
onAppEvent: jest.fn(),
appEvent: jest.fn(),
};
var timer = {
register: jest.fn(),
cancel: jest.fn(),
cancelAll: jest.fn(),
};
var location = {
search: jest.fn(() => ({})),
};
var timeSrv;
var _dashboard: any = {
time: { from: 'now-6h', to: 'now' },
getTimezone: jest.fn(() => 'browser'),
};
beforeEach(function() {
timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
timeSrv.init(_dashboard);
});
describe('timeRange', function() {
it('should return unparsed when parse is false', function() {
timeSrv.setTime({ from: 'now', to: 'now-1h' });
var time = timeSrv.timeRange();
expect(time.raw.from).toBe('now');
expect(time.raw.to).toBe('now-1h');
});
it('should return parsed when parse is true', function() {
timeSrv.setTime({ from: 'now', to: 'now-1h' });
var time = timeSrv.timeRange();
expect(moment.isMoment(time.from)).toBe(true);
expect(moment.isMoment(time.to)).toBe(true);
});
});
describe('init time from url', function() {
it('should handle relative times', function() {
location = {
search: jest.fn(() => ({
from: 'now-2d',
to: 'now',
})),
};
timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
timeSrv.init(_dashboard);
var time = timeSrv.timeRange();
expect(time.raw.from).toBe('now-2d');
expect(time.raw.to).toBe('now');
});
it('should handle formatted dates', function() {
location = {
search: jest.fn(() => ({
from: '20140410T052010',
to: '20140520T031022',
})),
};
timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
timeSrv.init(_dashboard);
var time = timeSrv.timeRange();
expect(time.from.valueOf()).toEqual(new Date('2014-04-10T05:20:10Z').getTime());
expect(time.to.valueOf()).toEqual(new Date('2014-05-20T03:10:22Z').getTime());
});
it('should handle formatted dates without time', function() {
location = {
search: jest.fn(() => ({
from: '20140410',
to: '20140520',
})),
};
timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
timeSrv.init(_dashboard);
var time = timeSrv.timeRange();
expect(time.from.valueOf()).toEqual(new Date('2014-04-10T00:00:00Z').getTime());
expect(time.to.valueOf()).toEqual(new Date('2014-05-20T00:00:00Z').getTime());
});
it('should handle epochs', function() {
location = {
search: jest.fn(() => ({
from: '1410337646373',
to: '1410337665699',
})),
};
timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
timeSrv.init(_dashboard);
var time = timeSrv.timeRange();
expect(time.from.valueOf()).toEqual(1410337646373);
expect(time.to.valueOf()).toEqual(1410337665699);
});
it('should handle bad dates', function() {
location = {
search: jest.fn(() => ({
from: '20151126T00010%3C%2Fp%3E%3Cspan%20class',
to: 'now',
})),
};
timeSrv = new TimeSrv(rootScope, jest.fn(), location, timer, { isGrafanaVisibile: jest.fn() });
_dashboard.time.from = 'now-6h';
timeSrv.init(_dashboard);
expect(timeSrv.time.from).toEqual('now-6h');
expect(timeSrv.time.to).toEqual('now');
});
});
describe('setTime', function() {
it('should return disable refresh if refresh is disabled for any range', function() {
_dashboard.refresh = false;
timeSrv.setTime({ from: '2011-01-01', to: '2015-01-01' });
expect(_dashboard.refresh).toBe(false);
});
it('should restore refresh for absolute time range', function() {
_dashboard.refresh = '30s';
timeSrv.setTime({ from: '2011-01-01', to: '2015-01-01' });
expect(_dashboard.refresh).toBe('30s');
});
it('should restore refresh after relative time range is set', function() {
_dashboard.refresh = '10s';
timeSrv.setTime({
from: moment([2011, 1, 1]),
to: moment([2015, 1, 1]),
});
expect(_dashboard.refresh).toBe(false);
timeSrv.setTime({ from: '2011-01-01', to: 'now' });
expect(_dashboard.refresh).toBe('10s');
});
it('should keep refresh after relative time range is changed and now delay exists', function() {
_dashboard.refresh = '10s';
timeSrv.setTime({ from: 'now-1h', to: 'now-10s' });
expect(_dashboard.refresh).toBe('10s');
});
});
});
import { describe, beforeEach, it, expect, sinon, angularMocks } from 'test/lib/common';
import helpers from 'test/specs/helpers';
import '../time_srv';
import moment from 'moment';
describe('timeSrv', function() {
var ctx = new helpers.ServiceTestContext();
var _dashboard: any = {
time: { from: 'now-6h', to: 'now' },
getTimezone: sinon.stub().returns('browser'),
};
beforeEach(angularMocks.module('grafana.core'));
beforeEach(angularMocks.module('grafana.services'));
beforeEach(ctx.createService('timeSrv'));
beforeEach(function() {
ctx.service.init(_dashboard);
});
describe('timeRange', function() {
it('should return unparsed when parse is false', function() {
ctx.service.setTime({ from: 'now', to: 'now-1h' });
var time = ctx.service.timeRange();
expect(time.raw.from).to.be('now');
expect(time.raw.to).to.be('now-1h');
});
it('should return parsed when parse is true', function() {
ctx.service.setTime({ from: 'now', to: 'now-1h' });
var time = ctx.service.timeRange();
expect(moment.isMoment(time.from)).to.be(true);
expect(moment.isMoment(time.to)).to.be(true);
});
});
describe('init time from url', function() {
it('should handle relative times', function() {
ctx.$location.search({ from: 'now-2d', to: 'now' });
ctx.service.init(_dashboard);
var time = ctx.service.timeRange();
expect(time.raw.from).to.be('now-2d');
expect(time.raw.to).to.be('now');
});
it('should handle formatted dates', function() {
ctx.$location.search({ from: '20140410T052010', to: '20140520T031022' });
ctx.service.init(_dashboard);
var time = ctx.service.timeRange(true);
expect(time.from.valueOf()).to.equal(new Date('2014-04-10T05:20:10Z').getTime());
expect(time.to.valueOf()).to.equal(new Date('2014-05-20T03:10:22Z').getTime());
});
it('should handle formatted dates without time', function() {
ctx.$location.search({ from: '20140410', to: '20140520' });
ctx.service.init(_dashboard);
var time = ctx.service.timeRange(true);
expect(time.from.valueOf()).to.equal(new Date('2014-04-10T00:00:00Z').getTime());
expect(time.to.valueOf()).to.equal(new Date('2014-05-20T00:00:00Z').getTime());
});
it('should handle epochs', function() {
ctx.$location.search({ from: '1410337646373', to: '1410337665699' });
ctx.service.init(_dashboard);
var time = ctx.service.timeRange(true);
expect(time.from.valueOf()).to.equal(1410337646373);
expect(time.to.valueOf()).to.equal(1410337665699);
});
it('should handle bad dates', function() {
ctx.$location.search({
from: '20151126T00010%3C%2Fp%3E%3Cspan%20class',
to: 'now',
});
_dashboard.time.from = 'now-6h';
ctx.service.init(_dashboard);
expect(ctx.service.time.from).to.equal('now-6h');
expect(ctx.service.time.to).to.equal('now');
});
});
describe('setTime', function() {
it('should return disable refresh if refresh is disabled for any range', function() {
_dashboard.refresh = false;
ctx.service.setTime({ from: '2011-01-01', to: '2015-01-01' });
expect(_dashboard.refresh).to.be(false);
});
it('should restore refresh for absolute time range', function() {
_dashboard.refresh = '30s';
ctx.service.setTime({ from: '2011-01-01', to: '2015-01-01' });
expect(_dashboard.refresh).to.be('30s');
});
it('should restore refresh after relative time range is set', function() {
_dashboard.refresh = '10s';
ctx.service.setTime({
from: moment([2011, 1, 1]),
to: moment([2015, 1, 1]),
});
expect(_dashboard.refresh).to.be(false);
ctx.service.setTime({ from: '2011-01-01', to: 'now' });
expect(_dashboard.refresh).to.be('10s');
});
it('should keep refresh after relative time range is changed and now delay exists', function() {
_dashboard.refresh = '10s';
ctx.service.setTime({ from: 'now-1h', to: 'now-10s' });
expect(_dashboard.refresh).to.be('10s');
});
});
});
......@@ -4,7 +4,7 @@ import coreModule from 'app/core/core_module';
import kbn from 'app/core/utils/kbn';
import * as dateMath from 'app/core/utils/datemath';
class TimeSrv {
export class TimeSrv {
time: any;
refreshTimer: any;
refresh: boolean;
......
......@@ -2,6 +2,7 @@ import '../datasource';
import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common';
import helpers from 'test/specs/helpers';
import CloudWatchDatasource from '../datasource';
import 'app/features/dashboard/time_srv';
describe('CloudWatchDatasource', function() {
var ctx = new helpers.ServiceTestContext();
......@@ -13,6 +14,7 @@ describe('CloudWatchDatasource', function() {
beforeEach(angularMocks.module('grafana.services'));
beforeEach(angularMocks.module('grafana.controllers'));
beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
beforeEach(ctx.createService('timeSrv'));
beforeEach(
angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment