Core_populateFromArray.spec.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. describe('Core_populateFromArray', () => {
  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. const 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. let 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. let 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. let 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. let 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. let 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. handsontable({
  77. data: arrayOfArrays(),
  78. minSpareRows: 1
  79. });
  80. populateFromArray(0, 0, [['test', 'test2'], ['test3', 'test4']], 2, 2, null, 'shift_down');
  81. expect(getData()).toEqual([
  82. ['test', 'test2', 'test', 'Toyota', 'Honda', 'Mix'],
  83. ['test3', 'test4', 'test3', 12, 13, { a: 1, b: 2 }],
  84. ['test', 'test2', 'test', 14, 13, { a: 1, b: 2 }],
  85. ['', 'Kia', 'Nissan', 12, 13, { a: 1, b: 2 }],
  86. ['2008', 10, 11, null, null, null],
  87. ['2009', 20, 11, null, null, null],
  88. ['2010', 30, 15, null, null, null],
  89. [null, null, null, null, null, null]
  90. ]);
  91. });
  92. it('should shift values right', () => {
  93. handsontable({
  94. data: arrayOfArrays(),
  95. minSpareCols: 1
  96. });
  97. populateFromArray(0, 0, [['test', 'test2'], ['test3', 'test4']], 2, 2, null, 'shift_right');
  98. expect(getData()).toEqual([
  99. ['test', 'test2', 'test', '', 'Kia', 'Nissan', 'Toyota', 'Honda', 'Mix', null],
  100. ['test3', 'test4', 'test3', '2008', 10, { a: 1, b: 2 }, 12, 13, null, null],
  101. ['test', 'test2', 'test', '2009', 20, { a: 1, b: 2 }, 14, 13, null, null],
  102. ['2010', 30, 15, 12, 13, { a: 1, b: 2 }, null, null, null, null]
  103. ]);
  104. });
  105. it('should run beforeAutofillInsidePopulate hook for each inserted value', () => {
  106. const hot = handsontable({
  107. data: arrayOfArrays()
  108. });
  109. let called = 0;
  110. hot.addHook('beforeAutofillInsidePopulate', () => {
  111. called += 1;
  112. });
  113. populateFromArray(0, 0, [['test', 'test2'], ['test3', 'test4']], 1, 1, 'Autofill.fill', 'overwrite');
  114. expect(called).toEqual(4);
  115. });
  116. it('should run beforeAutofillInsidePopulate hook and could change cell data before insert if returned object with value property', () => {
  117. const hot = handsontable({
  118. data: arrayOfArrays()
  119. });
  120. hot.addHook('beforeAutofillInsidePopulate', () => ({
  121. value: 'my_test'
  122. }));
  123. populateFromArray(0, 0, [['test', 'test2'], ['test3', 'test4']], 1, 1, 'Autofill.fill', 'overwrite');
  124. expect(getDataAtCell(0, 0)).toEqual('my_test');
  125. });
  126. it('should populate 1 row from 2 selected rows', () => {
  127. handsontable({
  128. data: arrayOfArrays()
  129. });
  130. populateFromArray(2, 0, [['A1'], ['A2']], 2, 0, 'autofill', null, 'down', [[0]]);
  131. expect(getDataAtCell(2, 0)).toEqual('A1');
  132. expect(getDataAtCell(3, 0)).toEqual('2010');
  133. });
  134. it('should populate 1 column from 2 selected columns`', () => {
  135. handsontable({
  136. data: arrayOfArrays()
  137. });
  138. populateFromArray(0, 2, [['A1', 'A2']], 0, 2, 'autofill', null, 'right', [[0]]);
  139. expect(getDataAtCell(0, 2)).toEqual('A1');
  140. expect(getDataAtCell(0, 3)).toEqual('Toyota');
  141. });
  142. });