helpers.options.tests.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. describe('Chart.helpers.options', function() {
  3. var options = Chart.helpers.options;
  4. describe('toLineHeight', function() {
  5. it ('should support keyword values', function() {
  6. expect(options.toLineHeight('normal', 16)).toBe(16 * 1.2);
  7. });
  8. it ('should support unitless values', function() {
  9. expect(options.toLineHeight(1.4, 16)).toBe(16 * 1.4);
  10. expect(options.toLineHeight('1.4', 16)).toBe(16 * 1.4);
  11. });
  12. it ('should support length values', function() {
  13. expect(options.toLineHeight('42px', 16)).toBe(42);
  14. expect(options.toLineHeight('1.4em', 16)).toBe(16 * 1.4);
  15. });
  16. it ('should support percentage values', function() {
  17. expect(options.toLineHeight('140%', 16)).toBe(16 * 1.4);
  18. });
  19. it ('should fallback to default (1.2) for invalid values', function() {
  20. expect(options.toLineHeight(null, 16)).toBe(16 * 1.2);
  21. expect(options.toLineHeight(undefined, 16)).toBe(16 * 1.2);
  22. expect(options.toLineHeight('foobar', 16)).toBe(16 * 1.2);
  23. });
  24. });
  25. describe('toPadding', function() {
  26. it ('should support number values', function() {
  27. expect(options.toPadding(4)).toEqual(
  28. {top: 4, right: 4, bottom: 4, left: 4, height: 8, width: 8});
  29. expect(options.toPadding(4.5)).toEqual(
  30. {top: 4.5, right: 4.5, bottom: 4.5, left: 4.5, height: 9, width: 9});
  31. });
  32. it ('should support string values', function() {
  33. expect(options.toPadding('4')).toEqual(
  34. {top: 4, right: 4, bottom: 4, left: 4, height: 8, width: 8});
  35. expect(options.toPadding('4.5')).toEqual(
  36. {top: 4.5, right: 4.5, bottom: 4.5, left: 4.5, height: 9, width: 9});
  37. });
  38. it ('should support object values', function() {
  39. expect(options.toPadding({top: 1, right: 2, bottom: 3, left: 4})).toEqual(
  40. {top: 1, right: 2, bottom: 3, left: 4, height: 4, width: 6});
  41. expect(options.toPadding({top: 1.5, right: 2.5, bottom: 3.5, left: 4.5})).toEqual(
  42. {top: 1.5, right: 2.5, bottom: 3.5, left: 4.5, height: 5, width: 7});
  43. expect(options.toPadding({top: '1', right: '2', bottom: '3', left: '4'})).toEqual(
  44. {top: 1, right: 2, bottom: 3, left: 4, height: 4, width: 6});
  45. });
  46. it ('should fallback to 0 for invalid values', function() {
  47. expect(options.toPadding({top: 'foo', right: 'foo', bottom: 'foo', left: 'foo'})).toEqual(
  48. {top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
  49. expect(options.toPadding({top: null, right: null, bottom: null, left: null})).toEqual(
  50. {top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
  51. expect(options.toPadding({})).toEqual(
  52. {top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
  53. expect(options.toPadding('foo')).toEqual(
  54. {top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
  55. expect(options.toPadding(null)).toEqual(
  56. {top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
  57. expect(options.toPadding(undefined)).toEqual(
  58. {top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
  59. });
  60. });
  61. describe('resolve', function() {
  62. it ('should fallback to the first defined input', function() {
  63. expect(options.resolve([42])).toBe(42);
  64. expect(options.resolve([42, 'foo'])).toBe(42);
  65. expect(options.resolve([undefined, 42, 'foo'])).toBe(42);
  66. expect(options.resolve([42, 'foo', undefined])).toBe(42);
  67. expect(options.resolve([undefined])).toBe(undefined);
  68. });
  69. it ('should correctly handle empty values (null, 0, "")', function() {
  70. expect(options.resolve([0, 'foo'])).toBe(0);
  71. expect(options.resolve(['', 'foo'])).toBe('');
  72. expect(options.resolve([null, 'foo'])).toBe(null);
  73. });
  74. it ('should support indexable options if index is provided', function() {
  75. var input = [42, 'foo', 'bar'];
  76. expect(options.resolve([input], undefined, 0)).toBe(42);
  77. expect(options.resolve([input], undefined, 1)).toBe('foo');
  78. expect(options.resolve([input], undefined, 2)).toBe('bar');
  79. });
  80. it ('should fallback if an indexable option value is undefined', function() {
  81. var input = [42, undefined, 'bar'];
  82. expect(options.resolve([input], undefined, 5)).toBe(undefined);
  83. expect(options.resolve([input, 'foo'], undefined, 1)).toBe('foo');
  84. expect(options.resolve([input, 'foo'], undefined, 5)).toBe('foo');
  85. });
  86. it ('should not handle indexable options if index is undefined', function() {
  87. var array = [42, 'foo', 'bar'];
  88. expect(options.resolve([array])).toBe(array);
  89. expect(options.resolve([array], undefined, undefined)).toBe(array);
  90. });
  91. it ('should support scriptable options if context is provided', function() {
  92. var input = function(context) {
  93. return context.v * 2;
  94. };
  95. expect(options.resolve([42], {v: 42})).toBe(42);
  96. expect(options.resolve([input], {v: 42})).toBe(84);
  97. });
  98. it ('should fallback if a scriptable option returns undefined', function() {
  99. var input = function() {};
  100. expect(options.resolve([input], {v: 42})).toBe(undefined);
  101. expect(options.resolve([input, 'foo'], {v: 42})).toBe('foo');
  102. expect(options.resolve([input, undefined, 'foo'], {v: 42})).toBe('foo');
  103. });
  104. it ('should not handle scriptable options if context is undefined', function() {
  105. var input = function(context) {
  106. return context.v * 2;
  107. };
  108. expect(options.resolve([input])).toBe(input);
  109. expect(options.resolve([input], undefined)).toBe(input);
  110. });
  111. it ('should handle scriptable and indexable option', function() {
  112. var input = function(context) {
  113. return [context.v, undefined, 'bar'];
  114. };
  115. expect(options.resolve([input, 'foo'], {v: 42}, 0)).toBe(42);
  116. expect(options.resolve([input, 'foo'], {v: 42}, 1)).toBe('foo');
  117. expect(options.resolve([input, 'foo'], {v: 42}, 5)).toBe('foo');
  118. expect(options.resolve([input, ['foo', 'bar']], {v: 42}, 1)).toBe('bar');
  119. });
  120. });
  121. });