plugin.filler.tests.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. describe('Plugin.filler', function() {
  2. function decodedFillValues(chart) {
  3. return chart.data.datasets.map(function(dataset, index) {
  4. var meta = chart.getDatasetMeta(index) || {};
  5. expect(meta.$filler).toBeDefined();
  6. return meta.$filler.fill;
  7. });
  8. }
  9. describe('auto', jasmine.fixture.specs('plugin.filler'));
  10. describe('dataset.fill', function() {
  11. it('should support boundaries', function() {
  12. var chart = window.acquireChart({
  13. type: 'line',
  14. data: {
  15. datasets: [
  16. {fill: 'origin'},
  17. {fill: 'start'},
  18. {fill: 'end'},
  19. ]
  20. }
  21. });
  22. expect(decodedFillValues(chart)).toEqual(['origin', 'start', 'end']);
  23. });
  24. it('should support absolute dataset index', function() {
  25. var chart = window.acquireChart({
  26. type: 'line',
  27. data: {
  28. datasets: [
  29. {fill: 1},
  30. {fill: 3},
  31. {fill: 0},
  32. {fill: 2},
  33. ]
  34. }
  35. });
  36. expect(decodedFillValues(chart)).toEqual([1, 3, 0, 2]);
  37. });
  38. it('should support relative dataset index', function() {
  39. var chart = window.acquireChart({
  40. type: 'line',
  41. data: {
  42. datasets: [
  43. {fill: '+3'},
  44. {fill: '-1'},
  45. {fill: '+1'},
  46. {fill: '-2'},
  47. ]
  48. }
  49. });
  50. expect(decodedFillValues(chart)).toEqual([
  51. 3, // 0 + 3
  52. 0, // 1 - 1
  53. 3, // 2 + 1
  54. 1, // 3 - 2
  55. ]);
  56. });
  57. it('should handle default fill when true (origin)', function() {
  58. var chart = window.acquireChart({
  59. type: 'line',
  60. data: {
  61. datasets: [
  62. {fill: true},
  63. {fill: false},
  64. ]
  65. }
  66. });
  67. expect(decodedFillValues(chart)).toEqual(['origin', false]);
  68. });
  69. it('should ignore self dataset index', function() {
  70. var chart = window.acquireChart({
  71. type: 'line',
  72. data: {
  73. datasets: [
  74. {fill: 0},
  75. {fill: '-0'},
  76. {fill: '+0'},
  77. {fill: 3},
  78. ]
  79. }
  80. });
  81. expect(decodedFillValues(chart)).toEqual([
  82. false, // 0 === 0
  83. false, // 1 === 1 - 0
  84. false, // 2 === 2 + 0
  85. false, // 3 === 3
  86. ]);
  87. });
  88. it('should ignore out of bounds dataset index', function() {
  89. var chart = window.acquireChart({
  90. type: 'line',
  91. data: {
  92. datasets: [
  93. {fill: -2},
  94. {fill: 4},
  95. {fill: '-3'},
  96. {fill: '+1'},
  97. ]
  98. }
  99. });
  100. expect(decodedFillValues(chart)).toEqual([
  101. false, // 0 - 2 < 0
  102. false, // 1 + 4 > 3
  103. false, // 2 - 3 < 0
  104. false, // 3 + 1 > 3
  105. ]);
  106. });
  107. it('should ignore invalid values', function() {
  108. var chart = window.acquireChart({
  109. type: 'line',
  110. data: {
  111. datasets: [
  112. {fill: 'foo'},
  113. {fill: '+foo'},
  114. {fill: '-foo'},
  115. {fill: '+1.1'},
  116. {fill: '-2.2'},
  117. {fill: 3.3},
  118. {fill: -4.4},
  119. {fill: NaN},
  120. {fill: Infinity},
  121. {fill: ''},
  122. {fill: null},
  123. {fill: []},
  124. {fill: {}},
  125. ]
  126. }
  127. });
  128. expect(decodedFillValues(chart)).toEqual([
  129. false, // NaN (string)
  130. false, // NaN (string)
  131. false, // NaN (string)
  132. false, // float (string)
  133. false, // float (string)
  134. false, // float (number)
  135. false, // float (number)
  136. false, // NaN
  137. false, // !isFinite
  138. false, // empty string
  139. false, // null
  140. false, // array
  141. false, // object
  142. ]);
  143. });
  144. });
  145. describe('options.plugins.filler.propagate', function() {
  146. it('should compute propagated fill targets if true', function() {
  147. var chart = window.acquireChart({
  148. type: 'line',
  149. data: {
  150. datasets: [
  151. {fill: 'start', hidden: true},
  152. {fill: '-1', hidden: true},
  153. {fill: 1, hidden: true},
  154. {fill: '-2', hidden: true},
  155. {fill: '+1'},
  156. {fill: '+2'},
  157. {fill: '-1'},
  158. {fill: 'end', hidden: true},
  159. ]
  160. },
  161. options: {
  162. plugins: {
  163. filler: {
  164. propagate: true
  165. }
  166. }
  167. }
  168. });
  169. expect(decodedFillValues(chart)).toEqual([
  170. 'start', // 'start'
  171. 'start', // 1 - 1 -> 0 (hidden) -> 'start'
  172. 'start', // 1 (hidden) -> 0 (hidden) -> 'start'
  173. 'start', // 3 - 2 -> 1 (hidden) -> 0 (hidden) -> 'start'
  174. 5, // 4 + 1
  175. 'end', // 5 + 2 -> 7 (hidden) -> 'end'
  176. 5, // 6 - 1 -> 5
  177. 'end', // 'end'
  178. ]);
  179. });
  180. it('should preserve initial fill targets if false', function() {
  181. var chart = window.acquireChart({
  182. type: 'line',
  183. data: {
  184. datasets: [
  185. {fill: 'start', hidden: true},
  186. {fill: '-1', hidden: true},
  187. {fill: 1, hidden: true},
  188. {fill: '-2', hidden: true},
  189. {fill: '+1'},
  190. {fill: '+2'},
  191. {fill: '-1'},
  192. {fill: 'end', hidden: true},
  193. ]
  194. },
  195. options: {
  196. plugins: {
  197. filler: {
  198. propagate: false
  199. }
  200. }
  201. }
  202. });
  203. expect(decodedFillValues(chart)).toEqual([
  204. 'start', // 'origin'
  205. 0, // 1 - 1
  206. 1, // 1
  207. 1, // 3 - 2
  208. 5, // 4 + 1
  209. 7, // 5 + 2
  210. 5, // 6 - 1
  211. 'end', // 'end'
  212. ]);
  213. });
  214. it('should prevent recursive propagation', function() {
  215. var chart = window.acquireChart({
  216. type: 'line',
  217. data: {
  218. datasets: [
  219. {fill: '+2', hidden: true},
  220. {fill: '-1', hidden: true},
  221. {fill: '-1', hidden: true},
  222. {fill: '-2'}
  223. ]
  224. },
  225. options: {
  226. plugins: {
  227. filler: {
  228. propagate: true
  229. }
  230. }
  231. }
  232. });
  233. expect(decodedFillValues(chart)).toEqual([
  234. false, // 0 + 2 -> 2 (hidden) -> 1 (hidden) -> 0 (loop)
  235. false, // 1 - 1 -> 0 (hidden) -> 2 (hidden) -> 1 (loop)
  236. false, // 2 - 1 -> 1 (hidden) -> 0 (hidden) -> 2 (loop)
  237. false, // 3 - 2 -> 1 (hidden) -> 0 (hidden) -> 2 (hidden) -> 1 (loop)
  238. ]);
  239. });
  240. });
  241. });