Core_removeCellMeta.spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. describe('Core_removeCellMeta', () => {
  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 remove meta for cell', () => {
  13. handsontable({
  14. data: [
  15. [1, 2, 3, 4],
  16. [5, 6, 7, 8],
  17. [0, 9, 8, 7]
  18. ]
  19. });
  20. const border = {
  21. top: {
  22. },
  23. left: {
  24. }
  25. };
  26. setCellMeta(0, 0, 'borders', border);
  27. expect(getCellMeta(0, 0).borders).toEqual(border);
  28. removeCellMeta(0, 0, 'borders');
  29. expect(getCellMeta(0, 0).borders).toBeUndefined();
  30. });
  31. it('should remove proper cell meta when indexes was modified', () => {
  32. handsontable({
  33. modifyRow(row) {
  34. return row + 10;
  35. },
  36. modifyCol(col) {
  37. return col + 10;
  38. }
  39. });
  40. setCellMeta(0, 0, 'key', 'value');
  41. removeCellMeta(0, 0, 'key');
  42. expect(getCellMeta(0, 0).key).toBeUndefined();
  43. });
  44. it('should trigger `beforeRemoveCellMeta` hook with proper parameters', () => {
  45. const beforeRemoveCellMeta = jasmine.createSpy('beforeRemoveCellMeta');
  46. handsontable({
  47. data: [
  48. [1, 2, 3, 4],
  49. [5, 6, 7, 8],
  50. [0, 9, 8, 7]
  51. ],
  52. beforeRemoveCellMeta
  53. });
  54. setCellMeta(0, 0, 'key', 'value');
  55. removeCellMeta(0, 0, 'key');
  56. expect(beforeRemoveCellMeta).toHaveBeenCalledWith(0, 0, 'key', 'value', undefined, undefined);
  57. });
  58. it('should trigger `afterRemoveCellMeta` hook with proper parameters - case 1 (removed `key` existed)', () => {
  59. const afterRemoveCellMeta = jasmine.createSpy('afterRemoveCellMeta');
  60. handsontable({
  61. data: [
  62. [1, 2, 3, 4],
  63. [5, 6, 7, 8],
  64. [0, 9, 8, 7]
  65. ],
  66. afterRemoveCellMeta
  67. });
  68. setCellMeta(0, 0, 'key', 'value');
  69. removeCellMeta(0, 0, 'key');
  70. expect(afterRemoveCellMeta).toHaveBeenCalledWith(0, 0, 'key', 'value', undefined, undefined);
  71. });
  72. it('should trigger `afterRemoveCellMeta` hook with proper parameters - case 2 (removed `key` not existed)', () => {
  73. const afterRemoveCellMeta = jasmine.createSpy('afterRemoveCellMeta');
  74. handsontable({
  75. data: [
  76. [1, 2, 3, 4],
  77. [5, 6, 7, 8],
  78. [0, 9, 8, 7]
  79. ],
  80. afterRemoveCellMeta
  81. });
  82. removeCellMeta(0, 0, 'key');
  83. expect(afterRemoveCellMeta).toHaveBeenCalledWith(0, 0, 'key', undefined, undefined, undefined);
  84. });
  85. it('should call `beforeRemoveCellMeta` plugin hook with visual indexes as parameters', () => {
  86. let rowInsideHook;
  87. let colInsideHook;
  88. handsontable({
  89. beforeRemoveCellMeta(row, col) {
  90. rowInsideHook = row;
  91. colInsideHook = col;
  92. },
  93. modifyRow(row) {
  94. return row + 10;
  95. },
  96. modifyCol(col) {
  97. return col + 10;
  98. }
  99. });
  100. removeCellMeta(0, 1, 'key');
  101. expect(rowInsideHook).toEqual(0);
  102. expect(colInsideHook).toEqual(1);
  103. });
  104. it('should call `afterRemoveCellMeta` plugin hook with visual indexes as parameters', () => {
  105. let rowInsideHook;
  106. let colInsideHook;
  107. handsontable({
  108. afterRemoveCellMeta(row, col) {
  109. rowInsideHook = row;
  110. colInsideHook = col;
  111. },
  112. modifyRow(row) {
  113. return row + 10;
  114. },
  115. modifyCol(col) {
  116. return col + 10;
  117. }
  118. });
  119. removeCellMeta(0, 1, 'key');
  120. expect(rowInsideHook).toEqual(0);
  121. expect(colInsideHook).toEqual(1);
  122. });
  123. it('should block removing cell meta when hook `beforeRemoveCellMeta` return false', () => {
  124. handsontable({
  125. beforeRemoveCellMeta(row, col) {
  126. if (row === 0 && col === 0) {
  127. return false;
  128. }
  129. return true;
  130. }
  131. });
  132. setCellMeta(0, 0, 'key', 'value');
  133. setCellMeta(0, 1, 'key', 'value');
  134. removeCellMeta(0, 0, 'key');
  135. removeCellMeta(0, 1, 'key');
  136. // `value` shouldn't be removed
  137. expect(getCellMeta(0, 0).key).toEqual('value');
  138. expect(getCellMeta(0, 1).key).toBeUndefined();
  139. });
  140. });