123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952 |
- // Test the rectangle element
- describe('Core.Tooltip', function() {
- describe('config', function() {
- it('should not include the dataset label in the body string if not defined', function() {
- var data = {
- datasets: [{
- data: [10, 20, 30],
- pointHoverBorderColor: 'rgb(255, 0, 0)',
- pointHoverBackgroundColor: 'rgb(0, 255, 0)'
- }],
- labels: ['Point 1', 'Point 2', 'Point 3']
- };
- var tooltipItem = {
- index: 1,
- datasetIndex: 0,
- xLabel: 'Point 2',
- yLabel: '20'
- };
- var label = Chart.defaults.global.tooltips.callbacks.label(tooltipItem, data);
- expect(label).toBe('20');
- data.datasets[0].label = 'My dataset';
- label = Chart.defaults.global.tooltips.callbacks.label(tooltipItem, data);
- expect(label).toBe('My dataset: 20');
- });
- });
- describe('index mode', function() {
- it('Should only use x distance when intersect is false', function() {
- var chart = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- label: 'Dataset 1',
- data: [10, 20, 30],
- pointHoverBorderColor: 'rgb(255, 0, 0)',
- pointHoverBackgroundColor: 'rgb(0, 255, 0)'
- }, {
- label: 'Dataset 2',
- data: [40, 40, 40],
- pointHoverBorderColor: 'rgb(0, 0, 255)',
- pointHoverBackgroundColor: 'rgb(0, 255, 255)'
- }],
- labels: ['Point 1', 'Point 2', 'Point 3']
- },
- options: {
- tooltips: {
- mode: 'index',
- intersect: false,
- },
- hover: {
- mode: 'index',
- intersect: false
- }
- }
- });
- // Trigger an event over top of the
- var meta = chart.getDatasetMeta(0);
- var point = meta.data[1];
- var node = chart.canvas;
- var rect = node.getBoundingClientRect();
- var evt = new MouseEvent('mousemove', {
- view: window,
- bubbles: true,
- cancelable: true,
- clientX: rect.left + point._model.x,
- clientY: 0
- });
- // Manually trigger rather than having an async test
- node.dispatchEvent(evt);
- // Check and see if tooltip was displayed
- var tooltip = chart.tooltip;
- var globalDefaults = Chart.defaults.global;
- expect(tooltip._view).toEqual(jasmine.objectContaining({
- // Positioning
- xPadding: 6,
- yPadding: 6,
- xAlign: 'left',
- yAlign: 'center',
- // Body
- bodyFontColor: '#fff',
- _bodyFontFamily: globalDefaults.defaultFontFamily,
- _bodyFontStyle: globalDefaults.defaultFontStyle,
- _bodyAlign: 'left',
- bodyFontSize: globalDefaults.defaultFontSize,
- bodySpacing: 2,
- // Title
- titleFontColor: '#fff',
- _titleFontFamily: globalDefaults.defaultFontFamily,
- _titleFontStyle: 'bold',
- titleFontSize: globalDefaults.defaultFontSize,
- _titleAlign: 'left',
- titleSpacing: 2,
- titleMarginBottom: 6,
- // Footer
- footerFontColor: '#fff',
- _footerFontFamily: globalDefaults.defaultFontFamily,
- _footerFontStyle: 'bold',
- footerFontSize: globalDefaults.defaultFontSize,
- _footerAlign: 'left',
- footerSpacing: 2,
- footerMarginTop: 6,
- // Appearance
- caretSize: 5,
- cornerRadius: 6,
- backgroundColor: 'rgba(0,0,0,0.8)',
- opacity: 1,
- legendColorBackground: '#fff',
- displayColors: true,
- // Text
- title: ['Point 2'],
- beforeBody: [],
- body: [{
- before: [],
- lines: ['Dataset 1: 20'],
- after: []
- }, {
- before: [],
- lines: ['Dataset 2: 40'],
- after: []
- }],
- afterBody: [],
- footer: [],
- caretPadding: 2,
- labelColors: [{
- borderColor: 'rgb(255, 0, 0)',
- backgroundColor: 'rgb(0, 255, 0)'
- }, {
- borderColor: 'rgb(0, 0, 255)',
- backgroundColor: 'rgb(0, 255, 255)'
- }]
- }));
- expect(tooltip._view.x).toBeCloseToPixel(266);
- expect(tooltip._view.y).toBeCloseToPixel(155);
- });
- it('Should only display if intersecting if intersect is set', function() {
- var chart = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- label: 'Dataset 1',
- data: [10, 20, 30],
- pointHoverBorderColor: 'rgb(255, 0, 0)',
- pointHoverBackgroundColor: 'rgb(0, 255, 0)'
- }, {
- label: 'Dataset 2',
- data: [40, 40, 40],
- pointHoverBorderColor: 'rgb(0, 0, 255)',
- pointHoverBackgroundColor: 'rgb(0, 255, 255)'
- }],
- labels: ['Point 1', 'Point 2', 'Point 3']
- },
- options: {
- tooltips: {
- mode: 'index',
- intersect: true
- }
- }
- });
- // Trigger an event over top of the
- var meta = chart.getDatasetMeta(0);
- var point = meta.data[1];
- var node = chart.canvas;
- var rect = node.getBoundingClientRect();
- var evt = new MouseEvent('mousemove', {
- view: window,
- bubbles: true,
- cancelable: true,
- clientX: rect.left + point._model.x,
- clientY: 0
- });
- // Manually trigger rather than having an async test
- node.dispatchEvent(evt);
- // Check and see if tooltip was displayed
- var tooltip = chart.tooltip;
- var globalDefaults = Chart.defaults.global;
- expect(tooltip._view).toEqual(jasmine.objectContaining({
- // Positioning
- xPadding: 6,
- yPadding: 6,
- // Body
- bodyFontColor: '#fff',
- _bodyFontFamily: globalDefaults.defaultFontFamily,
- _bodyFontStyle: globalDefaults.defaultFontStyle,
- _bodyAlign: 'left',
- bodyFontSize: globalDefaults.defaultFontSize,
- bodySpacing: 2,
- // Title
- titleFontColor: '#fff',
- _titleFontFamily: globalDefaults.defaultFontFamily,
- _titleFontStyle: 'bold',
- titleFontSize: globalDefaults.defaultFontSize,
- _titleAlign: 'left',
- titleSpacing: 2,
- titleMarginBottom: 6,
- // Footer
- footerFontColor: '#fff',
- _footerFontFamily: globalDefaults.defaultFontFamily,
- _footerFontStyle: 'bold',
- footerFontSize: globalDefaults.defaultFontSize,
- _footerAlign: 'left',
- footerSpacing: 2,
- footerMarginTop: 6,
- // Appearance
- caretSize: 5,
- cornerRadius: 6,
- backgroundColor: 'rgba(0,0,0,0.8)',
- opacity: 0,
- legendColorBackground: '#fff',
- displayColors: true,
- }));
- });
- });
- it('Should display in single mode', function() {
- var chart = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- label: 'Dataset 1',
- data: [10, 20, 30],
- pointHoverBorderColor: 'rgb(255, 0, 0)',
- pointHoverBackgroundColor: 'rgb(0, 255, 0)'
- }, {
- label: 'Dataset 2',
- data: [40, 40, 40],
- pointHoverBorderColor: 'rgb(0, 0, 255)',
- pointHoverBackgroundColor: 'rgb(0, 255, 255)'
- }],
- labels: ['Point 1', 'Point 2', 'Point 3']
- },
- options: {
- tooltips: {
- mode: 'single'
- }
- }
- });
- // Trigger an event over top of the
- var meta = chart.getDatasetMeta(0);
- var point = meta.data[1];
- var node = chart.canvas;
- var rect = node.getBoundingClientRect();
- var evt = new MouseEvent('mousemove', {
- view: window,
- bubbles: true,
- cancelable: true,
- clientX: rect.left + point._model.x,
- clientY: rect.top + point._model.y
- });
- // Manually trigger rather than having an async test
- node.dispatchEvent(evt);
- // Check and see if tooltip was displayed
- var tooltip = chart.tooltip;
- var globalDefaults = Chart.defaults.global;
- expect(tooltip._view).toEqual(jasmine.objectContaining({
- // Positioning
- xPadding: 6,
- yPadding: 6,
- xAlign: 'left',
- yAlign: 'center',
- // Body
- bodyFontColor: '#fff',
- _bodyFontFamily: globalDefaults.defaultFontFamily,
- _bodyFontStyle: globalDefaults.defaultFontStyle,
- _bodyAlign: 'left',
- bodyFontSize: globalDefaults.defaultFontSize,
- bodySpacing: 2,
- // Title
- titleFontColor: '#fff',
- _titleFontFamily: globalDefaults.defaultFontFamily,
- _titleFontStyle: 'bold',
- titleFontSize: globalDefaults.defaultFontSize,
- _titleAlign: 'left',
- titleSpacing: 2,
- titleMarginBottom: 6,
- // Footer
- footerFontColor: '#fff',
- _footerFontFamily: globalDefaults.defaultFontFamily,
- _footerFontStyle: 'bold',
- footerFontSize: globalDefaults.defaultFontSize,
- _footerAlign: 'left',
- footerSpacing: 2,
- footerMarginTop: 6,
- // Appearance
- caretSize: 5,
- cornerRadius: 6,
- backgroundColor: 'rgba(0,0,0,0.8)',
- opacity: 1,
- legendColorBackground: '#fff',
- displayColors: true,
- // Text
- title: ['Point 2'],
- beforeBody: [],
- body: [{
- before: [],
- lines: ['Dataset 1: 20'],
- after: []
- }],
- afterBody: [],
- footer: [],
- caretPadding: 2,
- labelTextColors: ['#fff'],
- labelColors: [{
- borderColor: 'rgb(255, 0, 0)',
- backgroundColor: 'rgb(0, 255, 0)'
- }]
- }));
- expect(tooltip._view.x).toBeCloseToPixel(266);
- expect(tooltip._view.y).toBeCloseToPixel(312);
- });
- it('Should display information from user callbacks', function() {
- var chart = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- label: 'Dataset 1',
- data: [10, 20, 30],
- pointHoverBorderColor: 'rgb(255, 0, 0)',
- pointHoverBackgroundColor: 'rgb(0, 255, 0)'
- }, {
- label: 'Dataset 2',
- data: [40, 40, 40],
- pointHoverBorderColor: 'rgb(0, 0, 255)',
- pointHoverBackgroundColor: 'rgb(0, 255, 255)'
- }],
- labels: ['Point 1', 'Point 2', 'Point 3']
- },
- options: {
- tooltips: {
- mode: 'label',
- callbacks: {
- beforeTitle: function() {
- return 'beforeTitle';
- },
- title: function() {
- return 'title';
- },
- afterTitle: function() {
- return 'afterTitle';
- },
- beforeBody: function() {
- return 'beforeBody';
- },
- beforeLabel: function() {
- return 'beforeLabel';
- },
- label: function() {
- return 'label';
- },
- afterLabel: function() {
- return 'afterLabel';
- },
- afterBody: function() {
- return 'afterBody';
- },
- beforeFooter: function() {
- return 'beforeFooter';
- },
- footer: function() {
- return 'footer';
- },
- afterFooter: function() {
- return 'afterFooter';
- },
- labelTextColor: function() {
- return 'labelTextColor';
- }
- }
- }
- }
- });
- // Trigger an event over top of the
- var meta = chart.getDatasetMeta(0);
- var point = meta.data[1];
- var node = chart.canvas;
- var rect = node.getBoundingClientRect();
- var evt = new MouseEvent('mousemove', {
- view: window,
- bubbles: true,
- cancelable: true,
- clientX: rect.left + point._model.x,
- clientY: rect.top + point._model.y
- });
- // Manually trigger rather than having an async test
- node.dispatchEvent(evt);
- // Check and see if tooltip was displayed
- var tooltip = chart.tooltip;
- var globalDefaults = Chart.defaults.global;
- expect(tooltip._view).toEqual(jasmine.objectContaining({
- // Positioning
- xPadding: 6,
- yPadding: 6,
- xAlign: 'center',
- yAlign: 'top',
- // Body
- bodyFontColor: '#fff',
- _bodyFontFamily: globalDefaults.defaultFontFamily,
- _bodyFontStyle: globalDefaults.defaultFontStyle,
- _bodyAlign: 'left',
- bodyFontSize: globalDefaults.defaultFontSize,
- bodySpacing: 2,
- // Title
- titleFontColor: '#fff',
- _titleFontFamily: globalDefaults.defaultFontFamily,
- _titleFontStyle: 'bold',
- titleFontSize: globalDefaults.defaultFontSize,
- _titleAlign: 'left',
- titleSpacing: 2,
- titleMarginBottom: 6,
- // Footer
- footerFontColor: '#fff',
- _footerFontFamily: globalDefaults.defaultFontFamily,
- _footerFontStyle: 'bold',
- footerFontSize: globalDefaults.defaultFontSize,
- _footerAlign: 'left',
- footerSpacing: 2,
- footerMarginTop: 6,
- // Appearance
- caretSize: 5,
- cornerRadius: 6,
- backgroundColor: 'rgba(0,0,0,0.8)',
- opacity: 1,
- legendColorBackground: '#fff',
- // Text
- title: ['beforeTitle', 'title', 'afterTitle'],
- beforeBody: ['beforeBody'],
- body: [{
- before: ['beforeLabel'],
- lines: ['label'],
- after: ['afterLabel']
- }, {
- before: ['beforeLabel'],
- lines: ['label'],
- after: ['afterLabel']
- }],
- afterBody: ['afterBody'],
- footer: ['beforeFooter', 'footer', 'afterFooter'],
- caretPadding: 2,
- labelTextColors: ['labelTextColor', 'labelTextColor'],
- labelColors: [{
- borderColor: 'rgb(255, 0, 0)',
- backgroundColor: 'rgb(0, 255, 0)'
- }, {
- borderColor: 'rgb(0, 0, 255)',
- backgroundColor: 'rgb(0, 255, 255)'
- }]
- }));
- expect(tooltip._view.x).toBeCloseToPixel(214);
- expect(tooltip._view.y).toBeCloseToPixel(190);
- });
- it('Should allow sorting items', function() {
- var chart = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- label: 'Dataset 1',
- data: [10, 20, 30],
- pointHoverBorderColor: 'rgb(255, 0, 0)',
- pointHoverBackgroundColor: 'rgb(0, 255, 0)'
- }, {
- label: 'Dataset 2',
- data: [40, 40, 40],
- pointHoverBorderColor: 'rgb(0, 0, 255)',
- pointHoverBackgroundColor: 'rgb(0, 255, 255)'
- }],
- labels: ['Point 1', 'Point 2', 'Point 3']
- },
- options: {
- tooltips: {
- mode: 'label',
- itemSort: function(a, b) {
- return a.datasetIndex > b.datasetIndex ? -1 : 1;
- }
- }
- }
- });
- // Trigger an event over top of the
- var meta0 = chart.getDatasetMeta(0);
- var point0 = meta0.data[1];
- var node = chart.canvas;
- var rect = node.getBoundingClientRect();
- var evt = new MouseEvent('mousemove', {
- view: window,
- bubbles: true,
- cancelable: true,
- clientX: rect.left + point0._model.x,
- clientY: rect.top + point0._model.y
- });
- // Manually trigger rather than having an async test
- node.dispatchEvent(evt);
- // Check and see if tooltip was displayed
- var tooltip = chart.tooltip;
- expect(tooltip._view).toEqual(jasmine.objectContaining({
- // Positioning
- xAlign: 'left',
- yAlign: 'center',
- // Text
- title: ['Point 2'],
- beforeBody: [],
- body: [{
- before: [],
- lines: ['Dataset 2: 40'],
- after: []
- }, {
- before: [],
- lines: ['Dataset 1: 20'],
- after: []
- }],
- afterBody: [],
- footer: [],
- labelColors: [{
- borderColor: 'rgb(0, 0, 255)',
- backgroundColor: 'rgb(0, 255, 255)'
- }, {
- borderColor: 'rgb(255, 0, 0)',
- backgroundColor: 'rgb(0, 255, 0)'
- }]
- }));
- expect(tooltip._view.x).toBeCloseToPixel(266);
- expect(tooltip._view.y).toBeCloseToPixel(155);
- });
- it('should filter items from the tooltip using the callback', function() {
- var chart = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- label: 'Dataset 1',
- data: [10, 20, 30],
- pointHoverBorderColor: 'rgb(255, 0, 0)',
- pointHoverBackgroundColor: 'rgb(0, 255, 0)',
- tooltipHidden: true
- }, {
- label: 'Dataset 2',
- data: [40, 40, 40],
- pointHoverBorderColor: 'rgb(0, 0, 255)',
- pointHoverBackgroundColor: 'rgb(0, 255, 255)'
- }],
- labels: ['Point 1', 'Point 2', 'Point 3']
- },
- options: {
- tooltips: {
- mode: 'label',
- filter: function(tooltipItem, data) {
- // For testing purposes remove the first dataset that has a tooltipHidden property
- return !data.datasets[tooltipItem.datasetIndex].tooltipHidden;
- }
- }
- }
- });
- // Trigger an event over top of the
- var meta0 = chart.getDatasetMeta(0);
- var point0 = meta0.data[1];
- var node = chart.canvas;
- var rect = node.getBoundingClientRect();
- var evt = new MouseEvent('mousemove', {
- view: window,
- bubbles: true,
- cancelable: true,
- clientX: rect.left + point0._model.x,
- clientY: rect.top + point0._model.y
- });
- // Manually trigger rather than having an async test
- node.dispatchEvent(evt);
- // Check and see if tooltip was displayed
- var tooltip = chart.tooltip;
- expect(tooltip._view).toEqual(jasmine.objectContaining({
- // Positioning
- xAlign: 'left',
- yAlign: 'center',
- // Text
- title: ['Point 2'],
- beforeBody: [],
- body: [{
- before: [],
- lines: ['Dataset 2: 40'],
- after: []
- }],
- afterBody: [],
- footer: [],
- labelColors: [{
- borderColor: 'rgb(0, 0, 255)',
- backgroundColor: 'rgb(0, 255, 255)'
- }]
- }));
- });
- it('should set the caretPadding based on a config setting', function() {
- var chart = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- label: 'Dataset 1',
- data: [10, 20, 30],
- pointHoverBorderColor: 'rgb(255, 0, 0)',
- pointHoverBackgroundColor: 'rgb(0, 255, 0)',
- tooltipHidden: true
- }, {
- label: 'Dataset 2',
- data: [40, 40, 40],
- pointHoverBorderColor: 'rgb(0, 0, 255)',
- pointHoverBackgroundColor: 'rgb(0, 255, 255)'
- }],
- labels: ['Point 1', 'Point 2', 'Point 3']
- },
- options: {
- tooltips: {
- caretPadding: 10
- }
- }
- });
- // Trigger an event over top of the
- var meta0 = chart.getDatasetMeta(0);
- var point0 = meta0.data[1];
- var node = chart.canvas;
- var rect = node.getBoundingClientRect();
- var evt = new MouseEvent('mousemove', {
- view: window,
- bubbles: true,
- cancelable: true,
- clientX: rect.left + point0._model.x,
- clientY: rect.top + point0._model.y
- });
- // Manually trigger rather than having an async test
- node.dispatchEvent(evt);
- // Check and see if tooltip was displayed
- var tooltip = chart.tooltip;
- expect(tooltip._model).toEqual(jasmine.objectContaining({
- // Positioning
- caretPadding: 10,
- }));
- });
- it('Should have dataPoints', function() {
- var chart = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- label: 'Dataset 1',
- data: [10, 20, 30],
- pointHoverBorderColor: 'rgb(255, 0, 0)',
- pointHoverBackgroundColor: 'rgb(0, 255, 0)'
- }, {
- label: 'Dataset 2',
- data: [40, 40, 40],
- pointHoverBorderColor: 'rgb(0, 0, 255)',
- pointHoverBackgroundColor: 'rgb(0, 255, 255)'
- }],
- labels: ['Point 1', 'Point 2', 'Point 3']
- },
- options: {
- tooltips: {
- mode: 'single'
- }
- }
- });
- // Trigger an event over top of the
- var pointIndex = 1;
- var datasetIndex = 0;
- var meta = chart.getDatasetMeta(datasetIndex);
- var point = meta.data[pointIndex];
- var node = chart.canvas;
- var rect = node.getBoundingClientRect();
- var evt = new MouseEvent('mousemove', {
- view: window,
- bubbles: true,
- cancelable: true,
- clientX: rect.left + point._model.x,
- clientY: rect.top + point._model.y
- });
- // Manually trigger rather than having an async test
- node.dispatchEvent(evt);
- // Check and see if tooltip was displayed
- var tooltip = chart.tooltip;
- expect(tooltip._view instanceof Object).toBe(true);
- expect(tooltip._view.dataPoints instanceof Array).toBe(true);
- expect(tooltip._view.dataPoints.length).toEqual(1);
- expect(tooltip._view.dataPoints[0].index).toEqual(pointIndex);
- expect(tooltip._view.dataPoints[0].datasetIndex).toEqual(datasetIndex);
- expect(tooltip._view.dataPoints[0].xLabel).toEqual(
- chart.data.labels[pointIndex]
- );
- expect(tooltip._view.dataPoints[0].yLabel).toEqual(
- chart.data.datasets[datasetIndex].data[pointIndex]
- );
- expect(tooltip._view.dataPoints[0].x).toBeCloseToPixel(point._model.x);
- expect(tooltip._view.dataPoints[0].y).toBeCloseToPixel(point._model.y);
- });
- it('Should not update if active element has not changed', function() {
- var chart = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- label: 'Dataset 1',
- data: [10, 20, 30],
- pointHoverBorderColor: 'rgb(255, 0, 0)',
- pointHoverBackgroundColor: 'rgb(0, 255, 0)'
- }, {
- label: 'Dataset 2',
- data: [40, 40, 40],
- pointHoverBorderColor: 'rgb(0, 0, 255)',
- pointHoverBackgroundColor: 'rgb(0, 255, 255)'
- }],
- labels: ['Point 1', 'Point 2', 'Point 3']
- },
- options: {
- tooltips: {
- mode: 'single',
- callbacks: {
- title: function() {
- return 'registering callback...';
- }
- }
- }
- }
- });
- // Trigger an event over top of the
- var meta = chart.getDatasetMeta(0);
- var firstPoint = meta.data[1];
- var node = chart.chart.canvas;
- var rect = node.getBoundingClientRect();
- var firstEvent = new MouseEvent('mousemove', {
- view: window,
- bubbles: false,
- cancelable: true,
- clientX: rect.left + firstPoint._model.x,
- clientY: rect.top + firstPoint._model.y
- });
- var tooltip = chart.tooltip;
- spyOn(tooltip, 'update');
- /* Manually trigger rather than having an async test */
- // First dispatch change event, should update tooltip
- node.dispatchEvent(firstEvent);
- expect(tooltip.update).toHaveBeenCalledWith(true);
- // Reset calls
- tooltip.update.calls.reset();
- // Second dispatch change event (same event), should not update tooltip
- node.dispatchEvent(firstEvent);
- expect(tooltip.update).not.toHaveBeenCalled();
- });
- describe('positioners', function() {
- it('Should call custom positioner with correct parameters and scope', function() {
- Chart.Tooltip.positioners.test = function() {
- return {x: 0, y: 0};
- };
- spyOn(Chart.Tooltip.positioners, 'test').and.callThrough();
- var chart = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- label: 'Dataset 1',
- data: [10, 20, 30],
- pointHoverBorderColor: 'rgb(255, 0, 0)',
- pointHoverBackgroundColor: 'rgb(0, 255, 0)'
- }, {
- label: 'Dataset 2',
- data: [40, 40, 40],
- pointHoverBorderColor: 'rgb(0, 0, 255)',
- pointHoverBackgroundColor: 'rgb(0, 255, 255)'
- }],
- labels: ['Point 1', 'Point 2', 'Point 3']
- },
- options: {
- tooltips: {
- mode: 'nearest',
- position: 'test'
- }
- }
- });
- // Trigger an event over top of the
- var pointIndex = 1;
- var datasetIndex = 0;
- var meta = chart.getDatasetMeta(datasetIndex);
- var point = meta.data[pointIndex];
- var node = chart.canvas;
- var rect = node.getBoundingClientRect();
- var evt = new MouseEvent('mousemove', {
- view: window,
- bubbles: true,
- cancelable: true,
- clientX: rect.left + point._model.x,
- clientY: rect.top + point._model.y
- });
- // Manually trigger rather than having an async test
- node.dispatchEvent(evt);
- var fn = Chart.Tooltip.positioners.test;
- expect(fn.calls.count()).toBe(1);
- expect(fn.calls.first().args[0] instanceof Array).toBe(true);
- expect(fn.calls.first().args[1].hasOwnProperty('x')).toBe(true);
- expect(fn.calls.first().args[1].hasOwnProperty('y')).toBe(true);
- expect(fn.calls.first().object instanceof Chart.Tooltip).toBe(true);
- });
- });
- it('Should avoid tooltip truncation in x axis if there is enough space to show tooltip without truncation', function() {
- var chart = window.acquireChart({
- type: 'pie',
- data: {
- datasets: [{
- data: [
- 50,
- 50
- ],
- backgroundColor: [
- 'rgb(255, 0, 0)',
- 'rgb(0, 255, 0)'
- ],
- label: 'Dataset 1'
- }],
- labels: [
- 'Red long tooltip text to avoid unnecessary loop steps',
- 'Green long tooltip text to avoid unnecessary loop steps'
- ]
- },
- options: {
- responsive: true,
- animation: {
- // without this slice center point is calculated wrong
- animateRotate: false
- }
- }
- });
- // Trigger an event over top of the slice
- for (var slice = 0; slice < 2; slice++) {
- var meta = chart.getDatasetMeta(0);
- var point = meta.data[slice].getCenterPoint();
- var tooltipPosition = meta.data[slice].tooltipPosition();
- var node = chart.canvas;
- var rect = node.getBoundingClientRect();
- var mouseMoveEvent = new MouseEvent('mousemove', {
- view: window,
- bubbles: true,
- cancelable: true,
- clientX: rect.left + point.x,
- clientY: rect.top + point.y
- });
- var mouseOutEvent = new MouseEvent('mouseout');
- // Lets cycle while tooltip is narrower than chart area
- var infiniteCycleDefense = 70;
- for (var i = 0; i < infiniteCycleDefense; i++) {
- chart.config.data.labels[slice] = chart.config.data.labels[slice] + 'l';
- chart.update();
- node.dispatchEvent(mouseOutEvent);
- node.dispatchEvent(mouseMoveEvent);
- var model = chart.tooltip._model;
- expect(model.x).toBeGreaterThanOrEqual(0);
- if (model.width <= chart.width) {
- expect(model.x + model.width).toBeLessThanOrEqual(chart.width);
- }
- expect(model.caretX).toBe(tooltipPosition.x);
- // if tooltip is longer than chart area then all tests done
- if (model.width > chart.width) {
- break;
- }
- }
- }
- });
- });
|