utils.spec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import Chart from 'chart.js';
  2. import utils from '../../src/utils';
  3. describe('utils.js', function() {
  4. describe('toTextLines', function() {
  5. var toTextLines = utils.toTextLines;
  6. it('should return an array containing the input string', function() {
  7. expect(toTextLines('')).toEqual(['']);
  8. expect(toTextLines('foo')).toEqual(['foo']);
  9. expect(toTextLines('foo bar')).toEqual(['foo bar']);
  10. });
  11. it('should return an array with converted values', function() {
  12. expect(toTextLines(null)).toEqual(['null']);
  13. expect(toTextLines(undefined)).toEqual(['undefined']);
  14. expect(toTextLines(42)).toEqual(['42']);
  15. expect(toTextLines(true)).toEqual(['true']);
  16. });
  17. it('should return an array of strings if inputs is an array', function() {
  18. expect(toTextLines([])).toEqual([]);
  19. expect(toTextLines(['foo'])).toEqual(['foo']);
  20. expect(toTextLines(['foo', 'bar'])).toEqual(['foo', 'bar']);
  21. });
  22. it('should split the input string if it contains \\n', function() {
  23. expect(toTextLines('foo\nbar')).toEqual(['foo', 'bar']);
  24. expect(toTextLines('foo\nbar\nbla')).toEqual(['foo', 'bar', 'bla']);
  25. });
  26. it('should preserve spaces when splitting strings', function() {
  27. expect(toTextLines('foo \n bar')).toEqual(['foo ', ' bar']);
  28. expect(toTextLines('foo \n bar \n bla')).toEqual(['foo ', ' bar ', ' bla']);
  29. });
  30. it('should flatten children arrays in the correct order', function() {
  31. expect(toTextLines(['foo', [['bar', 'xxx'], 'bla']])).toEqual(['foo', 'bar', 'xxx', 'bla']);
  32. });
  33. it('should split strings children in the correct order', function() {
  34. expect(toTextLines(['foo', [['bar\nxxx'], 'bla\nyyy']])).toEqual(['foo', 'bar', 'xxx', 'bla', 'yyy']);
  35. });
  36. });
  37. describe('toFontString', function() {
  38. var toFontString = utils.toFontString;
  39. it('should return null if the given font is invalid', function() {
  40. expect(toFontString({})).toBeNull();
  41. expect(toFontString(null)).toBeNull();
  42. expect(toFontString(undefined)).toBeNull();
  43. expect(toFontString(42)).toBeNull();
  44. expect(toFontString('foo')).toBeNull();
  45. expect(toFontString(new Date())).toBeNull();
  46. });
  47. it('should return null if size or family are missing', function() {
  48. expect(toFontString({style: 'italic', weight: 300, size: 12})).toBeNull();
  49. expect(toFontString({style: 'italic', weight: 300, family: 'serif'})).toBeNull();
  50. });
  51. it('should return the string representation of the given font', function() {
  52. expect(toFontString({style: 'italic', weight: 300, size: 12, family: 'serif'})).toBe('italic 300 12px serif');
  53. });
  54. it('weigth and style should be optional', function() {
  55. expect(toFontString({size: 12, family: 'serif'})).toBe('12px serif');
  56. expect(toFontString({style: 'italic', size: 12, family: 'serif'})).toBe('italic 12px serif');
  57. expect(toFontString({weight: 300, size: 12, family: 'serif'})).toBe('300 12px serif');
  58. });
  59. });
  60. describe('parseFont', function() {
  61. var parseFont = utils.parseFont;
  62. it ('should return a font with default values', function() {
  63. var global = Chart.defaults.global;
  64. Chart.defaults.global = {
  65. defaultFontFamily: 'foobar',
  66. defaultFontSize: 42,
  67. defaultFontStyle: 'xxxyyy'
  68. };
  69. expect(parseFont({})).toEqual({
  70. family: 'foobar',
  71. lineHeight: 50.4,
  72. size: 42,
  73. string: 'xxxyyy 42px foobar',
  74. style: 'xxxyyy',
  75. weight: null
  76. });
  77. Chart.defaults.global = global;
  78. });
  79. it ('should return a font with given values', function() {
  80. expect(parseFont({
  81. family: 'bla',
  82. lineHeight: 8,
  83. size: 21,
  84. style: 'zzz',
  85. weight: 400
  86. })).toEqual({
  87. family: 'bla',
  88. lineHeight: 8 * 21,
  89. size: 21,
  90. string: 'zzz 400 21px bla',
  91. style: 'zzz',
  92. weight: 400
  93. });
  94. });
  95. });
  96. describe('arrayDiff', function() {
  97. var arrayDiff = utils.arrayDiff;
  98. it ('should return an empty array if inputs are also empty', function() {
  99. expect(arrayDiff([], [])).toEqual([]);
  100. });
  101. it ('should return an array of [value, state] with proper state', function() {
  102. var a0 = [42, 51, 22];
  103. var a1 = [42, 11];
  104. expect(arrayDiff(a0, a1)).toEqual([[11, 1], [51, -1], [22, -1]]);
  105. expect(arrayDiff(a1, a0)).toEqual([[51, 1], [22, 1], [11, -1]]);
  106. expect(arrayDiff(a0, [])).toEqual([[42, -1], [51, -1], [22, -1]]);
  107. expect(arrayDiff([], a0)).toEqual([[42, 1], [51, 1], [22, 1]]);
  108. expect(arrayDiff(a0, a0)).toEqual([]);
  109. });
  110. it ('should not modify input arrays', function() {
  111. var a0 = [42, 51];
  112. var a1 = [42, 11];
  113. arrayDiff(a0, a1);
  114. expect(a0).toEqual([42, 51]);
  115. expect(a1).toEqual([42, 11]);
  116. });
  117. it ('should preserve value references', function() {
  118. var o0 = {};
  119. var o1 = {};
  120. var o2 = {};
  121. var a0 = [o0];
  122. var a1 = [o1, o2];
  123. var diff = arrayDiff(a0, a1);
  124. expect(diff).toEqual([[o1, 1], [o2, 1], [o0, -1]]);
  125. expect(diff[0][0]).toBe(o1);
  126. expect(diff[1][0]).toBe(o2);
  127. expect(diff[2][0]).toBe(o0);
  128. });
  129. });
  130. });