Core_alter.spec.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. describe('Core_alter', () => {
  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 arrayOfNestedObjects = function() {
  13. return [
  14. { id: 1,
  15. name: {
  16. first: 'Ted',
  17. last: 'Right'
  18. },
  19. address: 'Street Name',
  20. zip: '80410',
  21. city: 'City Name' },
  22. { id: 2,
  23. name: {
  24. first: 'Frank',
  25. last: 'Honest'
  26. },
  27. address: 'Street Name',
  28. zip: '80410',
  29. city: 'City Name' },
  30. { id: 3,
  31. name: {
  32. first: 'Joan',
  33. last: 'Well'
  34. },
  35. address: 'Street Name',
  36. zip: '80410',
  37. city: 'City Name' }
  38. ];
  39. };
  40. const arrayOfArrays = function() {
  41. return [
  42. ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
  43. ['2008', 10, 11, 12, 13],
  44. ['2009', 20, 11, 14, 13],
  45. ['2010', 30, 15, 12, 13]
  46. ];
  47. };
  48. describe('remove row', () => {
  49. describe('multiple items at once', () => {
  50. it('should remove rows when index groups are passed in ascending order', () => {
  51. handsontable({
  52. data: Handsontable.helper.createSpreadsheetData(15, 5),
  53. });
  54. // [[rowVisualIndex, amountRowsToRemove] ...]
  55. alter('remove_row', [[1, 3], [5, 1], [7, 3], [11, 2]]);
  56. // It remove rows as follow:
  57. // 1--------3 5-1 7---------3 11-----2
  58. // A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
  59. //
  60. // Result: A1, A5, A7, A11, A14, A15
  61. expect(getDataAtCol(0)).toEqual(['A1', 'A5', 'A7', 'A11', 'A14', 'A15']);
  62. expect(getData().length).toBe(6);
  63. });
  64. it('should remove rows when index groups are passed in descending order', () => {
  65. handsontable({
  66. data: Handsontable.helper.createSpreadsheetData(15, 5),
  67. });
  68. // [[rowVisualIndex, amountRowsToRemove] ...]
  69. alter('remove_row', [[11, 2], [7, 3], [5, 1], [1, 3]]);
  70. // It remove rows as follow:
  71. // 1--------3 5-1 7---------3 11-----2
  72. // A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
  73. //
  74. // Result: A1, A5, A7, A11, A14, A15
  75. expect(getDataAtCol(0)).toEqual(['A1', 'A5', 'A7', 'A11', 'A14', 'A15']);
  76. expect(getData().length).toBe(6);
  77. });
  78. it('should remove rows when index groups are passed as intersecting values', () => {
  79. handsontable({
  80. data: Handsontable.helper.createSpreadsheetData(15, 5),
  81. });
  82. // [[rowVisualIndex, amountRowsToRemove] ...]
  83. alter('remove_row', [[1, 3], [4, 2], [5, 5], [11, 1]]);
  84. // It remove rows as follow:
  85. // 1---------------------------------9 11-1
  86. // A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
  87. //
  88. // Result: A1, A11, A13, A14, A15
  89. expect(getDataAtCol(0)).toEqual(['A1', 'A11', 'A13', 'A14', 'A15']);
  90. expect(getData().length).toBe(5);
  91. });
  92. it('should remove rows when index groups are passed as intersecting values (the second scenario)', () => {
  93. handsontable({
  94. data: Handsontable.helper.createSpreadsheetData(15, 5),
  95. });
  96. // [[rowVisualIndex, amountRowsToRemove] ...]
  97. alter('remove_row', [[1, 3], [2, 1], [5, 2]]);
  98. // It remove columns as follow:
  99. // 1--------3 5----2
  100. // A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
  101. //
  102. // Result: A1, A5, A8, A9, A10, A11, A12, A13, A14, A15
  103. expect(getDataAtCol(0)).toEqual(['A1', 'A5', 'A8', 'A9', 'A10', 'A11', 'A12', 'A13', 'A14', 'A15']);
  104. expect(getData().length).toBe(10);
  105. });
  106. it('should remove rows when index groups are passed as intersecting values (placed randomly)', () => {
  107. handsontable({
  108. data: Handsontable.helper.createSpreadsheetData(15, 5),
  109. });
  110. // [[rowVisualIndex, amountRowsToRemove] ...]
  111. alter('remove_row', [[4, 2], [11, 1], [5, 5], [1, 3]]);
  112. // It remove rows as follow:
  113. // 1---------------------------------9 11-1
  114. // A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
  115. //
  116. // Result: A1, A11, A13, A14, A15
  117. expect(getDataAtCol(0)).toEqual(['A1', 'A11', 'A13', 'A14', 'A15']);
  118. expect(getData().length).toBe(5);
  119. });
  120. });
  121. it('should remove row', () => {
  122. handsontable({
  123. minRows: 5,
  124. data: arrayOfNestedObjects(),
  125. columns: [
  126. { data: 'id' },
  127. { data: 'name.first' }
  128. ]
  129. });
  130. alter('remove_row', 1);
  131. expect(getDataAtCell(1, 1)).toEqual('Joan'); // Joan should be moved up
  132. expect(getData().length).toEqual(5); // new row should be added by keepEmptyRows
  133. });
  134. it('should not remove row if amount is zero', () => {
  135. handsontable({
  136. data: arrayOfNestedObjects(),
  137. columns: [
  138. { data: 'id' },
  139. { data: 'name.first' }
  140. ],
  141. });
  142. const countedRows = countRows();
  143. alter('remove_row', 1, 0);
  144. expect(countRows()).toBe(countedRows);
  145. });
  146. it('should fire beforeRemoveRow event before removing row', () => {
  147. const onBeforeRemoveRow = jasmine.createSpy('onBeforeRemoveRow');
  148. handsontable({
  149. data: arrayOfNestedObjects(),
  150. columns: [
  151. { data: 'id' },
  152. { data: 'name.first' }
  153. ],
  154. beforeRemoveRow: onBeforeRemoveRow,
  155. });
  156. alter('remove_row', 2, 1, 'customSource');
  157. expect(onBeforeRemoveRow).toHaveBeenCalledWith(countRows(), 1, [2], 'customSource', undefined, undefined);
  158. });
  159. it('should not remove row if removing has been canceled by beforeRemoveRow event handler', () => {
  160. const onBeforeRemoveRow = jasmine.createSpy('onBeforeRemoveRow');
  161. onBeforeRemoveRow.and.callFake(() => false);
  162. handsontable({
  163. data: arrayOfNestedObjects(),
  164. columns: [
  165. { data: 'id' },
  166. { data: 'name.first' }
  167. ],
  168. beforeRemoveRow: onBeforeRemoveRow
  169. });
  170. expect(countRows()).toEqual(3);
  171. alter('remove_row');
  172. expect(countRows()).toEqual(3);
  173. });
  174. it('should not remove rows below minRows', () => {
  175. handsontable({
  176. startRows: 5,
  177. minRows: 4
  178. });
  179. alter('remove_row', 1);
  180. alter('remove_row', 1);
  181. alter('remove_row', 1);
  182. expect(countRows()).toEqual(4);
  183. });
  184. it('should not remove cols below minCols', () => {
  185. handsontable({
  186. startCols: 5,
  187. minCols: 4
  188. });
  189. alter('remove_col', 1);
  190. alter('remove_col', 1);
  191. alter('remove_col', 1);
  192. expect(countCols()).toEqual(4);
  193. });
  194. it('should remove one row if amount parameter is empty', () => {
  195. handsontable({
  196. data: [
  197. ['a1', 'a2', 'a3'],
  198. ['b1', 'b2', 'b3'],
  199. ['c1', 'c2', 'c3'],
  200. ['d1', 'd2', 'd3'],
  201. ['e1', 'e2', 'e3']
  202. ]
  203. });
  204. alter('remove_row', 1);
  205. expect(countRows()).toEqual(4);
  206. expect(spec().$container.find('tr:eq(0) td:eq(0)').html()).toEqual('a1');
  207. expect(spec().$container.find('tr:eq(1) td:eq(1)').html()).toEqual('c2');
  208. });
  209. it('should remove as many rows as given in the amount parameter', () => {
  210. handsontable({
  211. data: [
  212. ['a1', 'a2', 'a3'],
  213. ['b1', 'b2', 'b3'],
  214. ['c1', 'c2', 'c3'],
  215. ['d1', 'd2', 'd3'],
  216. ['e1', 'e2', 'e3']
  217. ]
  218. });
  219. alter('remove_row', 1, 3);
  220. expect(countRows()).toEqual(2);
  221. expect(spec().$container.find('tr:eq(0) td:eq(0)').html()).toEqual('a1');
  222. expect(spec().$container.find('tr:eq(1) td:eq(1)').html()).toEqual('e2');
  223. });
  224. it('should not remove more rows that exist', () => {
  225. handsontable({
  226. data: [
  227. ['a1', 'a2', 'a3'],
  228. ['b1', 'b2', 'b3'],
  229. ['c1', 'c2', 'c3'],
  230. ['d1', 'd2', 'd3'],
  231. ['e1', 'e2', 'e3']
  232. ]
  233. });
  234. alter('remove_row', 1, 10);
  235. expect(countRows()).toEqual(1);
  236. expect(getHtCore().find('tr:last td:last').html()).toEqual('a3');
  237. });
  238. it('should remove one row from end if no parameters are given', () => {
  239. handsontable({
  240. data: [
  241. ['a1', 'a2', 'a3'],
  242. ['b1', 'b2', 'b3'],
  243. ['c1', 'c2', 'c3'],
  244. ['d1', 'd2', 'd3'],
  245. ['e1', 'e2', 'e3']
  246. ]
  247. });
  248. alter('remove_row');
  249. expect(countRows()).toEqual(4);
  250. expect(getHtCore().find('tr:last td:eq(0)').html()).toEqual('d1');
  251. });
  252. it('should remove amount of rows from end if index parameter is not given', () => {
  253. handsontable({
  254. data: [
  255. ['a1', 'a2', 'a3'],
  256. ['b1', 'b2', 'b3'],
  257. ['c1', 'c2', 'c3'],
  258. ['d1', 'd2', 'd3'],
  259. ['e1', 'e2', 'e3']
  260. ]
  261. });
  262. alter('remove_row', null, 3);
  263. expect(countRows()).toEqual(2);
  264. expect(getHtCore().find('tr:last td:eq(0)').html()).toEqual('b1');
  265. });
  266. it('should remove rows from table with fixedRows', () => {
  267. handsontable({
  268. data: [
  269. ['a1', 'a2', 'a3'],
  270. ['b1', 'b2', 'b3']
  271. ],
  272. fixedRowsTop: 1,
  273. minSpareRows: 0
  274. });
  275. alter('remove_row', 1);
  276. expect(countRows()).toEqual(1);
  277. });
  278. it('should remove all rows from table with fixedRows', () => {
  279. handsontable({
  280. data: [
  281. ['a1', 'a2', 'a3'],
  282. ['b1', 'b2', 'b3']
  283. ],
  284. fixedRowsTop: 1,
  285. minSpareRows: 0
  286. });
  287. alter('remove_row', 1);
  288. alter('remove_row', 1);
  289. expect(countRows()).toEqual(0);
  290. });
  291. it('should remove row\'s cellProperties', () => {
  292. handsontable({
  293. startCols: 1,
  294. startRows: 3
  295. });
  296. getCellMeta(0, 0).someValue = [0, 0];
  297. getCellMeta(1, 0).someValue = [1, 0];
  298. getCellMeta(2, 0).someValue = [2, 0];
  299. alter('remove_row', 0);
  300. expect(getCellMeta(0, 0).someValue).toEqual([1, 0]);
  301. expect(getCellMeta(1, 0).someValue).toEqual([2, 0]);
  302. });
  303. it('should fire callback on remove row', () => {
  304. let outputBefore;
  305. let outputAfter;
  306. handsontable({
  307. minRows: 5,
  308. data: arrayOfNestedObjects(),
  309. columns: [
  310. { data: 'id' },
  311. { data: 'name.first' }
  312. ],
  313. beforeRemoveRow(index, amount, removedRows, source) {
  314. outputBefore = [index, amount, removedRows, source];
  315. },
  316. afterRemoveRow(index, amount, removedRows, source) {
  317. outputAfter = [index, amount, removedRows, source];
  318. }
  319. });
  320. alter('remove_row', 1, 2, 'customSource');
  321. expect(outputBefore).toEqual([1, 2, [1, 2], 'customSource']);
  322. expect(outputAfter).toEqual([1, 2, [1, 2], 'customSource']);
  323. });
  324. it('should decrement the number of fixed rows, if a fix row is removed', () => {
  325. const hot = handsontable({
  326. startCols: 1,
  327. startRows: 3,
  328. fixedRowsTop: 4
  329. });
  330. alter('remove_row', 1, 1);
  331. expect(hot.getSettings().fixedRowsTop).toEqual(3);
  332. alter('remove_row', 1, 2);
  333. expect(hot.getSettings().fixedRowsTop).toEqual(1);
  334. });
  335. it('should shift the cell meta according to the new row layout', () => {
  336. handsontable({
  337. startCols: 3,
  338. startRows: 4
  339. });
  340. setCellMeta(2, 1, 'className', 'test');
  341. alter('remove_row', 1, 1);
  342. expect(getCellMeta(1, 1).className).toEqual('test');
  343. });
  344. it('should shift the cell meta according to the new rows (>1) layout', () => {
  345. handsontable({
  346. startCols: 3,
  347. startRows: 4
  348. });
  349. setCellMeta(2, 1, 'className', 'test');
  350. alter('remove_row', 0, 2);
  351. expect(getCellMeta(0, 1).className).toEqual('test');
  352. });
  353. });
  354. describe('remove column', () => {
  355. describe('multiple items at once', () => {
  356. it('should remove columns when index groups are passed in ascending order', () => {
  357. handsontable({
  358. data: Handsontable.helper.createSpreadsheetData(5, 15),
  359. });
  360. // [[columnVisualIndex, amountColumnsToRemove] ...]
  361. alter('remove_col', [[1, 3], [5, 1], [7, 3], [11, 2]]);
  362. // It remove columns as follow:
  363. // 1--------3 5-1 7--------3 11---2
  364. // A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1
  365. //
  366. // Result: A1, E1, G1, K1, N1, O1
  367. expect(getDataAtRow(0)).toEqual(['A1', 'E1', 'G1', 'K1', 'N1', 'O1']);
  368. expect(getData()[0].length).toBe(6);
  369. });
  370. it('should remove columns when index groups are passed in descending order', () => {
  371. handsontable({
  372. data: Handsontable.helper.createSpreadsheetData(5, 15),
  373. });
  374. // [[columnVisualIndex, amountColumnsToRemove] ...]
  375. alter('remove_col', [[11, 2], [7, 3], [5, 1], [1, 3]]);
  376. // It remove columns as follow:
  377. // 1--------3 5-1 7--------3 11---2
  378. // A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1
  379. //
  380. // Result: A1, E1, G1, K1, N1, O1
  381. expect(getDataAtRow(0)).toEqual(['A1', 'E1', 'G1', 'K1', 'N1', 'O1']);
  382. expect(getData()[0].length).toBe(6);
  383. });
  384. it('should remove columns when index groups are passed as intersecting values', () => {
  385. handsontable({
  386. data: Handsontable.helper.createSpreadsheetData(5, 15),
  387. });
  388. // [[columnVisualIndex, amountColumnsToRemove] ...]
  389. alter('remove_col', [[1, 3], [4, 2], [5, 5], [11, 1]]);
  390. // It remove columns as follow:
  391. // 1--------------------------------9 11-1
  392. // A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1
  393. //
  394. // Result: A1, K1, M1, N1, O1
  395. expect(getDataAtRow(0)).toEqual(['A1', 'K1', 'M1', 'N1', 'O1']);
  396. expect(getData()[0].length).toBe(5);
  397. });
  398. it('should remove columns when index groups are passed as intersecting values (the second scenario)', () => {
  399. handsontable({
  400. data: Handsontable.helper.createSpreadsheetData(5, 15),
  401. });
  402. // [[columnVisualIndex, amountColumnsToRemove] ...]
  403. alter('remove_col', [[1, 3], [2, 1], [5, 2]]);
  404. // It remove columns as follow:
  405. // 1--------3 5----2
  406. // A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1
  407. //
  408. // Result: A1, E1, H1
  409. expect(getDataAtRow(0)).toEqual(['A1', 'E1', 'H1', 'I1', 'J1', 'K1', 'L1', 'M1', 'N1', 'O1']);
  410. expect(getData()[0].length).toBe(10);
  411. });
  412. it('should remove columns when index groups are passed as intersecting values (placed randomly)', () => {
  413. handsontable({
  414. data: Handsontable.helper.createSpreadsheetData(5, 15),
  415. });
  416. // [[columnVisualIndex, amountColumnsToRemove] ...]
  417. alter('remove_col', [[4, 2], [11, 1], [5, 5], [1, 3]]);
  418. // It remove columns as follow:
  419. // 1--------------------------------9 11-1
  420. // A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1
  421. //
  422. // Result: A1, K1, M1, N1, O1
  423. expect(getDataAtRow(0)).toEqual(['A1', 'K1', 'M1', 'N1', 'O1']);
  424. expect(getData()[0].length).toBe(5);
  425. });
  426. });
  427. it('should not remove column if amount is zero', () => {
  428. handsontable({
  429. data: arrayOfArrays(),
  430. });
  431. const countedColumns = countCols();
  432. alter('remove_col', 1, 0);
  433. expect(countCols()).toBe(countedColumns);
  434. });
  435. it('should remove one column if amount parameter is empty', () => {
  436. handsontable({
  437. data: [
  438. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  439. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  440. ]
  441. });
  442. alter('remove_col', 1);
  443. expect(countCols()).toEqual(7);
  444. expect(spec().$container.find('tr:eq(0) td:eq(0)').html()).toEqual('a');
  445. expect(spec().$container.find('tr:eq(1) td:eq(1)').html()).toEqual('c');
  446. });
  447. it('should remove as many columns as given in the amount parameter', () => {
  448. handsontable({
  449. data: [
  450. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  451. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  452. ]
  453. });
  454. alter('remove_col', 1, 3);
  455. expect(countCols()).toEqual(5);
  456. expect(spec().$container.find('tr:eq(0) td:eq(0)').html()).toEqual('a');
  457. expect(spec().$container.find('tr:eq(1) td:eq(1)').html()).toEqual('e');
  458. });
  459. it('should not remove more columns that exist', () => {
  460. handsontable({
  461. data: [
  462. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  463. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  464. ]
  465. });
  466. alter('remove_col', 6, 3);
  467. expect(countCols()).toEqual(6);
  468. expect(spec().$container.find('tr:eq(1) td:last').html()).toEqual('f');
  469. });
  470. it('should remove one column from end if no parameters are given', () => {
  471. handsontable({
  472. data: [
  473. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  474. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  475. ]
  476. });
  477. alter('remove_col');
  478. expect(countCols()).toEqual(7);
  479. expect(spec().$container.find('tr:eq(1) td:last').html()).toEqual('g');
  480. });
  481. it('should remove amount of columns from end if index parameter is not given', () => {
  482. handsontable({
  483. data: [
  484. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  485. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  486. ]
  487. });
  488. alter('remove_col', null, 3);
  489. expect(countCols()).toEqual(5);
  490. expect(spec().$container.find('tr:eq(1) td:last').html()).toEqual('e');
  491. });
  492. it('should fire beforeRemoveCol event before removing col', () => {
  493. const onBeforeRemoveCol = jasmine.createSpy('onBeforeRemoveCol');
  494. handsontable({
  495. beforeRemoveCol: onBeforeRemoveCol
  496. });
  497. alter('remove_col');
  498. expect(onBeforeRemoveCol).toHaveBeenCalledWith(countCols(), 1, [4], undefined, undefined, undefined);
  499. });
  500. it('should not remove column if removing has been canceled by beforeRemoveCol event handler', () => {
  501. const onBeforeRemoveCol = jasmine.createSpy('onBeforeRemoveCol');
  502. onBeforeRemoveCol.and.callFake(() => false);
  503. handsontable({
  504. beforeRemoveCol: onBeforeRemoveCol
  505. });
  506. expect(countCols()).toEqual(5);
  507. alter('remove_col');
  508. expect(countCols()).toEqual(5);
  509. });
  510. it('should fire callback on remove col', () => {
  511. let outputBefore;
  512. let outputAfter;
  513. handsontable({
  514. minRows: 5,
  515. data: arrayOfArrays(),
  516. beforeRemoveCol(index, amount, removedCols, source) {
  517. outputBefore = [index, amount, removedCols, source];
  518. },
  519. afterRemoveCol(index, amount, removedCols, source) {
  520. outputAfter = [index, amount, removedCols, source];
  521. }
  522. });
  523. alter('remove_col', 1, 2, 'customSource');
  524. expect(outputBefore).toEqual([1, 2, [1, 2], 'customSource']);
  525. expect(outputAfter).toEqual([1, 2, [1, 2], 'customSource']);
  526. });
  527. it('should remove column\'s properties', () => {
  528. handsontable({
  529. startCols: 3,
  530. startRows: 1
  531. });
  532. getCellMeta(0, 0).someValue = [0, 0];
  533. getCellMeta(0, 1).someValue = [0, 1];
  534. getCellMeta(0, 2).someValue = [0, 2];
  535. alter('remove_col', 0);
  536. expect(getCellMeta(0, 0).someValue).toEqual([0, 1]);
  537. expect(getCellMeta(0, 1).someValue).toEqual([0, 2]);
  538. });
  539. it('should remove column when not all rows are visible in the viewport', () => {
  540. spec().$container.css({
  541. height: '100',
  542. overflow: 'auto'
  543. });
  544. handsontable({
  545. startCols: 3,
  546. startRows: 20
  547. });
  548. expect(getHtCore().find('tbody tr').length).toBeLessThan(20);
  549. expect(countCols()).toEqual(3);
  550. alter('remove_col', 0);
  551. expect(countCols()).toEqual(2);
  552. });
  553. it('should not remove column header together with the column, if headers were NOT specified explicitly', () => {
  554. handsontable({
  555. startCols: 3,
  556. startRows: 2,
  557. colHeaders: true
  558. });
  559. expect(getColHeader()).toEqual(['A', 'B', 'C']);
  560. expect(countCols()).toEqual(3);
  561. alter('remove_col', 1);
  562. expect(countCols()).toEqual(2);
  563. expect(getColHeader()).toEqual(['A', 'B']);
  564. });
  565. it('should remove column header together with the column, if headers were specified explicitly', () => {
  566. handsontable({
  567. startCols: 3,
  568. startRows: 2,
  569. colHeaders: ['Header0', 'Header1', 'Header2']
  570. });
  571. expect(getColHeader()).toEqual(['Header0', 'Header1', 'Header2']);
  572. expect(countCols()).toEqual(3);
  573. alter('remove_col', 1);
  574. expect(countCols()).toEqual(2);
  575. expect(getColHeader()).toEqual(['Header0', 'Header2']);
  576. });
  577. it('should decrement the number of fixed columns, if a fix column is removed', () => {
  578. const hot = handsontable({
  579. startCols: 1,
  580. startRows: 3,
  581. fixedColumnsLeft: 4
  582. });
  583. alter('remove_col', 1, 1);
  584. expect(hot.getSettings().fixedColumnsLeft).toEqual(3);
  585. alter('remove_col', 1, 2);
  586. expect(hot.getSettings().fixedColumnsLeft).toEqual(1);
  587. });
  588. it('should shift the cell meta according to the new column layout', () => {
  589. handsontable({
  590. startCols: 4,
  591. startRows: 3
  592. });
  593. setCellMeta(1, 2, 'className', 'test');
  594. alter('remove_col', 1, 1);
  595. expect(getCellMeta(1, 1).className).toEqual('test');
  596. });
  597. it('should shift the cell meta according to the new columns (>1) layout', () => {
  598. handsontable({
  599. startCols: 4,
  600. startRows: 3
  601. });
  602. setCellMeta(1, 2, 'className', 'test');
  603. alter('remove_col', 0, 2);
  604. expect(getCellMeta(1, 0).className).toEqual('test');
  605. });
  606. });
  607. describe('insert row', () => {
  608. it('should insert row at given index', () => {
  609. handsontable({
  610. data: [
  611. ['a1', 'a2', 'a3'],
  612. ['b1', 'b2', 'b3'],
  613. ['c1', 'c2', 'c3'],
  614. ['d1', 'd2', 'd3'],
  615. ['e1', 'e2', 'e3']
  616. ]
  617. });
  618. alter('insert_row', 1);
  619. expect(countRows()).toEqual(6);
  620. expect(spec().$container.find('tr:eq(2) td:eq(0)').html()).toEqual('b1');
  621. });
  622. it('should fire the beforeCreateRow hook before creating a row', () => {
  623. const onBeforeCreateRow = jasmine.createSpy('beforeCreateRow');
  624. handsontable({
  625. data: arrayOfNestedObjects(),
  626. columns: [
  627. { data: 'id' },
  628. { data: 'name.first' }
  629. ],
  630. beforeCreateRow: onBeforeCreateRow,
  631. });
  632. alter('insert_row', 2, 1, 'customSource');
  633. expect(onBeforeCreateRow).toHaveBeenCalledWith(2, 1, 'customSource', void 0, void 0, void 0);
  634. });
  635. it('should not create row if removing has been canceled by beforeCreateRow hook handler', () => {
  636. const beforeCreateRow = jasmine.createSpy('beforeCreateRow');
  637. beforeCreateRow.and.callFake(() => false);
  638. handsontable({
  639. data: arrayOfNestedObjects(),
  640. columns: [
  641. { data: 'id' },
  642. { data: 'name.first' }
  643. ],
  644. beforeCreateRow
  645. });
  646. expect(countRows()).toEqual(3);
  647. alter('insert_row');
  648. expect(countRows()).toEqual(3);
  649. });
  650. it('should insert row at the end if index is not given', () => {
  651. handsontable({
  652. data: [
  653. ['a1', 'a2', 'a3'],
  654. ['b1', 'b2', 'b3'],
  655. ['c1', 'c2', 'c3'],
  656. ['d1', 'd2', 'd3'],
  657. ['e1', 'e2', 'e3']
  658. ]
  659. });
  660. alter('insert_row');
  661. expect(countRows()).toEqual(6);
  662. expect(getHtCore().find('tr:eq(4) td:eq(0)').html()).toEqual('e1');
  663. expect(getHtCore().find('tr:last td:eq(0)').html()).toEqual('');
  664. });
  665. it('should not change cellMeta after executing `insert row` without parameters (#3581, #3989, #2114)', () => {
  666. const greenRenderer = function(instance, td, ...args) {
  667. Handsontable.renderers.TextRenderer.apply(this, [instance, td, ...args]);
  668. td.style.backgroundColor = 'green';
  669. };
  670. handsontable({
  671. data: [
  672. [0, 'a', true],
  673. [1, 'b', false],
  674. [2, 'c', true],
  675. [3, 'd', true]
  676. ],
  677. cell: [
  678. { row: 0, col: 0, renderer: greenRenderer, type: 'text', readOnly: true }
  679. ],
  680. columns: [
  681. { type: 'numeric' },
  682. { type: 'text' },
  683. { type: 'checkbox' }
  684. ]
  685. });
  686. alter('insert_row');
  687. expect(getCellMeta(1, 0).renderer).not.toBe(greenRenderer);
  688. expect(getCellMeta(1, 0).readOnly).toBe(false);
  689. expect(getCellMeta(4, 0).renderer).not.toBe(greenRenderer);
  690. expect(getCellMeta(4, 0).readOnly).toBe(false);
  691. });
  692. it('should add new row which respect defined type of cells after executing `insert_row`', () => {
  693. handsontable({
  694. data: [
  695. [0, 'a', true],
  696. [1, 'b', false],
  697. [2, 'c', true],
  698. [3, 'd', true]
  699. ],
  700. cell: [
  701. { row: 0, col: 0, type: 'text' }
  702. ],
  703. columns: [
  704. { type: 'numeric' },
  705. { type: 'text' },
  706. { type: 'checkbox' }
  707. ]
  708. });
  709. alter('insert_row');
  710. // added row
  711. expect(getCellMeta(4, 0).type).toEqual('numeric');
  712. expect(getDataAtCell(4, 0)).toEqual(null);
  713. expect(getCellMeta(4, 2).type).toEqual('checkbox');
  714. expect(getDataAtCell(4, 2)).toEqual(null);
  715. });
  716. it('should insert the amount of rows at given index', () => {
  717. handsontable({
  718. data: [
  719. ['a1', 'a2', 'a3'],
  720. ['b1', 'b2', 'b3'],
  721. ['c1', 'c2', 'c3'],
  722. ['d1', 'd2', 'd3'],
  723. ['e1', 'e2', 'e3']
  724. ]
  725. });
  726. alter('insert_row', 1, 3);
  727. expect(countRows()).toEqual(8);
  728. expect(spec().$container.find('tr:eq(1) td:eq(0)').html()).toEqual('');
  729. expect(spec().$container.find('tr:eq(4) td:eq(0)').html()).toEqual('b1');
  730. });
  731. it('should insert the amount of rows at the end if index is not given', () => {
  732. handsontable({
  733. data: [
  734. ['a1', 'a2', 'a3'],
  735. ['b1', 'b2', 'b3'],
  736. ['c1', 'c2', 'c3'],
  737. ['d1', 'd2', 'd3'],
  738. ['e1', 'e2', 'e3']
  739. ]
  740. });
  741. alter('insert_row', null, 3);
  742. expect(countRows()).toEqual(8);
  743. expect(getHtCore().find('tr:eq(4) td:eq(0)').html()).toEqual('e1');
  744. expect(getHtCore().find('tr:eq(5) td:eq(0)').html()).toEqual('');
  745. expect(getHtCore().find('tr:eq(6) td:eq(0)').html()).toEqual('');
  746. expect(getHtCore().find('tr:eq(7) td:eq(0)').html()).toEqual('');
  747. });
  748. it('should insert not more rows than maxRows', () => {
  749. handsontable({
  750. startRows: 5,
  751. maxRows: 7
  752. });
  753. alter('insert_row', 1);
  754. alter('insert_row', 1);
  755. alter('insert_row', 1);
  756. expect(countRows()).toEqual(7);
  757. });
  758. it('when amount parameter is used, should not insert more rows than allowed by maxRows', () => {
  759. handsontable({
  760. data: [
  761. ['a1', 'a2', 'a3'],
  762. ['b1', 'b2', 'b3'],
  763. ['c1', 'c2', 'c3'],
  764. ['d1', 'd2', 'd3'],
  765. ['e1', 'e2', 'e3']
  766. ],
  767. maxRows: 10
  768. });
  769. alter('insert_row', 1, 10);
  770. expect(countRows()).toEqual(10);
  771. expect(spec().$container.find('tr:eq(6) td:eq(0)').html()).toEqual('b1');
  772. });
  773. it('should not add more source rows than defined in maxRows when trimming rows using the modifyRow hook', () => {
  774. const hot = handsontable({
  775. data: Handsontable.helper.createSpreadsheetData(10, 4),
  776. modifyRow(row) {
  777. return [8, 9].indexOf(row) > -1 ? null : row;
  778. },
  779. maxRows: 10
  780. });
  781. expect(hot.countRows()).toEqual(8);
  782. hot.populateFromArray(7, 0, [['a'], ['b'], ['c']]);
  783. expect(hot.countSourceRows()).toEqual(10);
  784. expect(hot.getDataAtCell(7, 0)).toEqual('a');
  785. });
  786. it('should fire callback on create row', () => {
  787. let outputBefore;
  788. let outputAfter;
  789. handsontable({
  790. minRows: 5,
  791. data: arrayOfNestedObjects(),
  792. columns: [
  793. { data: 'id' },
  794. { data: 'name.first' }
  795. ],
  796. beforeCreateRow(index, amount, source) {
  797. outputBefore = [index, amount, source];
  798. },
  799. afterCreateRow(index, amount, source) {
  800. outputAfter = [index, amount, source];
  801. },
  802. });
  803. alter('insert_row', 3, 1, 'customSource');
  804. expect(outputBefore).toEqual([3, 1, 'customSource']);
  805. expect(outputAfter).toEqual([3, 1, 'customSource']);
  806. });
  807. it('should keep the single-cell selection in the same position as before inserting the row', () => {
  808. handsontable({
  809. minRows: 5,
  810. data: [
  811. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  812. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  813. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  814. ]
  815. });
  816. selectCell(2, 2);
  817. alter('insert_row', 2);
  818. const selected = getSelected();
  819. expect(selected[0][0]).toBe(3);
  820. expect(selected[0][2]).toBe(3);
  821. expect(selected.length).toBe(1);
  822. });
  823. it('should shift the cell meta according to the new row layout', () => {
  824. handsontable({
  825. startCols: 4,
  826. startRows: 3
  827. });
  828. setCellMeta(2, 1, 'className', 'test');
  829. alter('insert_row', 1, 1);
  830. expect(getCellMeta(3, 1).className).toEqual('test');
  831. });
  832. it('should shift the cell meta according to the new rows (>1) layout', () => {
  833. handsontable({
  834. startCols: 4,
  835. startRows: 3
  836. });
  837. setCellMeta(2, 1, 'className', 'test');
  838. alter('insert_row', 0, 3);
  839. expect(getCellMeta(5, 1).className).toEqual('test');
  840. });
  841. });
  842. describe('insert column', () => {
  843. it('should insert column at given index', () => {
  844. handsontable({
  845. data: [
  846. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  847. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  848. ]
  849. });
  850. alter('insert_col', 1);
  851. expect(countCols()).toEqual(9);
  852. expect(spec().$container.find('tr:eq(1) td:eq(2)').html()).toEqual('b');
  853. });
  854. it('should insert column at the end if index is not given', () => {
  855. handsontable({
  856. data: [
  857. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  858. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  859. ]
  860. });
  861. alter('insert_col');
  862. expect(countCols()).toEqual(9);
  863. expect(spec().$container.find('tr:eq(1) td:eq(7)').html()).toEqual('h');
  864. });
  865. it('should insert the amount of columns at given index', () => {
  866. handsontable({
  867. data: [
  868. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  869. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  870. ]
  871. });
  872. alter('insert_col', 1, 3);
  873. expect(countCols()).toEqual(11);
  874. expect(spec().$container.find('tr:eq(1) td:eq(4)').html()).toEqual('b');
  875. });
  876. it('should insert the amount of columns at the end if index is not given', () => {
  877. handsontable({
  878. data: [
  879. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  880. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  881. ]
  882. });
  883. alter('insert_col', null, 3);
  884. expect(countCols()).toEqual(11);
  885. expect(spec().$container.find('tr:eq(1) td:eq(7)').html()).toEqual('h');
  886. expect(spec().$container.find('tr:eq(1) td:eq(8)').html()).toEqual('');
  887. expect(spec().$container.find('tr:eq(1) td:eq(9)').html()).toEqual('');
  888. expect(spec().$container.find('tr:eq(1) td:eq(10)').html()).toEqual('');
  889. });
  890. it('should insert not more cols than maxCols', () => {
  891. handsontable({
  892. startCols: 5,
  893. maxCols: 7
  894. });
  895. alter('insert_col', 1);
  896. alter('insert_col', 1);
  897. alter('insert_col', 1);
  898. expect(countCols()).toEqual(7);
  899. });
  900. it('should not insert more columns than allowed by maxCols, when amount parameter is used', () => {
  901. handsontable({
  902. data: [
  903. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  904. ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  905. ],
  906. maxCols: 10
  907. });
  908. alter('insert_col', 1, 10);
  909. expect(countCols()).toEqual(10);
  910. expect(spec().$container.find('tr:eq(1) td:eq(1)').html()).toEqual('');
  911. expect(spec().$container.find('tr:eq(1) td:eq(2)').html()).toEqual('');
  912. expect(spec().$container.find('tr:eq(1) td:eq(3)').html()).toEqual('b');
  913. });
  914. it('should fire callback on create col', () => {
  915. let outputBefore;
  916. let outputAfter;
  917. handsontable({
  918. minRows: 5,
  919. data: arrayOfArrays(),
  920. beforeCreateCol(index, amount, source) {
  921. outputBefore = [index, amount, source];
  922. },
  923. afterCreateCol(index, amount, source) {
  924. outputAfter = [index, amount, source];
  925. },
  926. });
  927. alter('insert_col', 2, 1, 'customSource');
  928. expect(outputBefore).toEqual([2, 1, 'customSource']);
  929. expect(outputAfter).toEqual([2, 1, 'customSource']);
  930. });
  931. it('should not create column header together with the column, if headers were NOT specified explicitly', () => {
  932. handsontable({
  933. startCols: 3,
  934. startRows: 2,
  935. colHeaders: true
  936. });
  937. expect(getColHeader()).toEqual(['A', 'B', 'C']);
  938. expect(countCols()).toEqual(3);
  939. alter('insert_col', 1);
  940. expect(countCols()).toEqual(4);
  941. expect(getColHeader()).toEqual(['A', 'B', 'C', 'D']);
  942. });
  943. it('should create column header together with the column, if headers were specified explicitly', () => {
  944. handsontable({
  945. startCols: 3,
  946. startRows: 2,
  947. colHeaders: ['Header0', 'Header1', 'Header2']
  948. });
  949. expect(getColHeader()).toEqual(['Header0', 'Header1', 'Header2']);
  950. expect(countCols()).toEqual(3);
  951. alter('insert_col', 1);
  952. expect(countCols()).toEqual(4);
  953. expect(getColHeader()).toEqual(['Header0', 'B', 'Header1', 'Header2']);
  954. });
  955. it('should stretch the table after adding another column (if stretching is set to \'all\')', () => {
  956. spec().$container.css({
  957. width: 500,
  958. });
  959. const hot = handsontable({
  960. startCols: 5,
  961. startRows: 10,
  962. stretchH: 'all'
  963. });
  964. expect(Handsontable.dom.outerWidth(hot.view.TBODY)).toEqual(500);
  965. alter('insert_col', null, 1);
  966. expect(Handsontable.dom.outerWidth(hot.view.TBODY)).toEqual(500);
  967. alter('insert_col', null, 1);
  968. expect(Handsontable.dom.outerWidth(hot.view.TBODY)).toEqual(500);
  969. });
  970. it('should shift the cell meta according to the new column layout', () => {
  971. handsontable({
  972. startCols: 4,
  973. startRows: 3
  974. });
  975. setCellMeta(1, 2, 'className', 'test');
  976. alter('insert_col', 1, 1);
  977. expect(getCellMeta(1, 3).className).toEqual('test');
  978. });
  979. it('should shift the cell meta according to the new columns (>1) layout', () => {
  980. handsontable({
  981. startCols: 4,
  982. startRows: 3
  983. });
  984. setCellMeta(1, 2, 'className', 'test');
  985. alter('insert_col', 0, 3);
  986. expect(getCellMeta(1, 5).className).toEqual('test');
  987. });
  988. });
  989. });