Core_getCellMeta.spec.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. describe('Core_getCellMeta', () => {
  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 get proper cell meta when indexes was modified', () => {
  13. handsontable({
  14. modifyRow(row) {
  15. return row + 10;
  16. },
  17. modifyCol(col) {
  18. return col + 10;
  19. }
  20. });
  21. const cellMeta = getCellMeta(0, 1);
  22. expect(cellMeta.row).toEqual(10);
  23. expect(cellMeta.col).toEqual(11);
  24. expect(cellMeta.visualRow).toEqual(0);
  25. expect(cellMeta.visualCol).toEqual(1);
  26. });
  27. it('should not allow manual editing of a read only cell', () => {
  28. let allCellsReadOnly = false;
  29. handsontable({
  30. cells() {
  31. return { readOnly: allCellsReadOnly };
  32. }
  33. });
  34. allCellsReadOnly = true;
  35. selectCell(2, 2);
  36. keyDown('enter');
  37. expect(isEditorVisible()).toEqual(false);
  38. });
  39. it('should allow manual editing of cell that is no longer read only', () => {
  40. let allCellsReadOnly = true;
  41. handsontable({
  42. cells() {
  43. return { readOnly: allCellsReadOnly };
  44. }
  45. });
  46. allCellsReadOnly = false;
  47. selectCell(2, 2);
  48. keyDown('enter');
  49. expect(isEditorVisible()).toEqual(true);
  50. });
  51. it('should move the selection to the cell below, when hitting the ENTER key on a read-only cell', () => {
  52. handsontable({
  53. data: Handsontable.helper.createSpreadsheetData(3, 3),
  54. cells() {
  55. return { readOnly: true };
  56. }
  57. });
  58. selectCell(0, 0);
  59. expect(getCellMeta(0, 0).readOnly).toBe(true);
  60. keyDown('enter');
  61. expect(getSelected()).toEqual([[1, 0, 1, 0]]);
  62. });
  63. it('should use default cell editor for a cell that has declared only cell renderer', () => {
  64. handsontable({
  65. cells() {
  66. return {
  67. renderer(instance, td, ...args) {
  68. // taken from demo/renderers.html
  69. Handsontable.renderers.TextRenderer.apply(this, [instance, td, ...args]);
  70. $(td).css({
  71. background: 'yellow'
  72. });
  73. }
  74. };
  75. }
  76. });
  77. selectCell(2, 2);
  78. keyDown('enter');
  79. document.activeElement.value = 'new value';
  80. destroyEditor();
  81. expect(getDataAtCell(2, 2)).toEqual('new value');
  82. });
  83. it('should allow to use type and renderer in `flat` notation', () => {
  84. handsontable({
  85. data: [
  86. [1, 2, 3, 4],
  87. [5, 6, 7, 8],
  88. [0, 9, 8, 7]
  89. ],
  90. cells(row, col) {
  91. if (row === 2 && col === 2) {
  92. return {
  93. type: 'checkbox',
  94. renderer(instance, td, ...args) {
  95. // taken from demo/renderers.html
  96. Handsontable.renderers.TextRenderer.apply(this, [instance, td, ...args]);
  97. td.style.backgroundColor = 'yellow';
  98. }
  99. };
  100. }
  101. }
  102. });
  103. expect(getCell(2, 2).style.backgroundColor).toEqual('yellow');
  104. expect(getCell(1, 1).style.backgroundColor).toEqual('');
  105. });
  106. it('this in cells should point to cellProperties', () => {
  107. let called = 0;
  108. let _row;
  109. let _this;
  110. handsontable({
  111. cells(row) {
  112. called += 1;
  113. _row = row;
  114. _this = this;
  115. }
  116. });
  117. const HOT = getInstance();
  118. expect(called).toBeGreaterThan(0);
  119. expect(_this.row).toEqual(_row);
  120. expect(_this.instance).toBe(HOT);
  121. });
  122. it('should get proper cellProperties when order of displayed rows is different than order of stored data', () => {
  123. handsontable({
  124. data: [
  125. ['C'],
  126. ['A'],
  127. ['B']
  128. ],
  129. minSpareRows: 1,
  130. cells(row, col) {
  131. const cellProperties = {};
  132. if (getSourceData()[row][col] === 'A') {
  133. cellProperties.readOnly = true;
  134. }
  135. return cellProperties;
  136. }
  137. });
  138. expect(spec().$container.find('tbody tr:eq(0) td:eq(0)').text()).toEqual('C');
  139. expect(spec().$container.find('tbody tr:eq(0) td:eq(0)').hasClass('htDimmed')).toBe(false);
  140. expect(spec().$container.find('tbody tr:eq(1) td:eq(0)').text()).toEqual('A');
  141. expect(spec().$container.find('tbody tr:eq(1) td:eq(0)').hasClass('htDimmed')).toBe(true);
  142. expect(spec().$container.find('tbody tr:eq(2) td:eq(0)').text()).toEqual('B');
  143. expect(spec().$container.find('tbody tr:eq(2) td:eq(0)').hasClass('htDimmed')).toBe(false);
  144. // Column sorting changes the order of displayed rows while keeping table data unchanged
  145. updateSettings({
  146. columnSorting: {
  147. initialConfig: {
  148. column: 0,
  149. sortOrder: 'asc'
  150. }
  151. }
  152. });
  153. expect(spec().$container.find('tbody tr:eq(0) td:eq(0)').text()).toEqual('A');
  154. expect(spec().$container.find('tbody tr:eq(0) td:eq(0)').hasClass('htDimmed')).toBe(true);
  155. expect(spec().$container.find('tbody tr:eq(1) td:eq(0)').text()).toEqual('B');
  156. expect(spec().$container.find('tbody tr:eq(1) td:eq(0)').hasClass('htDimmed')).toBe(false);
  157. expect(spec().$container.find('tbody tr:eq(2) td:eq(0)').text()).toEqual('C');
  158. expect(spec().$container.find('tbody tr:eq(2) td:eq(0)').hasClass('htDimmed')).toBe(false);
  159. });
  160. it('should call `beforeGetCellMeta` plugin hook with visual indexes as parameters', () => {
  161. let rowInsideHook;
  162. let colInsideHook;
  163. const hot = handsontable({
  164. beforeGetCellMeta(row, col) {
  165. rowInsideHook = row;
  166. colInsideHook = col;
  167. },
  168. modifyRow(row) {
  169. return row + 10;
  170. },
  171. modifyCol(col) {
  172. return col + 10;
  173. }
  174. });
  175. hot.getCellMeta(0, 1);
  176. expect(rowInsideHook).toEqual(0);
  177. expect(colInsideHook).toEqual(1);
  178. });
  179. it('should call `afterGetCellMeta` plugin hook with visual indexes as parameters', () => {
  180. let rowInsideHook;
  181. let colInsideHook;
  182. const hot = handsontable({
  183. afterGetCellMeta(row, col) {
  184. rowInsideHook = row;
  185. colInsideHook = col;
  186. },
  187. modifyRow(row) {
  188. return row + 10;
  189. },
  190. modifyCol(col) {
  191. return col + 10;
  192. }
  193. });
  194. hot.getCellMeta(0, 1);
  195. expect(rowInsideHook).toEqual(0);
  196. expect(colInsideHook).toEqual(1);
  197. });
  198. });