3b0712ec3104e35b74c567175409c0fcde910a2471be6cf28220c400c0fa11a6216fc936aef9c3255d224f1c89b342d7db9674c6d60f696568c180476464b8 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. describe('Core_datachange', () => {
  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 call onChange callback', () => {
  13. var output = null;
  14. handsontable({
  15. afterChange(changes) {
  16. output = changes;
  17. }
  18. });
  19. setDataAtCell(1, 2, 'test');
  20. expect(output[0][0]).toEqual(1);
  21. expect(output[0][1]).toEqual(2);
  22. expect(output[0][2]).toEqual(null);
  23. expect(output[0][3]).toEqual('test');
  24. });
  25. it('should use custom source for datachange', () => {
  26. var output = null,
  27. src = null;
  28. handsontable({
  29. afterChange(changes, source) {
  30. output = changes;
  31. src = source;
  32. }
  33. });
  34. setDataAtCell(1, 2, 'abc', 'test');
  35. expect(output[0][3]).toEqual('abc');
  36. expect(src).toEqual('test');
  37. });
  38. it('should use custom source for datachange with array', () => {
  39. var output = null,
  40. src = null;
  41. handsontable({
  42. afterChange(changes, source) {
  43. output = changes;
  44. src = source;
  45. }
  46. });
  47. setDataAtCell([[1, 2, 'abc']], 'test');
  48. expect(output[0][3]).toEqual('abc');
  49. expect(src).toEqual('test');
  50. });
  51. it('should trigger datachange event', () => {
  52. var output = null;
  53. handsontable();
  54. Handsontable.hooks.add('afterChange', (changes) => {
  55. output = changes;
  56. });
  57. setDataAtCell(1, 2, 'test');
  58. expect(output[0][0]).toEqual(1);
  59. expect(output[0][1]).toEqual(2);
  60. expect(output[0][2]).toEqual(null);
  61. expect(output[0][3]).toEqual('test');
  62. });
  63. it('this.rootElement should point to handsontable rootElement', function() {
  64. var output = null;
  65. var $container = this.$container;
  66. handsontable({
  67. afterChange() {
  68. output = this.rootElement;
  69. }
  70. });
  71. setDataAtCell(0, 0, 'test');
  72. expect(output).toEqual($container[0]);
  73. });
  74. it('onChange should be triggered after data is rendered to DOM (init)', function() {
  75. var output = null;
  76. var $container = this.$container;
  77. handsontable({
  78. data: [
  79. ['Joe Red']
  80. ],
  81. afterChange(changes, source) {
  82. if (source === 'loadData') {
  83. output = $container.find('table.htCore tbody td:first').html();
  84. }
  85. }
  86. });
  87. expect(output).toEqual('Joe Red');
  88. });
  89. it('onChange should be triggered after data is rendered to DOM (setDataAtCell)', function() {
  90. var output = null;
  91. var $container = this.$container;
  92. handsontable({
  93. data: [
  94. ['Joe Red']
  95. ],
  96. afterChange(changes, source) {
  97. if (source === 'edit') {
  98. output = $container.find('table.htCore tbody td:first').html();
  99. }
  100. }
  101. });
  102. setDataAtCell(0, 0, 'Alice Red');
  103. expect(output).toEqual('Alice Red');
  104. });
  105. it('onChange event object should contain documented keys and values when triggered by edit', () => {
  106. var sampleData = [
  107. {
  108. col1: 'a',
  109. col2: 'b',
  110. col3: 'c'
  111. }
  112. ];
  113. var event = null;
  114. handsontable({
  115. data: sampleData,
  116. afterChange(changes, source) {
  117. if (source === 'edit') {
  118. event = changes.shift();
  119. }
  120. }
  121. });
  122. setDataAtCell(0, 0, 'test');
  123. expect(event[0]).toEqual(0);
  124. expect(event[1]).toEqual('col1');
  125. expect(event[2]).toEqual('a');
  126. expect(event[3]).toEqual('test');
  127. });
  128. it('source parameter should be `edit` when cell value is changed through editor', () => {
  129. var sources = [];
  130. handsontable({
  131. data: [
  132. ['Joe Red']
  133. ],
  134. afterChange(changes, source) {
  135. sources.push(source);
  136. }
  137. });
  138. selectCell(0, 0);
  139. keyDown('enter');
  140. document.activeElement.value = 'Ted';
  141. keyDown('enter');
  142. expect(sources).toEqual(['loadData', 'edit']); // loadData is always the first source
  143. });
  144. });