RowHeader.spec.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. describe('RowHeader', () => {
  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 row headers by default', () => {
  13. handsontable();
  14. expect(spec().$container.find('tbody th').length).toEqual(0);
  15. });
  16. it('should show row headers if true', () => {
  17. handsontable({
  18. rowHeaders: true
  19. });
  20. expect(spec().$container.find('tbody th').length).toBeGreaterThan(0);
  21. });
  22. it('should show row headers numbered 1-10 by default', () => {
  23. const startRows = 5;
  24. handsontable({
  25. startRows,
  26. rowHeaders: true
  27. });
  28. const ths = getLeftClone().find('tbody th');
  29. expect(ths.length).toEqual(startRows);
  30. expect($.trim(ths.eq(0).text())).toEqual('1');
  31. expect($.trim(ths.eq(1).text())).toEqual('2');
  32. expect($.trim(ths.eq(2).text())).toEqual('3');
  33. expect($.trim(ths.eq(3).text())).toEqual('4');
  34. expect($.trim(ths.eq(4).text())).toEqual('5');
  35. });
  36. it('should show row headers with custom label', () => {
  37. const startRows = 5;
  38. handsontable({
  39. startRows,
  40. rowHeaders: ['First', 'Second', 'Third']
  41. });
  42. const ths = getLeftClone().find('tbody th');
  43. expect(ths.length).toEqual(startRows);
  44. expect($.trim(ths.eq(0).text())).toEqual('First');
  45. expect($.trim(ths.eq(1).text())).toEqual('Second');
  46. expect($.trim(ths.eq(2).text())).toEqual('Third');
  47. expect($.trim(ths.eq(3).text())).toEqual('4');
  48. expect($.trim(ths.eq(4).text())).toEqual('5');
  49. });
  50. it('should not show row headers if false', () => {
  51. handsontable({
  52. rowHeaders: false
  53. });
  54. expect(getLeftClone().find('tbody th').length).toEqual(0);
  55. });
  56. it('should hide rows headers after updateSetting', () => {
  57. const hot = handsontable({
  58. startRows: 5,
  59. rowHeaders: true
  60. });
  61. expect(getHtCore().find('tbody th').length).toEqual(5);
  62. expect(getLeftClone().find('tbody th').length).toEqual(5);
  63. hot.updateSettings({
  64. rowHeaders: false
  65. });
  66. expect(getHtCore().find('tbody th').length).toEqual(0);
  67. });
  68. it('should show rows headers after updateSettings', () => {
  69. const hot = handsontable({
  70. startRows: 5,
  71. rowHeaders: false
  72. });
  73. expect(getHtCore().find('tbody th').length).toEqual(0);
  74. expect(getLeftClone().find('tbody th').length).toEqual(0);
  75. hot.updateSettings({
  76. rowHeaders: true
  77. });
  78. expect(getHtCore().find('tbody th').length).toEqual(5);
  79. expect(getLeftClone().find('tbody th').length).toEqual(5);
  80. });
  81. it('should show/hide rows headers after multiple updateSettings', () => {
  82. const hot = handsontable({
  83. startRows: 5,
  84. rowHeaders: false
  85. });
  86. expect(getHtCore().find('tbody th').length).toEqual(0);
  87. expect(getLeftClone().find('tbody th').length).toEqual(0);
  88. hot.updateSettings({
  89. rowHeaders: true
  90. });
  91. expect(getHtCore().find('tbody th').length).toEqual(5);
  92. expect(getLeftClone().width()).toBeGreaterThan(0);
  93. hot.updateSettings({
  94. rowHeaders: false
  95. });
  96. expect(getHtCore().find('tbody th').length).toEqual(0);
  97. expect(getLeftClone().width()).toEqual(0);
  98. hot.updateSettings({
  99. rowHeaders: true
  100. });
  101. expect(getHtCore().find('tbody th').length).toEqual(5);
  102. expect(getLeftClone().width()).toBeGreaterThan(0);
  103. });
  104. it('should show new rows headers after updateSettings', () => {
  105. const hot = handsontable({
  106. startCols: 3,
  107. rowHeaders: ['A', 'B', 'C']
  108. });
  109. const leftClone = getLeftClone();
  110. expect(leftClone.find('tbody tr:eq(0) th:eq(0)').text()).toEqual('A');
  111. expect(leftClone.find('tbody tr:eq(1) th:eq(0)').text()).toEqual('B');
  112. expect(leftClone.find('tbody tr:eq(2) th:eq(0)').text()).toEqual('C');
  113. hot.updateSettings({
  114. rowHeaders: ['X', 'Y', 'Z']
  115. });
  116. expect(leftClone.find('tbody tr:eq(0) th:eq(0)').text()).toEqual('X');
  117. expect(leftClone.find('tbody tr:eq(1) th:eq(0)').text()).toEqual('Y');
  118. expect(leftClone.find('tbody tr:eq(2) th:eq(0)').text()).toEqual('Z');
  119. });
  120. it('should allow defining custom row header width using the rowHeaderWidth config option', () => {
  121. handsontable({
  122. startCols: 3,
  123. rowHeaders: true,
  124. rowHeaderWidth: 150
  125. });
  126. expect(spec().$container.find('th').eq(0).outerWidth()).toEqual(150);
  127. expect(spec().$container.find('col').first().css('width')).toEqual('150px');
  128. });
  129. it('should allow defining custom column header heights using the columnHeaderHeight config option, when multiple column header levels are defined', () => {
  130. const hot = handsontable({
  131. startCols: 3,
  132. rowHeaders: true,
  133. rowHeaderWidth: [66, 96],
  134. afterGetRowHeaderRenderers(array) {
  135. array.push((index, TH) => {
  136. TH.innerHTML = '';
  137. const div = document.createElement('div');
  138. const span = document.createElement('span');
  139. div.className = 'relative';
  140. span.className = 'rowHeader';
  141. span.innerText = index;
  142. div.appendChild(span);
  143. TH.appendChild(div);
  144. });
  145. return array;
  146. }
  147. });
  148. hot.render();
  149. expect(spec().$container.find('.handsontable.ht_clone_left tr:nth-child(1) th:nth-child(1)').outerWidth()).toEqual(66);
  150. expect(spec().$container.find('.handsontable.ht_clone_left tr:nth-child(1) th:nth-child(2)').outerWidth()).toEqual(96);
  151. expect(spec().$container.find('col').first().css('width')).toEqual('66px');
  152. expect(spec().$container.find('col').eq(1).css('width')).toEqual('96px');
  153. });
  154. });