2016-06-10 22:27:06 +02:00
|
|
|
describe('Chart.plugins', function() {
|
2016-06-10 22:26:55 +02:00
|
|
|
describe('Chart.plugins.notify', function() {
|
2016-11-03 22:40:47 +01:00
|
|
|
it('should call inline plugins with arguments', function() {
|
|
|
|
var plugin = {hook: function() {}};
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
plugins: [plugin]
|
|
|
|
});
|
|
|
|
|
|
|
|
spyOn(plugin, 'hook');
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
chart._plugins.notify(chart, 'hook', 42);
|
2016-11-03 22:40:47 +01:00
|
|
|
expect(plugin.hook.calls.count()).toBe(1);
|
|
|
|
expect(plugin.hook.calls.first().args[0]).toBe(chart);
|
|
|
|
expect(plugin.hook.calls.first().args[1]).toBe(42);
|
|
|
|
expect(plugin.hook.calls.first().args[2]).toEqual({});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call global plugins with arguments', function() {
|
2020-07-12 00:08:45 +02:00
|
|
|
var plugin = {id: 'a', hook: function() {}};
|
2016-11-03 22:40:47 +01:00
|
|
|
var chart = window.acquireChart({});
|
|
|
|
|
|
|
|
spyOn(plugin, 'hook');
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
Chart.register(plugin);
|
|
|
|
chart._plugins.notify(chart, 'hook', 42);
|
2016-11-03 22:40:47 +01:00
|
|
|
expect(plugin.hook.calls.count()).toBe(1);
|
|
|
|
expect(plugin.hook.calls.first().args[0]).toBe(chart);
|
|
|
|
expect(plugin.hook.calls.first().args[1]).toBe(42);
|
|
|
|
expect(plugin.hook.calls.first().args[2]).toEqual({});
|
2020-07-12 00:08:45 +02:00
|
|
|
Chart.unregister(plugin);
|
2016-11-03 22:40:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call plugin only once even if registered multiple times', function() {
|
2020-07-12 00:08:45 +02:00
|
|
|
var plugin = {id: 'test', hook: function() {}};
|
2016-11-03 22:40:47 +01:00
|
|
|
var chart = window.acquireChart({
|
|
|
|
plugins: [plugin, plugin]
|
|
|
|
});
|
|
|
|
|
|
|
|
spyOn(plugin, 'hook');
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
Chart.register([plugin, plugin]);
|
|
|
|
chart._plugins.notify(chart, 'hook');
|
2016-11-03 22:40:47 +01:00
|
|
|
expect(plugin.hook.calls.count()).toBe(1);
|
2020-07-12 00:08:45 +02:00
|
|
|
Chart.unregister(plugin);
|
2016-11-03 22:40:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call plugins in the correct order (global first)', function() {
|
|
|
|
var results = [];
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
plugins: [{
|
|
|
|
hook: function() {
|
|
|
|
results.push(1);
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
hook: function() {
|
|
|
|
results.push(2);
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
hook: function() {
|
|
|
|
results.push(3);
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
var plugins = [{
|
|
|
|
id: 'a',
|
2016-11-03 22:40:47 +01:00
|
|
|
hook: function() {
|
|
|
|
results.push(4);
|
2016-06-10 22:26:55 +02:00
|
|
|
}
|
2016-11-03 22:40:47 +01:00
|
|
|
}, {
|
2020-07-12 00:08:45 +02:00
|
|
|
id: 'b',
|
2016-11-03 22:40:47 +01:00
|
|
|
hook: function() {
|
|
|
|
results.push(5);
|
|
|
|
}
|
|
|
|
}, {
|
2020-07-12 00:08:45 +02:00
|
|
|
id: 'c',
|
2016-11-03 22:40:47 +01:00
|
|
|
hook: function() {
|
|
|
|
results.push(6);
|
|
|
|
}
|
2020-07-12 00:08:45 +02:00
|
|
|
}];
|
|
|
|
Chart.register(plugins);
|
2016-06-10 22:26:55 +02:00
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
var ret = chart._plugins.notify(chart, 'hook');
|
2016-11-03 22:40:47 +01:00
|
|
|
expect(ret).toBeTruthy();
|
|
|
|
expect(results).toEqual([4, 5, 6, 1, 2, 3]);
|
2020-07-12 00:08:45 +02:00
|
|
|
Chart.unregister(plugins);
|
2016-06-10 22:26:55 +02:00
|
|
|
});
|
2016-06-10 22:26:35 +02:00
|
|
|
|
2016-06-10 22:26:55 +02:00
|
|
|
it('should return TRUE if no plugin explicitly returns FALSE', function() {
|
2016-11-03 22:40:47 +01:00
|
|
|
var chart = window.acquireChart({
|
|
|
|
plugins: [{
|
|
|
|
hook: function() {}
|
|
|
|
}, {
|
|
|
|
hook: function() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
hook: function() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
hook: function() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
hook: function() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}]
|
2016-10-16 23:34:59 +02:00
|
|
|
});
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
var plugins = chart.config.plugins;
|
|
|
|
plugins.forEach(function(plugin) {
|
|
|
|
spyOn(plugin, 'hook').and.callThrough();
|
2016-10-16 23:34:59 +02:00
|
|
|
});
|
2016-11-03 22:40:47 +01:00
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
var ret = chart._plugins.notify(chart, 'hook');
|
2016-11-03 22:40:47 +01:00
|
|
|
expect(ret).toBeTruthy();
|
|
|
|
plugins.forEach(function(plugin) {
|
|
|
|
expect(plugin.hook).toHaveBeenCalled();
|
2016-10-16 23:34:59 +02:00
|
|
|
});
|
2016-11-03 22:40:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return FALSE if any plugin explicitly returns FALSE', function() {
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
plugins: [{
|
|
|
|
hook: function() {}
|
|
|
|
}, {
|
|
|
|
hook: function() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
hook: function() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
hook: function() {
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
hook: function() {
|
|
|
|
return 'bar';
|
|
|
|
}
|
|
|
|
}]
|
2016-10-16 23:34:59 +02:00
|
|
|
});
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
var plugins = chart.config.plugins;
|
|
|
|
plugins.forEach(function(plugin) {
|
|
|
|
spyOn(plugin, 'hook').and.callThrough();
|
|
|
|
});
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
var ret = chart._plugins.notify(chart, 'hook');
|
2016-11-03 22:40:47 +01:00
|
|
|
expect(ret).toBeFalsy();
|
|
|
|
expect(plugins[0].hook).toHaveBeenCalled();
|
|
|
|
expect(plugins[1].hook).toHaveBeenCalled();
|
|
|
|
expect(plugins[2].hook).toHaveBeenCalled();
|
|
|
|
expect(plugins[3].hook).not.toHaveBeenCalled();
|
|
|
|
expect(plugins[4].hook).not.toHaveBeenCalled();
|
2016-06-10 22:26:55 +02:00
|
|
|
});
|
2016-11-03 22:40:47 +01:00
|
|
|
});
|
2016-04-17 18:02:33 +02:00
|
|
|
|
2016-11-03 22:40:47 +01:00
|
|
|
describe('config.options.plugins', function() {
|
|
|
|
it('should call plugins with options at last argument', function() {
|
|
|
|
var plugin = {id: 'foo', hook: function() {}};
|
2020-07-12 00:08:45 +02:00
|
|
|
|
2016-11-03 22:40:47 +01:00
|
|
|
var chart = window.acquireChart({
|
|
|
|
options: {
|
|
|
|
plugins: {
|
|
|
|
foo: {a: '123'},
|
|
|
|
}
|
|
|
|
}
|
2016-10-16 23:34:59 +02:00
|
|
|
});
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
spyOn(plugin, 'hook');
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
Chart.register(plugin);
|
|
|
|
chart._plugins.notify(chart, 'hook');
|
|
|
|
chart._plugins.notify(chart, 'hook', ['bla']);
|
|
|
|
chart._plugins.notify(chart, 'hook', ['bla', 42]);
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
expect(plugin.hook.calls.count()).toBe(3);
|
|
|
|
expect(plugin.hook.calls.argsFor(0)[1]).toEqual({a: '123'});
|
|
|
|
expect(plugin.hook.calls.argsFor(1)[2]).toEqual({a: '123'});
|
|
|
|
expect(plugin.hook.calls.argsFor(2)[3]).toEqual({a: '123'});
|
2020-07-12 00:08:45 +02:00
|
|
|
|
|
|
|
Chart.unregister(plugin);
|
2016-11-03 22:40:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call plugins with options associated to their identifier', function() {
|
|
|
|
var plugins = {
|
|
|
|
a: {id: 'a', hook: function() {}},
|
|
|
|
b: {id: 'b', hook: function() {}},
|
|
|
|
c: {id: 'c', hook: function() {}}
|
|
|
|
};
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
Chart.register(plugins.a);
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
plugins: [plugins.b, plugins.c],
|
|
|
|
options: {
|
|
|
|
plugins: {
|
|
|
|
a: {a: '123'},
|
|
|
|
b: {b: '456'},
|
|
|
|
c: {c: '789'}
|
|
|
|
}
|
2016-10-16 23:34:59 +02:00
|
|
|
}
|
|
|
|
});
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
spyOn(plugins.a, 'hook');
|
|
|
|
spyOn(plugins.b, 'hook');
|
|
|
|
spyOn(plugins.c, 'hook');
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
chart._plugins.notify(chart, 'hook');
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
expect(plugins.a.hook).toHaveBeenCalled();
|
|
|
|
expect(plugins.b.hook).toHaveBeenCalled();
|
|
|
|
expect(plugins.c.hook).toHaveBeenCalled();
|
|
|
|
expect(plugins.a.hook.calls.first().args[1]).toEqual({a: '123'});
|
|
|
|
expect(plugins.b.hook.calls.first().args[1]).toEqual({b: '456'});
|
|
|
|
expect(plugins.c.hook.calls.first().args[1]).toEqual({c: '789'});
|
2020-07-12 00:08:45 +02:00
|
|
|
|
|
|
|
Chart.unregister(plugins.a);
|
2016-11-03 22:40:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not called plugins when config.options.plugins.{id} is FALSE', function() {
|
|
|
|
var plugins = {
|
|
|
|
a: {id: 'a', hook: function() {}},
|
|
|
|
b: {id: 'b', hook: function() {}},
|
|
|
|
c: {id: 'c', hook: function() {}}
|
|
|
|
};
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
Chart.register(plugins.a);
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
plugins: [plugins.b, plugins.c],
|
|
|
|
options: {
|
|
|
|
plugins: {
|
|
|
|
a: false,
|
|
|
|
b: false
|
|
|
|
}
|
2016-10-16 23:34:59 +02:00
|
|
|
}
|
|
|
|
});
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
spyOn(plugins.a, 'hook');
|
|
|
|
spyOn(plugins.b, 'hook');
|
|
|
|
spyOn(plugins.c, 'hook');
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
chart._plugins.notify(chart, 'hook');
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
expect(plugins.a.hook).not.toHaveBeenCalled();
|
|
|
|
expect(plugins.b.hook).not.toHaveBeenCalled();
|
|
|
|
expect(plugins.c.hook).toHaveBeenCalled();
|
2020-07-12 00:08:45 +02:00
|
|
|
|
|
|
|
Chart.unregister(plugins.a);
|
2016-11-03 22:40:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call plugins with default options when plugin options is TRUE', function() {
|
2020-07-12 00:08:45 +02:00
|
|
|
var plugin = {id: 'a', hook: function() {}, defaults: {a: 42}};
|
2016-11-03 22:40:47 +01:00
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
Chart.register(plugin);
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
options: {
|
|
|
|
plugins: {
|
|
|
|
a: true
|
|
|
|
}
|
2016-10-16 23:34:59 +02:00
|
|
|
}
|
|
|
|
});
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
spyOn(plugin, 'hook');
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
chart._plugins.notify(chart, 'hook');
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
expect(plugin.hook).toHaveBeenCalled();
|
|
|
|
expect(plugin.hook.calls.first().args[1]).toEqual({a: 42});
|
2018-11-18 09:33:34 +01:00
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
Chart.unregister(plugin);
|
2016-11-03 22:40:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should call plugins with default options if plugin config options is undefined', function() {
|
2020-07-12 00:08:45 +02:00
|
|
|
var plugin = {id: 'a', hook: function() {}, defaults: {a: 'foobar'}};
|
2016-11-03 22:40:47 +01:00
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
Chart.register(plugin);
|
2016-11-03 22:40:47 +01:00
|
|
|
spyOn(plugin, 'hook');
|
|
|
|
|
|
|
|
var chart = window.acquireChart();
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
chart._plugins.notify(chart, 'hook');
|
2016-11-03 22:40:47 +01:00
|
|
|
|
|
|
|
expect(plugin.hook).toHaveBeenCalled();
|
|
|
|
expect(plugin.hook.calls.first().args[1]).toEqual({a: 'foobar'});
|
2018-01-13 14:23:50 +01:00
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
Chart.unregister(plugin);
|
2018-01-13 14:23:50 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/chartjs/Chart.js/issues/5111#issuecomment-355934167
|
2020-07-12 00:08:45 +02:00
|
|
|
it('should update plugin options', function() {
|
2018-01-13 14:23:50 +01:00
|
|
|
var plugin = {id: 'a', hook: function() {}};
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
plugins: [plugin],
|
|
|
|
options: {
|
|
|
|
plugins: {
|
|
|
|
a: {
|
|
|
|
foo: 'foo'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
spyOn(plugin, 'hook');
|
|
|
|
|
2020-07-12 00:08:45 +02:00
|
|
|
chart._plugins.notify(chart, 'hook');
|
2018-01-13 14:23:50 +01:00
|
|
|
|
|
|
|
expect(plugin.hook).toHaveBeenCalled();
|
|
|
|
expect(plugin.hook.calls.first().args[1]).toEqual({foo: 'foo'});
|
|
|
|
|
|
|
|
chart.options.plugins.a = {bar: 'bar'};
|
|
|
|
chart.update();
|
|
|
|
|
|
|
|
plugin.hook.calls.reset();
|
2020-07-12 00:08:45 +02:00
|
|
|
chart._plugins.notify(chart, 'hook');
|
2018-01-13 14:23:50 +01:00
|
|
|
|
|
|
|
expect(plugin.hook).toHaveBeenCalled();
|
|
|
|
expect(plugin.hook.calls.first().args[1]).toEqual({bar: 'bar'});
|
2016-06-10 22:26:55 +02:00
|
|
|
});
|
2016-04-17 18:02:33 +02:00
|
|
|
});
|
|
|
|
});
|