93b1c2207d0776d17a96ac5b312b618f1a424e9960de957a8cece26fd40d622f3bc758c4081f85012aa0e651f3b81717f2d8ec95efeaf2788c39b4761a8fd2 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. describe('Core_populateFromArray', () => {
  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. var arrayOfArrays = function() {
  13. return [
  14. ['', 'Kia', 'Nissan', 'Toyota', 'Honda', 'Mix'],
  15. ['2008', 10, 11, 12, 13, {a: 1, b: 2}],
  16. ['2009', 20, 11, 14, 13, {a: 1, b: 2}],
  17. ['2010', 30, 15, 12, 13, {a: 1, b: 2}]
  18. ];
  19. };
  20. it('should call onChange callback', () => {
  21. var output = null;
  22. handsontable({
  23. data: arrayOfArrays(),
  24. afterChange(changes) {
  25. output = changes;
  26. }
  27. });
  28. populateFromArray(0, 0, [['test', 'test'], ['test', 'test']], 1, 1);
  29. expect(output).toEqual([[0, 0, '', 'test'], [0, 1, 'Kia', 'test'], [1, 0, '2008', 'test'], [1, 1, 10, 'test']]);
  30. });
  31. it('should populate single value for whole selection', () => {
  32. var output = null;
  33. handsontable({
  34. data: arrayOfArrays(),
  35. afterChange(changes) {
  36. output = changes;
  37. }
  38. });
  39. populateFromArray(0, 0, [['test']], 3, 0);
  40. expect(output).toEqual([[0, 0, '', 'test'], [1, 0, '2008', 'test'], [2, 0, '2009', 'test'], [3, 0, '2010', 'test']]);
  41. });
  42. it('should populate value for whole selection only if populated data isn\'t an array', () => {
  43. var output = null;
  44. handsontable({
  45. data: arrayOfArrays(),
  46. afterChange(changes) {
  47. output = changes;
  48. }
  49. });
  50. populateFromArray(0, 0, [['test'], [[1, 2, 3]]], 3, 0);
  51. expect(output).toEqual([[0, 0, '', 'test'], [2, 0, '2009', 'test']]);
  52. });
  53. it('should populate value for whole selection only if populated data isn\'t an object', () => {
  54. var output = null;
  55. handsontable({
  56. data: arrayOfArrays(),
  57. afterChange(changes) {
  58. output = changes;
  59. }
  60. });
  61. populateFromArray(0, 0, [['test'], [{test: 1}]], 3, 0);
  62. expect(output).toEqual([[0, 0, '', 'test'], [2, 0, '2009', 'test']]);
  63. });
  64. it('shouldn\'t populate value if original value doesn\'t have the same data structure', () => {
  65. var output = null;
  66. handsontable({
  67. data: arrayOfArrays(),
  68. afterChange(changes) {
  69. output = changes;
  70. }
  71. });
  72. populateFromArray(1, 3, [['test']], 1, 5);
  73. expect(output).toEqual([[1, 3, 12, 'test'], [1, 4, 13, 'test']]);
  74. });
  75. it('should shift values down', () => {
  76. var output = null;
  77. handsontable({
  78. data: arrayOfArrays(),
  79. afterChange(changes) {
  80. output = changes;
  81. },
  82. minSpareRows: 1
  83. });
  84. populateFromArray(0, 0, [['test', 'test2'], ['test3', 'test4']], 2, 2, null, 'shift_down');
  85. expect(getData()).toEqual([
  86. ['test', 'test2', 'test', 'Toyota', 'Honda', 'Mix'],
  87. ['test3', 'test4', 'test3', 12, 13, { a: 1, b: 2 }],
  88. ['test', 'test2', 'test', 14, 13, { a: 1, b: 2 }],
  89. ['', 'Kia', 'Nissan', 12, 13, { a: 1, b: 2 }],
  90. ['2008', 10, 11, null, null, null],
  91. ['2009', 20, 11, null, null, null],
  92. ['2010', 30, 15, null, null, null],
  93. [null, null, null, null, null, null]
  94. ]);
  95. });
  96. it('should shift values right', () => {
  97. var output = null;
  98. handsontable({
  99. data: arrayOfArrays(),
  100. afterChange(changes) {
  101. output = changes;
  102. },
  103. minSpareCols: 1
  104. });
  105. populateFromArray(0, 0, [['test', 'test2'], ['test3', 'test4']], 2, 2, null, 'shift_right');
  106. expect(getData()).toEqual([
  107. ['test', 'test2', 'test', '', 'Kia', 'Nissan', 'Toyota', 'Honda', 'Mix', null],
  108. ['test3', 'test4', 'test3', '2008', 10, {a: 1, b: 2}, 12, 13, null, null],
  109. ['test', 'test2', 'test', '2009', 20, {a: 1, b: 2}, 14, 13, null, null],
  110. ['2010', 30, 15, 12, 13, {a: 1, b: 2}, null, null, null, null]
  111. ]);
  112. });
  113. it('should run beforeAutofillInsidePopulate hook for each inserted value', () => {
  114. var called = 0;
  115. var hot = handsontable({
  116. data: arrayOfArrays()
  117. });
  118. hot.addHook('beforeAutofillInsidePopulate', (index) => {
  119. called++;
  120. });
  121. populateFromArray(0, 0, [['test', 'test2'], ['test3', 'test4']], 1, 1, 'Autofill.fill', 'overwrite');
  122. expect(called).toEqual(4);
  123. });
  124. it('should run beforeAutofillInsidePopulate hook and could change cell data before insert if returned object with value property', () => {
  125. var hot = handsontable({
  126. data: arrayOfArrays()
  127. });
  128. hot.addHook('beforeAutofillInsidePopulate', (index) => ({
  129. value: 'my_test'
  130. }));
  131. populateFromArray(0, 0, [['test', 'test2'], ['test3', 'test4']], 1, 1, 'Autofill.fill', 'overwrite');
  132. expect(getDataAtCell(0, 0)).toEqual('my_test');
  133. });
  134. it('should populate 1 row from 2 selected rows', () => {
  135. var hot = handsontable({
  136. data: arrayOfArrays()
  137. });
  138. populateFromArray(2, 0, [['A1'], ['A2']], 2, 0, 'autofill', null, 'down', [[0]]);
  139. expect(getDataAtCell(2, 0)).toEqual('A1');
  140. expect(getDataAtCell(3, 0)).toEqual('2010');
  141. });
  142. it('should populate 1 column from 2 selected columns`', () => {
  143. var hot = handsontable({
  144. data: arrayOfArrays()
  145. });
  146. populateFromArray(0, 2, [['A1', 'A2']], 0, 2, 'autofill', null, 'right', [[0]]);
  147. expect(getDataAtCell(0, 2)).toEqual('A1');
  148. expect(getDataAtCell(0, 3)).toEqual('Toyota');
  149. });
  150. });