numericRenderer.spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. describe('NumericRenderer', () => {
  2. var 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 render formatted number', (done) => {
  13. var onAfterValidate = jasmine.createSpy('onAfterValidate');
  14. handsontable({
  15. cells() {
  16. return {
  17. type: 'numeric',
  18. format: '$0,0.00'
  19. };
  20. },
  21. afterValidate: onAfterValidate
  22. });
  23. setDataAtCell(2, 2, '1000.234');
  24. setTimeout(() => {
  25. expect(getCell(2, 2).innerHTML).toEqual('$1,000.23');
  26. done();
  27. }, 200);
  28. });
  29. it('should render signed number', (done) => {
  30. var onAfterValidate = jasmine.createSpy('onAfterValidate');
  31. handsontable({
  32. cells() {
  33. return {
  34. type: 'numeric',
  35. format: '$0,0.00'
  36. };
  37. },
  38. afterValidate: onAfterValidate
  39. });
  40. setDataAtCell(2, 2, '-1000.234');
  41. setTimeout(() => {
  42. expect(getCell(2, 2).innerHTML).toEqual('-$1,000.23');
  43. done();
  44. }, 200);
  45. });
  46. it('should try to render string as numeral', (done) => {
  47. handsontable({
  48. cells() {
  49. return {
  50. type: 'numeric',
  51. format: '$0,0.00'
  52. };
  53. },
  54. });
  55. setDataAtCell(2, 2, '123 simple test');
  56. setTimeout(() => {
  57. expect(getCell(2, 2).innerHTML).toEqual('$123.00');
  58. done();
  59. }, 100);
  60. });
  61. it('should add class names `htNumeric` and `htRight` to the cell if it renders a number', () => {
  62. var DIV = document.createElement('DIV');
  63. var instance = new Handsontable(DIV, {});
  64. var TD = document.createElement('TD');
  65. TD.className = 'someClass';
  66. Handsontable.renderers.NumericRenderer(instance, TD, 0, 0, 0, 123, {});
  67. expect(TD.className).toEqual('someClass htRight htNumeric');
  68. instance.destroy();
  69. });
  70. it('should add class names `htNumeric` and `htRight` to the cell if it renders a numeric string', () => {
  71. var DIV = document.createElement('DIV');
  72. var instance = new Handsontable(DIV, {});
  73. var TD = document.createElement('TD');
  74. TD.className = 'someClass';
  75. Handsontable.renderers.NumericRenderer(instance, TD, 0, 0, 0, '123', {});
  76. expect(TD.className).toEqual('someClass htRight htNumeric');
  77. instance.destroy();
  78. });
  79. it('should not add class name `htNumeric` to the cell if it renders a text', () => {
  80. var DIV = document.createElement('DIV');
  81. var instance = new Handsontable(DIV, {});
  82. var TD = document.createElement('TD');
  83. TD.className = 'someClass';
  84. Handsontable.renderers.NumericRenderer(instance, TD, 0, 0, 0, 'abc', {});
  85. expect(TD.className).toEqual('someClass');
  86. instance.destroy();
  87. });
  88. it('should add class name `htDimmed` to a read only cell', () => {
  89. var DIV = document.createElement('DIV');
  90. var instance = new Handsontable(DIV, {});
  91. var TD = document.createElement('TD');
  92. Handsontable.renderers.NumericRenderer(instance, TD, 0, 0, 0, 123, {readOnly: true, readOnlyCellClassName: 'htDimmed'});
  93. expect(TD.className).toContain('htDimmed');
  94. instance.destroy();
  95. });
  96. describe('NumericRenderer with ContextMenu', () => {
  97. it('should change class name from default `htRight` to `htLeft` after set align in contextMenu', (done) => {
  98. handsontable({
  99. startRows: 1,
  100. startCols: 1,
  101. contextMenu: ['alignment'],
  102. cells() {
  103. return {
  104. type: 'numeric',
  105. format: '$0,0.00'
  106. };
  107. },
  108. height: 100
  109. });
  110. setDataAtCell(0, 0, '1000');
  111. selectCell(0, 0);
  112. contextMenu();
  113. var menu = $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator');
  114. menu.simulate('mouseover');
  115. setTimeout(() => {
  116. var contextSubMenu = $(`.htContextMenuSub_${menu.text()}`).find('tbody td').eq(0);
  117. contextSubMenu.simulate('mousedown');
  118. contextSubMenu.simulate('mouseup');
  119. expect($('.handsontable.ht_master .htLeft:not(.htRight)').length).toBe(1);
  120. done();
  121. }, 500);
  122. });
  123. });
  124. });