helpers.options.tests.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. 'use strict';
  2. describe('Chart.helpers.options', function() {
  3. var options = Chart.helpers.options;
  4. describe('toLineHeight', function() {
  5. var toLineHeight = options.toLineHeight;
  6. it ('should support keyword values', function() {
  7. expect(toLineHeight('normal', 16)).toBe(16 * 1.2);
  8. });
  9. it ('should support unitless values', function() {
  10. expect(toLineHeight(1.4, 16)).toBe(16 * 1.4);
  11. expect(toLineHeight('1.4', 16)).toBe(16 * 1.4);
  12. });
  13. it ('should support length values', function() {
  14. expect(toLineHeight('42px', 16)).toBe(42);
  15. expect(toLineHeight('1.4em', 16)).toBe(16 * 1.4);
  16. });
  17. it ('should support percentage values', function() {
  18. expect(toLineHeight('140%', 16)).toBe(16 * 1.4);
  19. });
  20. it ('should fallback to default (1.2) for invalid values', function() {
  21. expect(toLineHeight(null, 16)).toBe(16 * 1.2);
  22. expect(toLineHeight(undefined, 16)).toBe(16 * 1.2);
  23. expect(toLineHeight('foobar', 16)).toBe(16 * 1.2);
  24. });
  25. });
  26. describe('toPadding', function() {
  27. var toPadding = options.toPadding;
  28. it ('should support number values', function() {
  29. expect(toPadding(4)).toEqual(
  30. {top: 4, right: 4, bottom: 4, left: 4, height: 8, width: 8});
  31. expect(toPadding(4.5)).toEqual(
  32. {top: 4.5, right: 4.5, bottom: 4.5, left: 4.5, height: 9, width: 9});
  33. });
  34. it ('should support string values', function() {
  35. expect(toPadding('4')).toEqual(
  36. {top: 4, right: 4, bottom: 4, left: 4, height: 8, width: 8});
  37. expect(toPadding('4.5')).toEqual(
  38. {top: 4.5, right: 4.5, bottom: 4.5, left: 4.5, height: 9, width: 9});
  39. });
  40. it ('should support object values', function() {
  41. expect(toPadding({top: 1, right: 2, bottom: 3, left: 4})).toEqual(
  42. {top: 1, right: 2, bottom: 3, left: 4, height: 4, width: 6});
  43. expect(toPadding({top: 1.5, right: 2.5, bottom: 3.5, left: 4.5})).toEqual(
  44. {top: 1.5, right: 2.5, bottom: 3.5, left: 4.5, height: 5, width: 7});
  45. expect(toPadding({top: '1', right: '2', bottom: '3', left: '4'})).toEqual(
  46. {top: 1, right: 2, bottom: 3, left: 4, height: 4, width: 6});
  47. });
  48. it ('should fallback to 0 for invalid values', function() {
  49. expect(toPadding({top: 'foo', right: 'foo', bottom: 'foo', left: 'foo'})).toEqual(
  50. {top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
  51. expect(toPadding({top: null, right: null, bottom: null, left: null})).toEqual(
  52. {top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
  53. expect(toPadding({})).toEqual(
  54. {top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
  55. expect(toPadding('foo')).toEqual(
  56. {top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
  57. expect(toPadding(null)).toEqual(
  58. {top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
  59. expect(toPadding(undefined)).toEqual(
  60. {top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
  61. });
  62. });
  63. describe('_parseFont', function() {
  64. var parseFont = options._parseFont;
  65. it ('should return a font with default values', function() {
  66. var global = Chart.defaults.global;
  67. Chart.defaults.global = {
  68. defaultFontFamily: 'foobar',
  69. defaultFontSize: 42,
  70. defaultFontStyle: 'xxxyyy',
  71. defaultLineHeight: 1.5
  72. };
  73. expect(parseFont({})).toEqual({
  74. family: 'foobar',
  75. lineHeight: 63,
  76. size: 42,
  77. string: 'xxxyyy 42px foobar',
  78. style: 'xxxyyy',
  79. weight: null
  80. });
  81. Chart.defaults.global = global;
  82. });
  83. it ('should return a font with given values', function() {
  84. expect(parseFont({
  85. fontFamily: 'bla',
  86. lineHeight: 8,
  87. fontSize: 21,
  88. fontStyle: 'zzz'
  89. })).toEqual({
  90. family: 'bla',
  91. lineHeight: 8 * 21,
  92. size: 21,
  93. string: 'zzz 21px bla',
  94. style: 'zzz',
  95. weight: null
  96. });
  97. });
  98. it('should return null as a font string if fontSize or fontFamily are missing', function() {
  99. var global = Chart.defaults.global;
  100. Chart.defaults.global = {};
  101. expect(parseFont({
  102. fontStyle: 'italic',
  103. fontSize: 12
  104. }).string).toBeNull();
  105. expect(parseFont({
  106. fontStyle: 'italic',
  107. fontFamily: 'serif'
  108. }).string).toBeNull();
  109. Chart.defaults.global = global;
  110. });
  111. it('fontStyle should be optional for font strings', function() {
  112. var global = Chart.defaults.global;
  113. Chart.defaults.global = {};
  114. expect(parseFont({
  115. fontSize: 12,
  116. fontFamily: 'serif'
  117. }).string).toBe('12px serif');
  118. Chart.defaults.global = global;
  119. });
  120. });
  121. describe('resolve', function() {
  122. var resolve = options.resolve;
  123. it ('should fallback to the first defined input', function() {
  124. expect(resolve([42])).toBe(42);
  125. expect(resolve([42, 'foo'])).toBe(42);
  126. expect(resolve([undefined, 42, 'foo'])).toBe(42);
  127. expect(resolve([42, 'foo', undefined])).toBe(42);
  128. expect(resolve([undefined])).toBe(undefined);
  129. });
  130. it ('should correctly handle empty values (null, 0, "")', function() {
  131. expect(resolve([0, 'foo'])).toBe(0);
  132. expect(resolve(['', 'foo'])).toBe('');
  133. expect(resolve([null, 'foo'])).toBe(null);
  134. });
  135. it ('should support indexable options if index is provided', function() {
  136. var input = [42, 'foo', 'bar'];
  137. expect(resolve([input], undefined, 0)).toBe(42);
  138. expect(resolve([input], undefined, 1)).toBe('foo');
  139. expect(resolve([input], undefined, 2)).toBe('bar');
  140. });
  141. it ('should fallback if an indexable option value is undefined', function() {
  142. var input = [42, undefined, 'bar'];
  143. expect(resolve([input], undefined, 5)).toBe(undefined);
  144. expect(resolve([input, 'foo'], undefined, 1)).toBe('foo');
  145. expect(resolve([input, 'foo'], undefined, 5)).toBe('foo');
  146. });
  147. it ('should not handle indexable options if index is undefined', function() {
  148. var array = [42, 'foo', 'bar'];
  149. expect(resolve([array])).toBe(array);
  150. expect(resolve([array], undefined, undefined)).toBe(array);
  151. });
  152. it ('should support scriptable options if context is provided', function() {
  153. var input = function(context) {
  154. return context.v * 2;
  155. };
  156. expect(resolve([42], {v: 42})).toBe(42);
  157. expect(resolve([input], {v: 42})).toBe(84);
  158. });
  159. it ('should fallback if a scriptable option returns undefined', function() {
  160. var input = function() {};
  161. expect(resolve([input], {v: 42})).toBe(undefined);
  162. expect(resolve([input, 'foo'], {v: 42})).toBe('foo');
  163. expect(resolve([input, undefined, 'foo'], {v: 42})).toBe('foo');
  164. });
  165. it ('should not handle scriptable options if context is undefined', function() {
  166. var input = function(context) {
  167. return context.v * 2;
  168. };
  169. expect(resolve([input])).toBe(input);
  170. expect(resolve([input], undefined)).toBe(input);
  171. });
  172. it ('should handle scriptable and indexable option', function() {
  173. var input = function(context) {
  174. return [context.v, undefined, 'bar'];
  175. };
  176. expect(resolve([input, 'foo'], {v: 42}, 0)).toBe(42);
  177. expect(resolve([input, 'foo'], {v: 42}, 1)).toBe('foo');
  178. expect(resolve([input, 'foo'], {v: 42}, 5)).toBe('foo');
  179. expect(resolve([input, ['foo', 'bar']], {v: 42}, 1)).toBe('bar');
  180. });
  181. });
  182. });