| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- import { TimeRegionManager, colorModes } from '../time_region_manager';
- import moment from 'moment';
- describe('TimeRegionManager', () => {
- function plotOptionsScenario(desc, func) {
- describe(desc, () => {
- const ctx: any = {
- panel: {
- timeRegions: [],
- },
- options: {
- grid: { markings: [] },
- },
- panelCtrl: {
- range: {},
- dashboard: {
- isTimezoneUtc: () => false,
- },
- },
- };
- ctx.setup = (regions, from, to) => {
- ctx.panel.timeRegions = regions;
- ctx.panelCtrl.range.from = from;
- ctx.panelCtrl.range.to = to;
- const manager = new TimeRegionManager(ctx.panelCtrl);
- manager.addFlotOptions(ctx.options, ctx.panel);
- };
- ctx.printScenario = () => {
- console.log(
- `Time range: from=${ctx.panelCtrl.range.from.format()}, to=${ctx.panelCtrl.range.to.format()}`,
- ctx.panelCtrl.range.from._isUTC
- );
- ctx.options.grid.markings.forEach((m, i) => {
- console.log(
- `Marking (${i}): from=${moment(m.xaxis.from).format()}, to=${moment(m.xaxis.to).format()}, color=${m.color}`
- );
- });
- };
- func(ctx);
- });
- }
- describe('When creating plot markings using local time', () => {
- plotOptionsScenario('for day of week region', ctx => {
- const regions = [{ fromDayOfWeek: 1, toDayOfWeek: 1, fill: true, line: true, colorMode: 'red' }];
- const from = moment('2018-01-01T00:00:00+01:00');
- const to = moment('2018-01-01T23:59:00+01:00');
- ctx.setup(regions, from, to);
- it('should add 3 markings', () => {
- expect(ctx.options.grid.markings.length).toBe(3);
- });
- it('should add fill', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[0].xaxis.from).format()).toBe(moment('2018-01-01T01:00:00+01:00').format());
- expect(moment(markings[0].xaxis.to).format()).toBe(moment('2018-01-02T00:59:59+01:00').format());
- expect(markings[0].color).toBe(colorModes.red.color.fill);
- });
- it('should add line before', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[1].xaxis.from).format()).toBe(moment('2018-01-01T01:00:00+01:00').format());
- expect(moment(markings[1].xaxis.to).format()).toBe(moment('2018-01-01T01:00:00+01:00').format());
- expect(markings[1].color).toBe(colorModes.red.color.line);
- });
- it('should add line after', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[2].xaxis.from).format()).toBe(moment('2018-01-02T00:59:59+01:00').format());
- expect(moment(markings[2].xaxis.to).format()).toBe(moment('2018-01-02T00:59:59+01:00').format());
- expect(markings[2].color).toBe(colorModes.red.color.line);
- });
- });
- plotOptionsScenario('for time from region', ctx => {
- const regions = [{ from: '05:00', fill: true, colorMode: 'red' }];
- const from = moment('2018-01-01T00:00+01:00');
- const to = moment('2018-01-03T23:59+01:00');
- ctx.setup(regions, from, to);
- it('should add 3 markings', () => {
- expect(ctx.options.grid.markings.length).toBe(3);
- });
- it('should add one fill at 05:00 each day', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[0].xaxis.from).format()).toBe(moment('2018-01-01T06:00:00+01:00').format());
- expect(moment(markings[0].xaxis.to).format()).toBe(moment('2018-01-01T06:00:00+01:00').format());
- expect(markings[0].color).toBe(colorModes.red.color.fill);
- expect(moment(markings[1].xaxis.from).format()).toBe(moment('2018-01-02T06:00:00+01:00').format());
- expect(moment(markings[1].xaxis.to).format()).toBe(moment('2018-01-02T06:00:00+01:00').format());
- expect(markings[1].color).toBe(colorModes.red.color.fill);
- expect(moment(markings[2].xaxis.from).format()).toBe(moment('2018-01-03T06:00:00+01:00').format());
- expect(moment(markings[2].xaxis.to).format()).toBe(moment('2018-01-03T06:00:00+01:00').format());
- expect(markings[2].color).toBe(colorModes.red.color.fill);
- });
- });
- plotOptionsScenario('for time to region', ctx => {
- const regions = [{ to: '05:00', fill: true, colorMode: 'red' }];
- const from = moment('2018-02-01T00:00+01:00');
- const to = moment('2018-02-03T23:59+01:00');
- ctx.setup(regions, from, to);
- it('should add 3 markings', () => {
- expect(ctx.options.grid.markings.length).toBe(3);
- });
- it('should add one fill at 05:00 each day', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[0].xaxis.from).format()).toBe(moment('2018-02-01T06:00:00+01:00').format());
- expect(moment(markings[0].xaxis.to).format()).toBe(moment('2018-02-01T06:00:00+01:00').format());
- expect(markings[0].color).toBe(colorModes.red.color.fill);
- expect(moment(markings[1].xaxis.from).format()).toBe(moment('2018-02-02T06:00:00+01:00').format());
- expect(moment(markings[1].xaxis.to).format()).toBe(moment('2018-02-02T06:00:00+01:00').format());
- expect(markings[1].color).toBe(colorModes.red.color.fill);
- expect(moment(markings[2].xaxis.from).format()).toBe(moment('2018-02-03T06:00:00+01:00').format());
- expect(moment(markings[2].xaxis.to).format()).toBe(moment('2018-02-03T06:00:00+01:00').format());
- expect(markings[2].color).toBe(colorModes.red.color.fill);
- });
- });
- plotOptionsScenario('for time from/to region', ctx => {
- const regions = [{ from: '00:00', to: '05:00', fill: true, colorMode: 'red' }];
- const from = moment('2018-12-01T00:00+01:00');
- const to = moment('2018-12-03T23:59+01:00');
- ctx.setup(regions, from, to);
- it('should add 3 markings', () => {
- expect(ctx.options.grid.markings.length).toBe(3);
- });
- it('should add one fill between 00:00 and 05:00 each day', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[0].xaxis.from).format()).toBe(moment('2018-12-01T01:00:00+01:00').format());
- expect(moment(markings[0].xaxis.to).format()).toBe(moment('2018-12-01T06:00:00+01:00').format());
- expect(markings[0].color).toBe(colorModes.red.color.fill);
- expect(moment(markings[1].xaxis.from).format()).toBe(moment('2018-12-02T01:00:00+01:00').format());
- expect(moment(markings[1].xaxis.to).format()).toBe(moment('2018-12-02T06:00:00+01:00').format());
- expect(markings[1].color).toBe(colorModes.red.color.fill);
- expect(moment(markings[2].xaxis.from).format()).toBe(moment('2018-12-03T01:00:00+01:00').format());
- expect(moment(markings[2].xaxis.to).format()).toBe(moment('2018-12-03T06:00:00+01:00').format());
- expect(markings[2].color).toBe(colorModes.red.color.fill);
- });
- });
- plotOptionsScenario('for day of week from/to region', ctx => {
- const regions = [{ fromDayOfWeek: 7, toDayOfWeek: 7, fill: true, colorMode: 'red' }];
- const from = moment('2018-01-01T18:45:05+01:00');
- const to = moment('2018-01-22T08:27:00+01:00');
- ctx.setup(regions, from, to);
- it('should add 3 markings', () => {
- expect(ctx.options.grid.markings.length).toBe(3);
- });
- it('should add one fill at each sunday', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[0].xaxis.from).format()).toBe(moment('2018-01-07T01:00:00+01:00').format());
- expect(moment(markings[0].xaxis.to).format()).toBe(moment('2018-01-08T00:59:59+01:00').format());
- expect(markings[0].color).toBe(colorModes.red.color.fill);
- expect(moment(markings[1].xaxis.from).format()).toBe(moment('2018-01-14T01:00:00+01:00').format());
- expect(moment(markings[1].xaxis.to).format()).toBe(moment('2018-01-15T00:59:59+01:00').format());
- expect(markings[1].color).toBe(colorModes.red.color.fill);
- expect(moment(markings[2].xaxis.from).format()).toBe(moment('2018-01-21T01:00:00+01:00').format());
- expect(moment(markings[2].xaxis.to).format()).toBe(moment('2018-01-22T00:59:59+01:00').format());
- expect(markings[2].color).toBe(colorModes.red.color.fill);
- });
- });
- plotOptionsScenario('for day of week from region', ctx => {
- const regions = [{ fromDayOfWeek: 7, fill: true, colorMode: 'red' }];
- const from = moment('2018-01-01T18:45:05+01:00');
- const to = moment('2018-01-22T08:27:00+01:00');
- ctx.setup(regions, from, to);
- it('should add 3 markings', () => {
- expect(ctx.options.grid.markings.length).toBe(3);
- });
- it('should add one fill at each sunday', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[0].xaxis.from).format()).toBe(moment('2018-01-07T01:00:00+01:00').format());
- expect(moment(markings[0].xaxis.to).format()).toBe(moment('2018-01-08T00:59:59+01:00').format());
- expect(markings[0].color).toBe(colorModes.red.color.fill);
- expect(moment(markings[1].xaxis.from).format()).toBe(moment('2018-01-14T01:00:00+01:00').format());
- expect(moment(markings[1].xaxis.to).format()).toBe(moment('2018-01-15T00:59:59+01:00').format());
- expect(markings[1].color).toBe(colorModes.red.color.fill);
- expect(moment(markings[2].xaxis.from).format()).toBe(moment('2018-01-21T01:00:00+01:00').format());
- expect(moment(markings[2].xaxis.to).format()).toBe(moment('2018-01-22T00:59:59+01:00').format());
- expect(markings[2].color).toBe(colorModes.red.color.fill);
- });
- });
- plotOptionsScenario('for day of week to region', ctx => {
- const regions = [{ toDayOfWeek: 7, fill: true, colorMode: 'red' }];
- const from = moment('2018-01-01T18:45:05+01:00');
- const to = moment('2018-01-22T08:27:00+01:00');
- ctx.setup(regions, from, to);
- it('should add 3 markings', () => {
- expect(ctx.options.grid.markings.length).toBe(3);
- });
- it('should add one fill at each sunday', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[0].xaxis.from).format()).toBe(moment('2018-01-07T01:00:00+01:00').format());
- expect(moment(markings[0].xaxis.to).format()).toBe(moment('2018-01-08T00:59:59+01:00').format());
- expect(markings[0].color).toBe(colorModes.red.color.fill);
- expect(moment(markings[1].xaxis.from).format()).toBe(moment('2018-01-14T01:00:00+01:00').format());
- expect(moment(markings[1].xaxis.to).format()).toBe(moment('2018-01-15T00:59:59+01:00').format());
- expect(markings[1].color).toBe(colorModes.red.color.fill);
- expect(moment(markings[2].xaxis.from).format()).toBe(moment('2018-01-21T01:00:00+01:00').format());
- expect(moment(markings[2].xaxis.to).format()).toBe(moment('2018-01-22T00:59:59+01:00').format());
- expect(markings[2].color).toBe(colorModes.red.color.fill);
- });
- });
- plotOptionsScenario('for day of week from/to time region', ctx => {
- const regions = [{ fromDayOfWeek: 7, from: '23:00', toDayOfWeek: 1, to: '01:40', fill: true, colorMode: 'red' }];
- const from = moment('2018-12-07T12:51:19+01:00');
- const to = moment('2018-12-10T13:51:29+01:00');
- ctx.setup(regions, from, to);
- it('should add 1 marking', () => {
- expect(ctx.options.grid.markings.length).toBe(1);
- });
- it('should add one fill between sunday 23:00 and monday 01:40', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[0].xaxis.from).format()).toBe(moment('2018-12-10T00:00:00+01:00').format());
- expect(moment(markings[0].xaxis.to).format()).toBe(moment('2018-12-10T02:40:00+01:00').format());
- });
- });
- plotOptionsScenario('for day of week from/to time region', ctx => {
- const regions = [{ fromDayOfWeek: 6, from: '03:00', toDayOfWeek: 7, to: '02:00', fill: true, colorMode: 'red' }];
- const from = moment('2018-12-07T12:51:19+01:00');
- const to = moment('2018-12-10T13:51:29+01:00');
- ctx.setup(regions, from, to);
- it('should add 1 marking', () => {
- expect(ctx.options.grid.markings.length).toBe(1);
- });
- it('should add one fill between saturday 03:00 and sunday 02:00', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[0].xaxis.from).format()).toBe(moment('2018-12-08T04:00:00+01:00').format());
- expect(moment(markings[0].xaxis.to).format()).toBe(moment('2018-12-09T03:00:00+01:00').format());
- });
- });
- plotOptionsScenario('for day of week from/to time region with daylight saving time', ctx => {
- const regions = [{ fromDayOfWeek: 7, from: '20:00', toDayOfWeek: 7, to: '23:00', fill: true, colorMode: 'red' }];
- const from = moment('2018-03-17T06:00:00+01:00');
- const to = moment('2018-04-03T06:00:00+02:00');
- ctx.setup(regions, from, to);
- it('should add 3 markings', () => {
- expect(ctx.options.grid.markings.length).toBe(3);
- });
- it('should add one fill at each sunday between 20:00 and 23:00', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[0].xaxis.from).format()).toBe(moment('2018-03-18T21:00:00+01:00').format());
- expect(moment(markings[0].xaxis.to).format()).toBe(moment('2018-03-19T00:00:00+01:00').format());
- expect(moment(markings[1].xaxis.from).format()).toBe(moment('2018-03-25T22:00:00+02:00').format());
- expect(moment(markings[1].xaxis.to).format()).toBe(moment('2018-03-26T01:00:00+02:00').format());
- expect(moment(markings[2].xaxis.from).format()).toBe(moment('2018-04-01T22:00:00+02:00').format());
- expect(moment(markings[2].xaxis.to).format()).toBe(moment('2018-04-02T01:00:00+02:00').format());
- });
- });
- plotOptionsScenario('for each day of week with winter time', ctx => {
- const regions = [{ fromDayOfWeek: 7, toDayOfWeek: 7, fill: true, colorMode: 'red' }];
- const from = moment('2018-10-20T14:50:11+02:00');
- const to = moment('2018-11-07T12:56:23+01:00');
- ctx.setup(regions, from, to);
- it('should add 3 markings', () => {
- expect(ctx.options.grid.markings.length).toBe(3);
- });
- it('should add one fill at each sunday', () => {
- const markings = ctx.options.grid.markings;
- expect(moment(markings[0].xaxis.from).format()).toBe(moment('2018-10-21T02:00:00+02:00').format());
- expect(moment(markings[0].xaxis.to).format()).toBe(moment('2018-10-22T01:59:59+02:00').format());
- expect(moment(markings[1].xaxis.from).format()).toBe(moment('2018-10-28T02:00:00+02:00').format());
- expect(moment(markings[1].xaxis.to).format()).toBe(moment('2018-10-29T00:59:59+01:00').format());
- expect(moment(markings[2].xaxis.from).format()).toBe(moment('2018-11-04T01:00:00+01:00').format());
- expect(moment(markings[2].xaxis.to).format()).toBe(moment('2018-11-05T00:59:59+01:00').format());
- });
- });
- });
- });
|