123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- jest.mock('app/core/core', () => ({}));
- import $ from 'jquery';
- import GraphTooltip from '../graph_tooltip';
- const scope: any = {
- appEvent: jest.fn(),
- onAppEvent: jest.fn(),
- ctrl: {
- panel: {
- tooltip: {},
- },
- },
- };
- const elem = $('<div></div>');
- const dashboard = {};
- const getSeriesFn = () => {};
- describe('findHoverIndexFromData', () => {
- // @ts-ignore
- const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
- const series = {
- data: [
- [100, 0],
- [101, 0],
- [102, 0],
- [103, 0],
- [104, 0],
- [105, 0],
- [106, 0],
- [107, 0],
- ],
- };
- it('should return 0 if posX out of lower bounds', () => {
- const posX = 99;
- expect(tooltip.findHoverIndexFromData(posX, series)).toBe(0);
- });
- it('should return n - 1 if posX out of upper bounds', () => {
- const posX = 108;
- expect(tooltip.findHoverIndexFromData(posX, series)).toBe(series.data.length - 1);
- });
- it('should return i if posX in series', () => {
- const posX = 104;
- expect(tooltip.findHoverIndexFromData(posX, series)).toBe(4);
- });
- it('should return i if posX not in series and i + 1 > posX', () => {
- const posX = 104.9;
- expect(tooltip.findHoverIndexFromData(posX, series)).toBe(4);
- });
- });
- describe('with a shared tooltip', () => {
- beforeEach(() => {
- scope.ctrl.panel = {
- tooltip: {
- shared: true,
- },
- legend: {},
- stack: false,
- };
- });
- describe('steppedLine false, stack false', () => {
- const data = [
- {
- data: [
- [10, 15],
- [12, 20],
- ],
- lines: {},
- hideTooltip: false,
- },
- {
- data: [
- [10, 2],
- [12, 3],
- ],
- lines: {},
- hideTooltip: false,
- },
- ];
- const pos = { x: 11 };
- it('should return 2 series', () => {
- // @ts-ignore
- const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
- const results = tooltip.getMultiSeriesPlotHoverInfo(data, pos);
- expect(results.length).toBe(2);
- });
- it('should add time to results array', () => {
- // @ts-ignore
- const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
- const results = tooltip.getMultiSeriesPlotHoverInfo(data, pos);
- expect(results.time).toBe(10);
- });
- it('should set value and hoverIndex', () => {
- // @ts-ignore
- const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
- const results = tooltip.getMultiSeriesPlotHoverInfo(data, pos);
- expect(results[0].value).toBe(15);
- expect(results[1].value).toBe(2);
- expect(results[0].hoverIndex).toBe(0);
- });
- });
- describe('steppedLine false, stack true, individual false', () => {
- const data = [
- {
- data: [
- [10, 15],
- [12, 20],
- ],
- lines: {},
- datapoints: {
- pointsize: 2,
- points: [
- [10, 15],
- [12, 20],
- ],
- },
- stack: true,
- hideTooltip: false,
- },
- {
- data: [
- [10, 2],
- [12, 3],
- ],
- lines: {},
- datapoints: {
- pointsize: 2,
- points: [
- [10, 2],
- [12, 3],
- ],
- },
- stack: true,
- hideTooltip: false,
- },
- ];
- scope.ctrl.panel.stack = true;
- const pos = { x: 11 };
- it('should show stacked value', () => {
- // @ts-ignore
- const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
- const results = tooltip.getMultiSeriesPlotHoverInfo(data, pos);
- expect(results[1].value).toBe(17);
- });
- });
- describe('steppedLine false, stack true, individual false, series stack false', () => {
- const data = [
- {
- data: [
- [10, 15],
- [12, 20],
- ],
- lines: {},
- datapoints: {
- pointsize: 2,
- points: [
- [10, 15],
- [12, 20],
- ],
- },
- stack: true,
- hideTooltip: false,
- },
- {
- data: [
- [10, 2],
- [12, 3],
- ],
- lines: {},
- datapoints: {
- pointsize: 2,
- points: [
- [10, 2],
- [12, 3],
- ],
- },
- stack: false,
- hideTooltip: false,
- },
- ];
- scope.ctrl.panel.stack = true;
- const pos = { x: 11 };
- it('should not show stacked value', () => {
- // @ts-ignore
- const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
- const results = tooltip.getMultiSeriesPlotHoverInfo(data, pos);
- expect(results[1].value).toBe(2);
- });
- });
- describe('steppedLine false, stack true, individual true', () => {
- const data = [
- {
- data: [
- [10, 15],
- [12, 20],
- ],
- lines: {},
- datapoints: {
- pointsize: 2,
- points: [
- [10, 15],
- [12, 20],
- ],
- },
- stack: true,
- hideTooltip: false,
- },
- {
- data: [
- [10, 2],
- [12, 3],
- ],
- lines: {},
- datapoints: {
- pointsize: 2,
- points: [
- [10, 2],
- [12, 3],
- ],
- },
- stack: false,
- hideTooltip: false,
- },
- ];
- scope.ctrl.panel.stack = true;
- scope.ctrl.panel.tooltip.value_type = 'individual';
- const pos = { x: 11 };
- it('should not show stacked value', () => {
- // @ts-ignore
- const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
- const results = tooltip.getMultiSeriesPlotHoverInfo(data, pos);
- expect(results[1].value).toBe(2);
- });
- });
- });
|