setCellMeta.spec.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. describe('Core.setCellMeta', () => {
  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 set correct meta className for cell', () => {
  13. const className = 'htCenter htMiddle';
  14. handsontable({
  15. afterCellMetaReset() {
  16. this.setCellMeta(0, 0, 'className', className);
  17. }
  18. });
  19. const cellMeta = getCellMeta(0, 0);
  20. expect(cellMeta.className).not.toBeUndefined();
  21. expect(cellMeta.className).toEqual(className);
  22. });
  23. it('should set proper cell meta when indexes was modified', () => {
  24. handsontable({
  25. modifyRow(row) {
  26. return row + 10;
  27. },
  28. modifyCol(col) {
  29. return col + 10;
  30. }
  31. });
  32. setCellMeta(0, 1, 'key', 'value');
  33. expect(getCellMeta(0, 1).key).toEqual('value');
  34. });
  35. it('should set correct meta className for non existed cell', () => {
  36. const className = 'htCenter htMiddle';
  37. handsontable({
  38. data: Handsontable.helper.createSpreadsheetData(5, 5),
  39. afterCellMetaReset() {
  40. this.setCellMeta(100, 100, 'className', className);
  41. }
  42. });
  43. const cellMeta = getCellMeta(100, 100);
  44. expect(cellMeta.className).not.toBeUndefined();
  45. expect(cellMeta.className).toEqual(className);
  46. });
  47. it('should set correct meta classNames for cells using cell in configuration', () => {
  48. const classNames = [
  49. 'htCenter htTop',
  50. 'htRight htBottom'
  51. ];
  52. handsontable({
  53. cell: [
  54. { row: 0, col: 0, className: classNames[0] },
  55. { row: 1, col: 1, className: classNames[1] }
  56. ]
  57. });
  58. expect(spec().$container.find('tbody tr:eq(0) td:eq(0)')[0].className).toEqual(classNames[0]);
  59. expect(spec().$container.find('tbody tr:eq(1) td:eq(1)')[0].className).toEqual(classNames[1]);
  60. });
  61. it('should change cell meta data with updateSettings when the cell option is defined', () => {
  62. const classNames = [
  63. 'htCenter htTop',
  64. 'htRight htBottom'
  65. ];
  66. handsontable({
  67. cell: [
  68. { row: 0, col: 0, className: classNames[0] },
  69. { row: 1, col: 1, className: classNames[1] }
  70. ]
  71. });
  72. expect(spec().$container.find('tbody tr:eq(0) td:eq(0)')[0].className).toEqual(classNames[0]);
  73. expect(spec().$container.find('tbody tr:eq(1) td:eq(1)')[0].className).toEqual(classNames[1]);
  74. updateSettings({
  75. cell: []
  76. });
  77. expect(spec().$container.find('tbody tr:eq(0) td:eq(0)')[0].className).toEqual('');
  78. expect(spec().$container.find('tbody tr:eq(1) td:eq(1)')[0].className).toEqual('');
  79. updateSettings({
  80. cell: [
  81. { row: 0, col: 0, className: classNames[1] },
  82. { row: 1, col: 1, className: classNames[0] }
  83. ]
  84. });
  85. expect(spec().$container.find('tbody tr:eq(0) td:eq(0)')[0].className).toEqual(classNames[1]);
  86. expect(spec().$container.find('tbody tr:eq(1) td:eq(1)')[0].className).toEqual(classNames[0]);
  87. });
  88. it('should call `afterSetCellMeta` plugin hook with visual indexes as parameters', () => {
  89. const className = 'htCenter htMiddle';
  90. const afterSetCellMeta = jasmine.createSpy('afterSetCellMeta');
  91. const hot = handsontable({
  92. afterSetCellMeta,
  93. modifyRow(row) {
  94. return row + 10;
  95. },
  96. modifyCol(col) {
  97. return col + 10;
  98. }
  99. });
  100. hot.setCellMeta(0, 1, 'className', className);
  101. expect(afterSetCellMeta).toHaveBeenCalledWith(0, 1, 'className', className, undefined, undefined);
  102. });
  103. });