defaults.spec.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import Chart from 'chart.js';
  2. describe('defaults.js', function() {
  3. var expected = {
  4. align: 'center',
  5. anchor: 'center',
  6. backgroundColor: null,
  7. borderColor: null,
  8. borderRadius: 0,
  9. borderWidth: 0,
  10. color: undefined,
  11. display: true,
  12. font: {
  13. family: undefined,
  14. lineHeight: 1.2,
  15. size: undefined,
  16. style: undefined,
  17. weight: null
  18. },
  19. offset: 4,
  20. opacity: 1,
  21. padding: {
  22. top: 4,
  23. right: 4,
  24. bottom: 4,
  25. left: 4
  26. },
  27. rotation: 0,
  28. textAlign: 'start',
  29. // can't test formatter?!
  30. };
  31. var plugin = Chart.plugins.getAll().filter(function(p) {
  32. return p.id === 'datalabels';
  33. })[0];
  34. it('should be registered as global plugin options', function() {
  35. var globals = Chart.defaults.global.plugins.datalabels;
  36. expect(globals).toEqual(jasmine.objectContaining(expected));
  37. });
  38. it('should be called with default options', function() {
  39. var spy = spyOn(plugin, 'afterDatasetUpdate');
  40. var chart = jasmine.chart.acquire({
  41. type: 'line',
  42. data: {
  43. datasets: [{
  44. data: []
  45. }]
  46. }
  47. });
  48. expect(spy).toHaveBeenCalled();
  49. var args = spy.calls.first().args;
  50. expect(args[0]).toBe(chart);
  51. expect(args[2]).toEqual(jasmine.objectContaining(expected));
  52. expect(args[2].formatter).toBe(Chart.defaults.global.plugins.datalabels.formatter);
  53. });
  54. describe('default formatter', function() {
  55. var formatter = Chart.defaults.global.plugins.datalabels.formatter;
  56. it('should null if value is null or undefined', function() {
  57. expect(formatter()).toBeNull();
  58. expect(formatter(null)).toBeNull();
  59. expect(formatter(undefined)).toBeNull();
  60. });
  61. it('should return input strings unchanged', function() {
  62. expect(formatter('')).toBe('');
  63. expect(formatter('foo')).toBe('foo');
  64. expect(formatter('foo\nbar')).toBe('foo\nbar');
  65. });
  66. it('should convert numbers and booleans to strings', function() {
  67. expect(formatter(42)).toBe('42');
  68. expect(formatter(42.5)).toBe('42.5');
  69. expect(formatter(true)).toBe('true');
  70. expect(formatter(false)).toBe('false');
  71. });
  72. it('should convert dates to formatted strings', function() {
  73. var now = new Date();
  74. expect(typeof formatter(now)).toBe('string');
  75. expect(formatter(now)).toBe(now.toString());
  76. });
  77. it('should return value.label if defined', function() {
  78. expect(formatter({label: 42, r: 51})).toBe('42');
  79. expect(formatter({label: 'foo', r: 51})).toBe('foo');
  80. });
  81. it('should return value.r if value.label is undefined', function() {
  82. expect(formatter({r: 42})).toBe('42');
  83. expect(formatter({r: 'foo'})).toBe('foo');
  84. });
  85. it('should return serialized object values if value.label|r are undefined', function() {
  86. expect(formatter({a: 'foo', b: 42, c: true})).toBe('a: foo, b: 42, c: true');
  87. });
  88. });
  89. });