You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.0KB

  1. describe('Chart.plugins', function() {
  2. var oldPlugins;
  3. beforeAll(function() {
  4. oldPlugins = Chart.plugins.getAll();
  5. });
  6. afterAll(function() {
  7. Chart.plugins.register(oldPlugins);
  8. });
  9. beforeEach(function() {
  10. Chart.plugins.clear();
  11. });
  12. describe('Chart.plugins.register', function() {
  13. it('should register a plugin', function() {
  14. Chart.plugins.register({});
  15. expect(Chart.plugins.count()).toBe(1);
  16. Chart.plugins.register({});
  17. expect(Chart.plugins.count()).toBe(2);
  18. });
  19. it('should register an array of plugins', function() {
  20. Chart.plugins.register([{}, {}, {}]);
  21. expect(Chart.plugins.count()).toBe(3);
  22. });
  23. it('should succeed to register an already registered plugin', function() {
  24. var plugin = {};
  25. Chart.plugins.register(plugin);
  26. expect(Chart.plugins.count()).toBe(1);
  27. Chart.plugins.register(plugin);
  28. expect(Chart.plugins.count()).toBe(1);
  29. Chart.plugins.register([{}, plugin, plugin]);
  30. expect(Chart.plugins.count()).toBe(2);
  31. });
  32. });
  33. describe('Chart.plugins.unregister', function() {
  34. it('should unregister a plugin', function() {
  35. var plugin = {};
  36. Chart.plugins.register(plugin);
  37. expect(Chart.plugins.count()).toBe(1);
  38. Chart.plugins.unregister(plugin);
  39. expect(Chart.plugins.count()).toBe(0);
  40. });
  41. it('should unregister an array of plugins', function() {
  42. var plugins = [{}, {}, {}];
  43. Chart.plugins.register(plugins);
  44. expect(Chart.plugins.count()).toBe(3);
  45. Chart.plugins.unregister(plugins.slice(0, 2));
  46. expect(Chart.plugins.count()).toBe(1);
  47. });
  48. it('should succeed to unregister a plugin not registered', function() {
  49. var plugin = {};
  50. Chart.plugins.register(plugin);
  51. expect(Chart.plugins.count()).toBe(1);
  52. Chart.plugins.unregister({});
  53. expect(Chart.plugins.count()).toBe(1);
  54. Chart.plugins.unregister([{}, plugin]);
  55. expect(Chart.plugins.count()).toBe(0);
  56. });
  57. });
  58. describe('Chart.plugins.notify', function() {
  59. it('should call plugins with arguments', function() {
  60. var myplugin = {
  61. count: 0,
  62. trigger: function(chart) {
  63. myplugin.count = chart.count;
  64. }
  65. };
  66. Chart.plugins.register(myplugin);
  67. Chart.plugins.notify('trigger', [{ count: 10 }]);
  68. expect(myplugin.count).toBe(10);
  69. });
  70. it('should return TRUE if no plugin explicitly returns FALSE', function() {
  71. Chart.plugins.register({ check: function() {} });
  72. Chart.plugins.register({ check: function() { return; } });
  73. Chart.plugins.register({ check: function() { return null; } });
  74. Chart.plugins.register({ check: function() { return 42 } });
  75. var res = Chart.plugins.notify('check');
  76. expect(res).toBeTruthy();
  77. });
  78. it('should return FALSE if no plugin explicitly returns FALSE', function() {
  79. Chart.plugins.register({ check: function() {} });
  80. Chart.plugins.register({ check: function() { return; } });
  81. Chart.plugins.register({ check: function() { return false; } });
  82. Chart.plugins.register({ check: function() { return 42 } });
  83. var res = Chart.plugins.notify('check');
  84. expect(res).toBeFalsy();
  85. });
  86. });
  87. });