grafana/public/app/plugins/panel/graph/specs/graph_tooltip.jest.ts

190 lines
4.5 KiB
TypeScript
Raw Normal View History

2018-06-11 08:59:20 -05:00
jest.mock('app/core/core', () => ({}));
import $ from 'jquery';
import GraphTooltip from '../graph_tooltip';
var scope = {
2018-06-11 08:59:20 -05:00
appEvent: jest.fn(),
onAppEvent: jest.fn(),
ctrl: {},
};
var elem = $('<div></div>');
var dashboard = {};
2018-03-29 04:51:34 -05:00
var getSeriesFn;
function describeSharedTooltip(desc, fn) {
var ctx: any = {};
2016-01-27 11:51:01 -06:00
ctx.ctrl = scope.ctrl;
ctx.ctrl.panel = {
tooltip: {
shared: true,
},
legend: {},
stack: false,
};
ctx.setup = function(setupFn) {
ctx.setupFn = setupFn;
};
describe(desc, function() {
beforeEach(function() {
ctx.setupFn();
2018-03-29 04:51:34 -05:00
var tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
ctx.results = tooltip.getMultiSeriesPlotHoverInfo(ctx.data, ctx.pos);
});
fn(ctx);
});
}
describe('findHoverIndexFromData', function() {
2018-03-29 04:51:34 -05:00
var tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
var series = {
data: [[100, 0], [101, 0], [102, 0], [103, 0], [104, 0], [105, 0], [106, 0], [107, 0]],
};
2017-01-19 03:02:42 -06:00
it('should return 0 if posX out of lower bounds', function() {
2017-01-19 03:02:42 -06:00
var posX = 99;
2018-06-11 08:59:20 -05:00
expect(tooltip.findHoverIndexFromData(posX, series)).toBe(0);
2017-01-19 03:02:42 -06:00
});
it('should return n - 1 if posX out of upper bounds', function() {
2017-01-19 03:02:42 -06:00
var posX = 108;
2018-06-11 08:59:20 -05:00
expect(tooltip.findHoverIndexFromData(posX, series)).toBe(series.data.length - 1);
2017-01-19 03:02:42 -06:00
});
it('should return i if posX in series', function() {
2017-01-19 03:02:42 -06:00
var posX = 104;
2018-06-11 08:59:20 -05:00
expect(tooltip.findHoverIndexFromData(posX, series)).toBe(4);
2017-01-19 03:02:42 -06:00
});
it('should return i if posX not in series and i + 1 > posX', function() {
2017-01-19 03:02:42 -06:00
var posX = 104.9;
2018-06-11 08:59:20 -05:00
expect(tooltip.findHoverIndexFromData(posX, series)).toBe(4);
2017-01-19 03:02:42 -06:00
});
});
describeSharedTooltip('steppedLine false, stack false', function(ctx) {
ctx.setup(function() {
ctx.data = [{ data: [[10, 15], [12, 20]], lines: {} }, { data: [[10, 2], [12, 3]], lines: {} }];
ctx.pos = { x: 11 };
});
it('should return 2 series', function() {
2018-06-11 08:59:20 -05:00
expect(ctx.results.length).toBe(2);
});
2016-01-27 11:51:01 -06:00
it('should add time to results array', function() {
2018-06-11 08:59:20 -05:00
expect(ctx.results.time).toBe(10);
});
2016-01-27 11:51:01 -06:00
it('should set value and hoverIndex', function() {
2018-06-11 08:59:20 -05:00
expect(ctx.results[0].value).toBe(15);
expect(ctx.results[1].value).toBe(2);
expect(ctx.results[0].hoverIndex).toBe(0);
});
});
describeSharedTooltip('one series is hidden', function(ctx) {
ctx.setup(function() {
ctx.data = [{ data: [[10, 15], [12, 20]] }, { data: [] }];
ctx.pos = { x: 11 };
});
});
describeSharedTooltip('steppedLine false, stack true, individual false', function(ctx) {
ctx.setup(function() {
ctx.data = [
{
data: [[10, 15], [12, 20]],
lines: {},
datapoints: {
pointsize: 2,
points: [[10, 15], [12, 20]],
},
stack: true,
},
{
data: [[10, 2], [12, 3]],
lines: {},
datapoints: {
pointsize: 2,
points: [[10, 2], [12, 3]],
},
stack: true,
},
];
ctx.ctrl.panel.stack = true;
ctx.pos = { x: 11 };
});
it('should show stacked value', function() {
2018-06-11 08:59:20 -05:00
expect(ctx.results[1].value).toBe(17);
});
});
describeSharedTooltip('steppedLine false, stack true, individual false, series stack false', function(ctx) {
ctx.setup(function() {
ctx.data = [
{
data: [[10, 15], [12, 20]],
lines: {},
datapoints: {
pointsize: 2,
points: [[10, 15], [12, 20]],
},
stack: true,
},
{
data: [[10, 2], [12, 3]],
lines: {},
datapoints: {
pointsize: 2,
points: [[10, 2], [12, 3]],
},
stack: false,
},
];
ctx.ctrl.panel.stack = true;
ctx.pos = { x: 11 };
});
it('should not show stacked value', function() {
2018-06-11 08:59:20 -05:00
expect(ctx.results[1].value).toBe(2);
});
});
describeSharedTooltip('steppedLine false, stack true, individual true', function(ctx) {
ctx.setup(function() {
ctx.data = [
{
data: [[10, 15], [12, 20]],
lines: {},
datapoints: {
pointsize: 2,
points: [[10, 15], [12, 20]],
},
stack: true,
},
{
data: [[10, 2], [12, 3]],
lines: {},
datapoints: {
pointsize: 2,
points: [[10, 2], [12, 3]],
},
stack: false,
},
];
ctx.ctrl.panel.stack = true;
ctx.ctrl.panel.tooltip.value_type = 'individual';
ctx.pos = { x: 11 };
});
it('should not show stacked value', function() {
2018-06-11 08:59:20 -05:00
expect(ctx.results[1].value).toBe(2);
});
});