ColHeader.spec.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. describe('ColHeader', () => {
  2. const id = 'testContainer';
  3. beforeEach(function() {
  4. this.$container = $(`<div id="${id}"></div>`).appendTo('body');
  5. });
  6. afterEach(function() {
  7. if (this.$container) {
  8. destroy();
  9. this.$container.remove();
  10. }
  11. });
  12. it('should not show col headers by default', () => {
  13. handsontable();
  14. expect(spec().$container.find('thead th').length).toEqual(0);
  15. });
  16. it('should show col headers if true', () => {
  17. handsontable({
  18. colHeaders: true
  19. });
  20. expect(spec().$container.find('thead th').length).toBeGreaterThan(0);
  21. });
  22. it('should show default columns headers labelled A-(Z * n)', () => {
  23. const startCols = 5;
  24. handsontable({
  25. startCols,
  26. colHeaders: true
  27. });
  28. const ths = getHtCore().find('thead th');
  29. expect(ths.length).toEqual(startCols);
  30. expect($.trim(ths.eq(0).text())).toEqual('A');
  31. expect($.trim(ths.eq(1).text())).toEqual('B');
  32. expect($.trim(ths.eq(2).text())).toEqual('C');
  33. expect($.trim(ths.eq(3).text())).toEqual('D');
  34. expect($.trim(ths.eq(4).text())).toEqual('E');
  35. });
  36. it('should show default columns headers labelled A-(Z * n) when columns as an array is present', () => {
  37. const startCols = 5;
  38. handsontable({
  39. startCols,
  40. colHeaders: true,
  41. columns: [{}, {}, {}, {}, {}]
  42. });
  43. const ths = getHtCore().find('thead th');
  44. expect(ths.length).toEqual(startCols);
  45. expect($.trim(ths.eq(0).text())).toEqual('A');
  46. expect($.trim(ths.eq(1).text())).toEqual('B');
  47. expect($.trim(ths.eq(2).text())).toEqual('C');
  48. expect($.trim(ths.eq(3).text())).toEqual('D');
  49. expect($.trim(ths.eq(4).text())).toEqual('E');
  50. });
  51. it('should show default columns headers labelled A-(Z * n) when columns as a function is present', () => {
  52. const startCols = 5;
  53. handsontable({
  54. startCols,
  55. colHeaders: true,
  56. columns() {
  57. return {};
  58. }
  59. });
  60. const ths = getHtCore().find('thead th');
  61. expect(ths.length).toEqual(startCols);
  62. expect($.trim(ths.eq(0).text())).toEqual('A');
  63. expect($.trim(ths.eq(1).text())).toEqual('B');
  64. expect($.trim(ths.eq(2).text())).toEqual('C');
  65. expect($.trim(ths.eq(3).text())).toEqual('D');
  66. expect($.trim(ths.eq(4).text())).toEqual('E');
  67. });
  68. it('should show col headers with custom label', () => {
  69. const startCols = 5;
  70. handsontable({
  71. startCols,
  72. colHeaders: ['First', 'Second', 'Third']
  73. });
  74. const ths = getHtCore().find('thead th');
  75. expect(ths.length).toEqual(startCols);
  76. expect($.trim(ths.eq(0).text())).toEqual('First');
  77. expect($.trim(ths.eq(1).text())).toEqual('Second');
  78. expect($.trim(ths.eq(2).text())).toEqual('Third');
  79. expect($.trim(ths.eq(3).text())).toEqual('D');
  80. expect($.trim(ths.eq(4).text())).toEqual('E');
  81. });
  82. it('should not show col headers if false', () => {
  83. handsontable({
  84. colHeaders: false
  85. });
  86. expect(spec().$container.find('th.htColHeader').length).toEqual(0);
  87. });
  88. it('should hide columns headers after updateSettings', () => {
  89. const hot = handsontable({
  90. startCols: 5,
  91. colHeaders: true
  92. });
  93. expect(getHtCore().find('thead th').length).toEqual(5);
  94. expect(getTopClone().find('thead th').length).toEqual(5);
  95. hot.updateSettings({
  96. colHeaders: false
  97. });
  98. expect(getHtCore().find('thead th').length).toEqual(0);
  99. expect(getTopClone().width()).toEqual(0);
  100. });
  101. it('should show/hide columns headers after updateSettings', () => {
  102. const hot = handsontable({
  103. startCols: 5,
  104. colHeaders: true
  105. });
  106. expect(getHtCore().find('thead th').length).toEqual(5);
  107. expect(getTopClone().find('thead th').length).toEqual(5);
  108. hot.updateSettings({
  109. colHeaders: false
  110. });
  111. expect(getHtCore().find('thead th').length).toEqual(0);
  112. expect(getTopClone().width()).toEqual(0);
  113. hot.updateSettings({
  114. colHeaders: true
  115. });
  116. expect(getHtCore().find('thead th').length).toEqual(5);
  117. expect(getTopClone().width()).toBeGreaterThan(0);
  118. hot.updateSettings({
  119. colHeaders: false
  120. });
  121. expect(getHtCore().find('thead th').length).toEqual(0);
  122. expect(getTopClone().width()).toEqual(0);
  123. });
  124. it('should show columns headers after updateSettings', () => {
  125. const hot = handsontable({
  126. startCols: 5,
  127. colHeaders: false
  128. });
  129. expect(getHtCore().find('thead th').length).toEqual(0);
  130. expect(getTopClone().find('thead th').length).toEqual(0);
  131. hot.updateSettings({
  132. colHeaders: true
  133. });
  134. expect(getHtCore().find('thead th').length).toEqual(5);
  135. expect(getTopClone().find('thead th').length).toEqual(5);
  136. });
  137. it('should show new columns headers after updateSettings', () => {
  138. const hot = handsontable({
  139. startCols: 3,
  140. colHeaders: ['A', 'B', 'C']
  141. });
  142. const htCore = getHtCore();
  143. expect(htCore.find('thead th:eq(0)').text()).toEqual('A');
  144. expect(htCore.find('thead th:eq(1)').text()).toEqual('B');
  145. expect(htCore.find('thead th:eq(2)').text()).toEqual('C');
  146. hot.updateSettings({
  147. colHeaders: ['X', 'Y', 'Z']
  148. });
  149. expect(htCore.find('thead th:eq(0)').text()).toEqual('X');
  150. expect(htCore.find('thead th:eq(1)').text()).toEqual('Y');
  151. expect(htCore.find('thead th:eq(2)').text()).toEqual('Z');
  152. });
  153. it('should be possible to define colHeaders with a function', () => {
  154. handsontable({
  155. startCols: 2,
  156. colHeaders(col) {
  157. switch (col) {
  158. case 0:
  159. return 'One';
  160. case 1:
  161. return 'Two';
  162. default:
  163. break;
  164. }
  165. }
  166. });
  167. const htCore = getHtCore();
  168. expect(htCore.find('thead th:eq(0)').text()).toEqual('One');
  169. expect(htCore.find('thead th:eq(1)').text()).toEqual('Two');
  170. });
  171. it('should be possible to set HTML in colHeaders', () => {
  172. handsontable({
  173. startCols: 2,
  174. colHeaders: ['One <input type="checkbox">', 'Two <input type="checkbox">']
  175. });
  176. const htCore = getHtCore();
  177. expect(htCore.find('thead th:eq(0) input[type=checkbox]').length).toEqual(1);
  178. expect(htCore.find('thead th:eq(1) input[type=checkbox]').length).toEqual(1);
  179. });
  180. it('should be possible to set colHeaders when columns array is present', () => {
  181. handsontable({
  182. startCols: 2,
  183. colHeaders: ['One', 'Two'],
  184. columns: [
  185. { type: 'text' },
  186. { type: 'text' }
  187. ]
  188. });
  189. const htCore = getHtCore();
  190. expect(htCore.find('thead th:eq(0)').text()).toEqual('One');
  191. expect(htCore.find('thead th:eq(1)').text()).toEqual('Two');
  192. });
  193. it('should be possible to set colHeaders when columns function is present', () => {
  194. handsontable({
  195. startCols: 2,
  196. colHeaders: ['One', 'Two'],
  197. columns(column) {
  198. let colMeta = { type: 'text' };
  199. if ([0, 1].indexOf(column) < 0) {
  200. colMeta = null;
  201. }
  202. return colMeta;
  203. }
  204. });
  205. const htCore = getHtCore();
  206. expect(htCore.find('thead th:eq(0)').text()).toEqual('One');
  207. expect(htCore.find('thead th:eq(1)').text()).toEqual('Two');
  208. });
  209. it('should be possible to set colHeaders using columns title property', () => {
  210. handsontable({
  211. startCols: 2,
  212. colHeaders: ['One', 'Two'],
  213. columns: [
  214. { type: 'text', title: 'Special title' },
  215. { type: 'text' }
  216. ]
  217. });
  218. const htCore = getHtCore();
  219. expect(htCore.find('thead th:eq(0)').text()).toEqual('Special title');
  220. expect(htCore.find('thead th:eq(1)').text()).toEqual('Two');
  221. });
  222. it('should be possible to set colHeaders using columns title property when columns is a function', () => {
  223. handsontable({
  224. startCols: 2,
  225. colHeaders: ['One', 'Two'],
  226. columns(column) {
  227. let colMeta = { type: 'text' };
  228. if (column === 0) {
  229. colMeta.title = 'Special title';
  230. }
  231. if ([0, 1].indexOf(column) < 0) {
  232. colMeta = null;
  233. }
  234. return colMeta;
  235. }
  236. });
  237. const htCore = getHtCore();
  238. expect(htCore.find('thead th:eq(0)').text()).toEqual('Special title');
  239. expect(htCore.find('thead th:eq(1)').text()).toEqual('Two');
  240. });
  241. it('should resize all the column headers in the overlays, according to the other overlays\' height', () => {
  242. handsontable({
  243. startCols: 5,
  244. colHeaders: ['a', 'a', 'a', 'a<BR>a', 'a'],
  245. fixedColumnsLeft: 2
  246. });
  247. const topHeaderExample = $('.ht_clone_top').find('thead tr:first-child th:nth-child(1)');
  248. const masterHeaderExample = $('.ht_master').find('thead tr:first-child th:nth-child(3)');
  249. expect(topHeaderExample.height()).toEqual(masterHeaderExample.height());
  250. });
  251. it('should allow defining custom column header height using the columnHeaderHeight config option', () => {
  252. const hot = handsontable({
  253. startCols: 3,
  254. colHeaders: true,
  255. columnHeaderHeight: 40
  256. });
  257. hot.render();
  258. expect(spec().$container.find('th').eq(0).height()).toEqual(40);
  259. });
  260. it('should allow defining custom column header heights using the columnHeaderHeight config option, when multiple column header levels are defined', () => {
  261. const hot = handsontable({
  262. startCols: 3,
  263. colHeaders: true,
  264. columnHeaderHeight: [45, 65],
  265. afterGetColumnHeaderRenderers(array) {
  266. array.push((index, TH) => {
  267. TH.innerHTML = '';
  268. const div = document.createElement('div');
  269. const span = document.createElement('span');
  270. div.className = 'relative';
  271. span.className = 'colHeader';
  272. span.innerText = index;
  273. div.appendChild(span);
  274. TH.appendChild(div);
  275. });
  276. return array;
  277. }
  278. });
  279. hot.render();
  280. expect(spec().$container.find('.handsontable.ht_clone_top tr:nth-child(1) th:nth-child(1)').height()).toEqual(45);
  281. expect(spec().$container.find('.handsontable.ht_clone_top tr:nth-child(2) th:nth-child(1)').height()).toEqual(65);
  282. });
  283. });