| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216 |
- describe('Core_alter', () => {
- const id = 'testContainer';
- beforeEach(function() {
- this.$container = $(`<div id="${id}"></div>`).appendTo('body');
- });
- afterEach(function() {
- if (this.$container) {
- destroy();
- this.$container.remove();
- }
- });
- const arrayOfNestedObjects = function() {
- return [
- { id: 1,
- name: {
- first: 'Ted',
- last: 'Right'
- },
- address: 'Street Name',
- zip: '80410',
- city: 'City Name' },
- { id: 2,
- name: {
- first: 'Frank',
- last: 'Honest'
- },
- address: 'Street Name',
- zip: '80410',
- city: 'City Name' },
- { id: 3,
- name: {
- first: 'Joan',
- last: 'Well'
- },
- address: 'Street Name',
- zip: '80410',
- city: 'City Name' }
- ];
- };
- const arrayOfArrays = function() {
- return [
- ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
- ['2008', 10, 11, 12, 13],
- ['2009', 20, 11, 14, 13],
- ['2010', 30, 15, 12, 13]
- ];
- };
- describe('remove row', () => {
- describe('multiple items at once', () => {
- it('should remove rows when index groups are passed in ascending order', () => {
- handsontable({
- data: Handsontable.helper.createSpreadsheetData(15, 5),
- });
- // [[rowVisualIndex, amountRowsToRemove] ...]
- alter('remove_row', [[1, 3], [5, 1], [7, 3], [11, 2]]);
- // It remove rows as follow:
- // 1--------3 5-1 7---------3 11-----2
- // A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
- //
- // Result: A1, A5, A7, A11, A14, A15
- expect(getDataAtCol(0)).toEqual(['A1', 'A5', 'A7', 'A11', 'A14', 'A15']);
- expect(getData().length).toBe(6);
- });
- it('should remove rows when index groups are passed in descending order', () => {
- handsontable({
- data: Handsontable.helper.createSpreadsheetData(15, 5),
- });
- // [[rowVisualIndex, amountRowsToRemove] ...]
- alter('remove_row', [[11, 2], [7, 3], [5, 1], [1, 3]]);
- // It remove rows as follow:
- // 1--------3 5-1 7---------3 11-----2
- // A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
- //
- // Result: A1, A5, A7, A11, A14, A15
- expect(getDataAtCol(0)).toEqual(['A1', 'A5', 'A7', 'A11', 'A14', 'A15']);
- expect(getData().length).toBe(6);
- });
- it('should remove rows when index groups are passed as intersecting values', () => {
- handsontable({
- data: Handsontable.helper.createSpreadsheetData(15, 5),
- });
- // [[rowVisualIndex, amountRowsToRemove] ...]
- alter('remove_row', [[1, 3], [4, 2], [5, 5], [11, 1]]);
- // It remove rows as follow:
- // 1---------------------------------9 11-1
- // A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
- //
- // Result: A1, A11, A13, A14, A15
- expect(getDataAtCol(0)).toEqual(['A1', 'A11', 'A13', 'A14', 'A15']);
- expect(getData().length).toBe(5);
- });
- it('should remove rows when index groups are passed as intersecting values (the second scenario)', () => {
- handsontable({
- data: Handsontable.helper.createSpreadsheetData(15, 5),
- });
- // [[rowVisualIndex, amountRowsToRemove] ...]
- alter('remove_row', [[1, 3], [2, 1], [5, 2]]);
- // It remove columns as follow:
- // 1--------3 5----2
- // A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
- //
- // Result: A1, A5, A8, A9, A10, A11, A12, A13, A14, A15
- expect(getDataAtCol(0)).toEqual(['A1', 'A5', 'A8', 'A9', 'A10', 'A11', 'A12', 'A13', 'A14', 'A15']);
- expect(getData().length).toBe(10);
- });
- it('should remove rows when index groups are passed as intersecting values (placed randomly)', () => {
- handsontable({
- data: Handsontable.helper.createSpreadsheetData(15, 5),
- });
- // [[rowVisualIndex, amountRowsToRemove] ...]
- alter('remove_row', [[4, 2], [11, 1], [5, 5], [1, 3]]);
- // It remove rows as follow:
- // 1---------------------------------9 11-1
- // A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
- //
- // Result: A1, A11, A13, A14, A15
- expect(getDataAtCol(0)).toEqual(['A1', 'A11', 'A13', 'A14', 'A15']);
- expect(getData().length).toBe(5);
- });
- });
- it('should remove row', () => {
- handsontable({
- minRows: 5,
- data: arrayOfNestedObjects(),
- columns: [
- { data: 'id' },
- { data: 'name.first' }
- ]
- });
- alter('remove_row', 1);
- expect(getDataAtCell(1, 1)).toEqual('Joan'); // Joan should be moved up
- expect(getData().length).toEqual(5); // new row should be added by keepEmptyRows
- });
- it('should not remove row if amount is zero', () => {
- handsontable({
- data: arrayOfNestedObjects(),
- columns: [
- { data: 'id' },
- { data: 'name.first' }
- ],
- });
- const countedRows = countRows();
- alter('remove_row', 1, 0);
- expect(countRows()).toBe(countedRows);
- });
- it('should fire beforeRemoveRow event before removing row', () => {
- const onBeforeRemoveRow = jasmine.createSpy('onBeforeRemoveRow');
- handsontable({
- data: arrayOfNestedObjects(),
- columns: [
- { data: 'id' },
- { data: 'name.first' }
- ],
- beforeRemoveRow: onBeforeRemoveRow,
- });
- alter('remove_row', 2, 1, 'customSource');
- expect(onBeforeRemoveRow).toHaveBeenCalledWith(countRows(), 1, [2], 'customSource', undefined, undefined);
- });
- it('should not remove row if removing has been canceled by beforeRemoveRow event handler', () => {
- const onBeforeRemoveRow = jasmine.createSpy('onBeforeRemoveRow');
- onBeforeRemoveRow.and.callFake(() => false);
- handsontable({
- data: arrayOfNestedObjects(),
- columns: [
- { data: 'id' },
- { data: 'name.first' }
- ],
- beforeRemoveRow: onBeforeRemoveRow
- });
- expect(countRows()).toEqual(3);
- alter('remove_row');
- expect(countRows()).toEqual(3);
- });
- it('should not remove rows below minRows', () => {
- handsontable({
- startRows: 5,
- minRows: 4
- });
- alter('remove_row', 1);
- alter('remove_row', 1);
- alter('remove_row', 1);
- expect(countRows()).toEqual(4);
- });
- it('should not remove cols below minCols', () => {
- handsontable({
- startCols: 5,
- minCols: 4
- });
- alter('remove_col', 1);
- alter('remove_col', 1);
- alter('remove_col', 1);
- expect(countCols()).toEqual(4);
- });
- it('should remove one row if amount parameter is empty', () => {
- handsontable({
- data: [
- ['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3'],
- ['c1', 'c2', 'c3'],
- ['d1', 'd2', 'd3'],
- ['e1', 'e2', 'e3']
- ]
- });
- alter('remove_row', 1);
- expect(countRows()).toEqual(4);
- expect(spec().$container.find('tr:eq(0) td:eq(0)').html()).toEqual('a1');
- expect(spec().$container.find('tr:eq(1) td:eq(1)').html()).toEqual('c2');
- });
- it('should remove as many rows as given in the amount parameter', () => {
- handsontable({
- data: [
- ['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3'],
- ['c1', 'c2', 'c3'],
- ['d1', 'd2', 'd3'],
- ['e1', 'e2', 'e3']
- ]
- });
- alter('remove_row', 1, 3);
- expect(countRows()).toEqual(2);
- expect(spec().$container.find('tr:eq(0) td:eq(0)').html()).toEqual('a1');
- expect(spec().$container.find('tr:eq(1) td:eq(1)').html()).toEqual('e2');
- });
- it('should not remove more rows that exist', () => {
- handsontable({
- data: [
- ['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3'],
- ['c1', 'c2', 'c3'],
- ['d1', 'd2', 'd3'],
- ['e1', 'e2', 'e3']
- ]
- });
- alter('remove_row', 1, 10);
- expect(countRows()).toEqual(1);
- expect(getHtCore().find('tr:last td:last').html()).toEqual('a3');
- });
- it('should remove one row from end if no parameters are given', () => {
- handsontable({
- data: [
- ['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3'],
- ['c1', 'c2', 'c3'],
- ['d1', 'd2', 'd3'],
- ['e1', 'e2', 'e3']
- ]
- });
- alter('remove_row');
- expect(countRows()).toEqual(4);
- expect(getHtCore().find('tr:last td:eq(0)').html()).toEqual('d1');
- });
- it('should remove amount of rows from end if index parameter is not given', () => {
- handsontable({
- data: [
- ['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3'],
- ['c1', 'c2', 'c3'],
- ['d1', 'd2', 'd3'],
- ['e1', 'e2', 'e3']
- ]
- });
- alter('remove_row', null, 3);
- expect(countRows()).toEqual(2);
- expect(getHtCore().find('tr:last td:eq(0)').html()).toEqual('b1');
- });
- it('should remove rows from table with fixedRows', () => {
- handsontable({
- data: [
- ['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3']
- ],
- fixedRowsTop: 1,
- minSpareRows: 0
- });
- alter('remove_row', 1);
- expect(countRows()).toEqual(1);
- });
- it('should remove all rows from table with fixedRows', () => {
- handsontable({
- data: [
- ['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3']
- ],
- fixedRowsTop: 1,
- minSpareRows: 0
- });
- alter('remove_row', 1);
- alter('remove_row', 1);
- expect(countRows()).toEqual(0);
- });
- it('should remove row\'s cellProperties', () => {
- handsontable({
- startCols: 1,
- startRows: 3
- });
- getCellMeta(0, 0).someValue = [0, 0];
- getCellMeta(1, 0).someValue = [1, 0];
- getCellMeta(2, 0).someValue = [2, 0];
- alter('remove_row', 0);
- expect(getCellMeta(0, 0).someValue).toEqual([1, 0]);
- expect(getCellMeta(1, 0).someValue).toEqual([2, 0]);
- });
- it('should fire callback on remove row', () => {
- let outputBefore;
- let outputAfter;
- handsontable({
- minRows: 5,
- data: arrayOfNestedObjects(),
- columns: [
- { data: 'id' },
- { data: 'name.first' }
- ],
- beforeRemoveRow(index, amount, removedRows, source) {
- outputBefore = [index, amount, removedRows, source];
- },
- afterRemoveRow(index, amount, removedRows, source) {
- outputAfter = [index, amount, removedRows, source];
- }
- });
- alter('remove_row', 1, 2, 'customSource');
- expect(outputBefore).toEqual([1, 2, [1, 2], 'customSource']);
- expect(outputAfter).toEqual([1, 2, [1, 2], 'customSource']);
- });
- it('should decrement the number of fixed rows, if a fix row is removed', () => {
- const hot = handsontable({
- startCols: 1,
- startRows: 3,
- fixedRowsTop: 4
- });
- alter('remove_row', 1, 1);
- expect(hot.getSettings().fixedRowsTop).toEqual(3);
- alter('remove_row', 1, 2);
- expect(hot.getSettings().fixedRowsTop).toEqual(1);
- });
- it('should shift the cell meta according to the new row layout', () => {
- handsontable({
- startCols: 3,
- startRows: 4
- });
- setCellMeta(2, 1, 'className', 'test');
- alter('remove_row', 1, 1);
- expect(getCellMeta(1, 1).className).toEqual('test');
- });
- it('should shift the cell meta according to the new rows (>1) layout', () => {
- handsontable({
- startCols: 3,
- startRows: 4
- });
- setCellMeta(2, 1, 'className', 'test');
- alter('remove_row', 0, 2);
- expect(getCellMeta(0, 1).className).toEqual('test');
- });
- });
- describe('remove column', () => {
- describe('multiple items at once', () => {
- it('should remove columns when index groups are passed in ascending order', () => {
- handsontable({
- data: Handsontable.helper.createSpreadsheetData(5, 15),
- });
- // [[columnVisualIndex, amountColumnsToRemove] ...]
- alter('remove_col', [[1, 3], [5, 1], [7, 3], [11, 2]]);
- // It remove columns as follow:
- // 1--------3 5-1 7--------3 11---2
- // A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1
- //
- // Result: A1, E1, G1, K1, N1, O1
- expect(getDataAtRow(0)).toEqual(['A1', 'E1', 'G1', 'K1', 'N1', 'O1']);
- expect(getData()[0].length).toBe(6);
- });
- it('should remove columns when index groups are passed in descending order', () => {
- handsontable({
- data: Handsontable.helper.createSpreadsheetData(5, 15),
- });
- // [[columnVisualIndex, amountColumnsToRemove] ...]
- alter('remove_col', [[11, 2], [7, 3], [5, 1], [1, 3]]);
- // It remove columns as follow:
- // 1--------3 5-1 7--------3 11---2
- // A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1
- //
- // Result: A1, E1, G1, K1, N1, O1
- expect(getDataAtRow(0)).toEqual(['A1', 'E1', 'G1', 'K1', 'N1', 'O1']);
- expect(getData()[0].length).toBe(6);
- });
- it('should remove columns when index groups are passed as intersecting values', () => {
- handsontable({
- data: Handsontable.helper.createSpreadsheetData(5, 15),
- });
- // [[columnVisualIndex, amountColumnsToRemove] ...]
- alter('remove_col', [[1, 3], [4, 2], [5, 5], [11, 1]]);
- // It remove columns as follow:
- // 1--------------------------------9 11-1
- // A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1
- //
- // Result: A1, K1, M1, N1, O1
- expect(getDataAtRow(0)).toEqual(['A1', 'K1', 'M1', 'N1', 'O1']);
- expect(getData()[0].length).toBe(5);
- });
- it('should remove columns when index groups are passed as intersecting values (the second scenario)', () => {
- handsontable({
- data: Handsontable.helper.createSpreadsheetData(5, 15),
- });
- // [[columnVisualIndex, amountColumnsToRemove] ...]
- alter('remove_col', [[1, 3], [2, 1], [5, 2]]);
- // It remove columns as follow:
- // 1--------3 5----2
- // A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1
- //
- // Result: A1, E1, H1
- expect(getDataAtRow(0)).toEqual(['A1', 'E1', 'H1', 'I1', 'J1', 'K1', 'L1', 'M1', 'N1', 'O1']);
- expect(getData()[0].length).toBe(10);
- });
- it('should remove columns when index groups are passed as intersecting values (placed randomly)', () => {
- handsontable({
- data: Handsontable.helper.createSpreadsheetData(5, 15),
- });
- // [[columnVisualIndex, amountColumnsToRemove] ...]
- alter('remove_col', [[4, 2], [11, 1], [5, 5], [1, 3]]);
- // It remove columns as follow:
- // 1--------------------------------9 11-1
- // A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1
- //
- // Result: A1, K1, M1, N1, O1
- expect(getDataAtRow(0)).toEqual(['A1', 'K1', 'M1', 'N1', 'O1']);
- expect(getData()[0].length).toBe(5);
- });
- });
- it('should not remove column if amount is zero', () => {
- handsontable({
- data: arrayOfArrays(),
- });
- const countedColumns = countCols();
- alter('remove_col', 1, 0);
- expect(countCols()).toBe(countedColumns);
- });
- it('should remove one column if amount parameter is empty', () => {
- handsontable({
- data: [
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
- ]
- });
- alter('remove_col', 1);
- expect(countCols()).toEqual(7);
- expect(spec().$container.find('tr:eq(0) td:eq(0)').html()).toEqual('a');
- expect(spec().$container.find('tr:eq(1) td:eq(1)').html()).toEqual('c');
- });
- it('should remove as many columns as given in the amount parameter', () => {
- handsontable({
- data: [
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
- ]
- });
- alter('remove_col', 1, 3);
- expect(countCols()).toEqual(5);
- expect(spec().$container.find('tr:eq(0) td:eq(0)').html()).toEqual('a');
- expect(spec().$container.find('tr:eq(1) td:eq(1)').html()).toEqual('e');
- });
- it('should not remove more columns that exist', () => {
- handsontable({
- data: [
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
- ]
- });
- alter('remove_col', 6, 3);
- expect(countCols()).toEqual(6);
- expect(spec().$container.find('tr:eq(1) td:last').html()).toEqual('f');
- });
- it('should remove one column from end if no parameters are given', () => {
- handsontable({
- data: [
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
- ]
- });
- alter('remove_col');
- expect(countCols()).toEqual(7);
- expect(spec().$container.find('tr:eq(1) td:last').html()).toEqual('g');
- });
- it('should remove amount of columns from end if index parameter is not given', () => {
- handsontable({
- data: [
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
- ]
- });
- alter('remove_col', null, 3);
- expect(countCols()).toEqual(5);
- expect(spec().$container.find('tr:eq(1) td:last').html()).toEqual('e');
- });
- it('should fire beforeRemoveCol event before removing col', () => {
- const onBeforeRemoveCol = jasmine.createSpy('onBeforeRemoveCol');
- handsontable({
- beforeRemoveCol: onBeforeRemoveCol
- });
- alter('remove_col');
- expect(onBeforeRemoveCol).toHaveBeenCalledWith(countCols(), 1, [4], undefined, undefined, undefined);
- });
- it('should not remove column if removing has been canceled by beforeRemoveCol event handler', () => {
- const onBeforeRemoveCol = jasmine.createSpy('onBeforeRemoveCol');
- onBeforeRemoveCol.and.callFake(() => false);
- handsontable({
- beforeRemoveCol: onBeforeRemoveCol
- });
- expect(countCols()).toEqual(5);
- alter('remove_col');
- expect(countCols()).toEqual(5);
- });
- it('should fire callback on remove col', () => {
- let outputBefore;
- let outputAfter;
- handsontable({
- minRows: 5,
- data: arrayOfArrays(),
- beforeRemoveCol(index, amount, removedCols, source) {
- outputBefore = [index, amount, removedCols, source];
- },
- afterRemoveCol(index, amount, removedCols, source) {
- outputAfter = [index, amount, removedCols, source];
- }
- });
- alter('remove_col', 1, 2, 'customSource');
- expect(outputBefore).toEqual([1, 2, [1, 2], 'customSource']);
- expect(outputAfter).toEqual([1, 2, [1, 2], 'customSource']);
- });
- it('should remove column\'s properties', () => {
- handsontable({
- startCols: 3,
- startRows: 1
- });
- getCellMeta(0, 0).someValue = [0, 0];
- getCellMeta(0, 1).someValue = [0, 1];
- getCellMeta(0, 2).someValue = [0, 2];
- alter('remove_col', 0);
- expect(getCellMeta(0, 0).someValue).toEqual([0, 1]);
- expect(getCellMeta(0, 1).someValue).toEqual([0, 2]);
- });
- it('should remove column when not all rows are visible in the viewport', () => {
- spec().$container.css({
- height: '100',
- overflow: 'auto'
- });
- handsontable({
- startCols: 3,
- startRows: 20
- });
- expect(getHtCore().find('tbody tr').length).toBeLessThan(20);
- expect(countCols()).toEqual(3);
- alter('remove_col', 0);
- expect(countCols()).toEqual(2);
- });
- it('should not remove column header together with the column, if headers were NOT specified explicitly', () => {
- handsontable({
- startCols: 3,
- startRows: 2,
- colHeaders: true
- });
- expect(getColHeader()).toEqual(['A', 'B', 'C']);
- expect(countCols()).toEqual(3);
- alter('remove_col', 1);
- expect(countCols()).toEqual(2);
- expect(getColHeader()).toEqual(['A', 'B']);
- });
- it('should remove column header together with the column, if headers were specified explicitly', () => {
- handsontable({
- startCols: 3,
- startRows: 2,
- colHeaders: ['Header0', 'Header1', 'Header2']
- });
- expect(getColHeader()).toEqual(['Header0', 'Header1', 'Header2']);
- expect(countCols()).toEqual(3);
- alter('remove_col', 1);
- expect(countCols()).toEqual(2);
- expect(getColHeader()).toEqual(['Header0', 'Header2']);
- });
- it('should decrement the number of fixed columns, if a fix column is removed', () => {
- const hot = handsontable({
- startCols: 1,
- startRows: 3,
- fixedColumnsLeft: 4
- });
- alter('remove_col', 1, 1);
- expect(hot.getSettings().fixedColumnsLeft).toEqual(3);
- alter('remove_col', 1, 2);
- expect(hot.getSettings().fixedColumnsLeft).toEqual(1);
- });
- it('should shift the cell meta according to the new column layout', () => {
- handsontable({
- startCols: 4,
- startRows: 3
- });
- setCellMeta(1, 2, 'className', 'test');
- alter('remove_col', 1, 1);
- expect(getCellMeta(1, 1).className).toEqual('test');
- });
- it('should shift the cell meta according to the new columns (>1) layout', () => {
- handsontable({
- startCols: 4,
- startRows: 3
- });
- setCellMeta(1, 2, 'className', 'test');
- alter('remove_col', 0, 2);
- expect(getCellMeta(1, 0).className).toEqual('test');
- });
- });
- describe('insert row', () => {
- it('should insert row at given index', () => {
- handsontable({
- data: [
- ['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3'],
- ['c1', 'c2', 'c3'],
- ['d1', 'd2', 'd3'],
- ['e1', 'e2', 'e3']
- ]
- });
- alter('insert_row', 1);
- expect(countRows()).toEqual(6);
- expect(spec().$container.find('tr:eq(2) td:eq(0)').html()).toEqual('b1');
- });
- it('should fire the beforeCreateRow hook before creating a row', () => {
- const onBeforeCreateRow = jasmine.createSpy('beforeCreateRow');
- handsontable({
- data: arrayOfNestedObjects(),
- columns: [
- { data: 'id' },
- { data: 'name.first' }
- ],
- beforeCreateRow: onBeforeCreateRow,
- });
- alter('insert_row', 2, 1, 'customSource');
- expect(onBeforeCreateRow).toHaveBeenCalledWith(2, 1, 'customSource', void 0, void 0, void 0);
- });
- it('should not create row if removing has been canceled by beforeCreateRow hook handler', () => {
- const beforeCreateRow = jasmine.createSpy('beforeCreateRow');
- beforeCreateRow.and.callFake(() => false);
- handsontable({
- data: arrayOfNestedObjects(),
- columns: [
- { data: 'id' },
- { data: 'name.first' }
- ],
- beforeCreateRow
- });
- expect(countRows()).toEqual(3);
- alter('insert_row');
- expect(countRows()).toEqual(3);
- });
- it('should insert row at the end if index is not given', () => {
- handsontable({
- data: [
- ['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3'],
- ['c1', 'c2', 'c3'],
- ['d1', 'd2', 'd3'],
- ['e1', 'e2', 'e3']
- ]
- });
- alter('insert_row');
- expect(countRows()).toEqual(6);
- expect(getHtCore().find('tr:eq(4) td:eq(0)').html()).toEqual('e1');
- expect(getHtCore().find('tr:last td:eq(0)').html()).toEqual('');
- });
- it('should not change cellMeta after executing `insert row` without parameters (#3581, #3989, #2114)', () => {
- const greenRenderer = function(instance, td, ...args) {
- Handsontable.renderers.TextRenderer.apply(this, [instance, td, ...args]);
- td.style.backgroundColor = 'green';
- };
- handsontable({
- data: [
- [0, 'a', true],
- [1, 'b', false],
- [2, 'c', true],
- [3, 'd', true]
- ],
- cell: [
- { row: 0, col: 0, renderer: greenRenderer, type: 'text', readOnly: true }
- ],
- columns: [
- { type: 'numeric' },
- { type: 'text' },
- { type: 'checkbox' }
- ]
- });
- alter('insert_row');
- expect(getCellMeta(1, 0).renderer).not.toBe(greenRenderer);
- expect(getCellMeta(1, 0).readOnly).toBe(false);
- expect(getCellMeta(4, 0).renderer).not.toBe(greenRenderer);
- expect(getCellMeta(4, 0).readOnly).toBe(false);
- });
- it('should add new row which respect defined type of cells after executing `insert_row`', () => {
- handsontable({
- data: [
- [0, 'a', true],
- [1, 'b', false],
- [2, 'c', true],
- [3, 'd', true]
- ],
- cell: [
- { row: 0, col: 0, type: 'text' }
- ],
- columns: [
- { type: 'numeric' },
- { type: 'text' },
- { type: 'checkbox' }
- ]
- });
- alter('insert_row');
- // added row
- expect(getCellMeta(4, 0).type).toEqual('numeric');
- expect(getDataAtCell(4, 0)).toEqual(null);
- expect(getCellMeta(4, 2).type).toEqual('checkbox');
- expect(getDataAtCell(4, 2)).toEqual(null);
- });
- it('should insert the amount of rows at given index', () => {
- handsontable({
- data: [
- ['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3'],
- ['c1', 'c2', 'c3'],
- ['d1', 'd2', 'd3'],
- ['e1', 'e2', 'e3']
- ]
- });
- alter('insert_row', 1, 3);
- expect(countRows()).toEqual(8);
- expect(spec().$container.find('tr:eq(1) td:eq(0)').html()).toEqual('');
- expect(spec().$container.find('tr:eq(4) td:eq(0)').html()).toEqual('b1');
- });
- it('should insert the amount of rows at the end if index is not given', () => {
- handsontable({
- data: [
- ['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3'],
- ['c1', 'c2', 'c3'],
- ['d1', 'd2', 'd3'],
- ['e1', 'e2', 'e3']
- ]
- });
- alter('insert_row', null, 3);
- expect(countRows()).toEqual(8);
- expect(getHtCore().find('tr:eq(4) td:eq(0)').html()).toEqual('e1');
- expect(getHtCore().find('tr:eq(5) td:eq(0)').html()).toEqual('');
- expect(getHtCore().find('tr:eq(6) td:eq(0)').html()).toEqual('');
- expect(getHtCore().find('tr:eq(7) td:eq(0)').html()).toEqual('');
- });
- it('should insert not more rows than maxRows', () => {
- handsontable({
- startRows: 5,
- maxRows: 7
- });
- alter('insert_row', 1);
- alter('insert_row', 1);
- alter('insert_row', 1);
- expect(countRows()).toEqual(7);
- });
- it('when amount parameter is used, should not insert more rows than allowed by maxRows', () => {
- handsontable({
- data: [
- ['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3'],
- ['c1', 'c2', 'c3'],
- ['d1', 'd2', 'd3'],
- ['e1', 'e2', 'e3']
- ],
- maxRows: 10
- });
- alter('insert_row', 1, 10);
- expect(countRows()).toEqual(10);
- expect(spec().$container.find('tr:eq(6) td:eq(0)').html()).toEqual('b1');
- });
- it('should not add more source rows than defined in maxRows when trimming rows using the modifyRow hook', () => {
- const hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(10, 4),
- modifyRow(row) {
- return [8, 9].indexOf(row) > -1 ? null : row;
- },
- maxRows: 10
- });
- expect(hot.countRows()).toEqual(8);
- hot.populateFromArray(7, 0, [['a'], ['b'], ['c']]);
- expect(hot.countSourceRows()).toEqual(10);
- expect(hot.getDataAtCell(7, 0)).toEqual('a');
- });
- it('should fire callback on create row', () => {
- let outputBefore;
- let outputAfter;
- handsontable({
- minRows: 5,
- data: arrayOfNestedObjects(),
- columns: [
- { data: 'id' },
- { data: 'name.first' }
- ],
- beforeCreateRow(index, amount, source) {
- outputBefore = [index, amount, source];
- },
- afterCreateRow(index, amount, source) {
- outputAfter = [index, amount, source];
- },
- });
- alter('insert_row', 3, 1, 'customSource');
- expect(outputBefore).toEqual([3, 1, 'customSource']);
- expect(outputAfter).toEqual([3, 1, 'customSource']);
- });
- it('should keep the single-cell selection in the same position as before inserting the row', () => {
- handsontable({
- minRows: 5,
- data: [
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
- ]
- });
- selectCell(2, 2);
- alter('insert_row', 2);
- const selected = getSelected();
- expect(selected[0][0]).toBe(3);
- expect(selected[0][2]).toBe(3);
- expect(selected.length).toBe(1);
- });
- it('should shift the cell meta according to the new row layout', () => {
- handsontable({
- startCols: 4,
- startRows: 3
- });
- setCellMeta(2, 1, 'className', 'test');
- alter('insert_row', 1, 1);
- expect(getCellMeta(3, 1).className).toEqual('test');
- });
- it('should shift the cell meta according to the new rows (>1) layout', () => {
- handsontable({
- startCols: 4,
- startRows: 3
- });
- setCellMeta(2, 1, 'className', 'test');
- alter('insert_row', 0, 3);
- expect(getCellMeta(5, 1).className).toEqual('test');
- });
- });
- describe('insert column', () => {
- it('should insert column at given index', () => {
- handsontable({
- data: [
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
- ]
- });
- alter('insert_col', 1);
- expect(countCols()).toEqual(9);
- expect(spec().$container.find('tr:eq(1) td:eq(2)').html()).toEqual('b');
- });
- it('should insert column at the end if index is not given', () => {
- handsontable({
- data: [
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
- ]
- });
- alter('insert_col');
- expect(countCols()).toEqual(9);
- expect(spec().$container.find('tr:eq(1) td:eq(7)').html()).toEqual('h');
- });
- it('should insert the amount of columns at given index', () => {
- handsontable({
- data: [
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
- ]
- });
- alter('insert_col', 1, 3);
- expect(countCols()).toEqual(11);
- expect(spec().$container.find('tr:eq(1) td:eq(4)').html()).toEqual('b');
- });
- it('should insert the amount of columns at the end if index is not given', () => {
- handsontable({
- data: [
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
- ]
- });
- alter('insert_col', null, 3);
- expect(countCols()).toEqual(11);
- expect(spec().$container.find('tr:eq(1) td:eq(7)').html()).toEqual('h');
- expect(spec().$container.find('tr:eq(1) td:eq(8)').html()).toEqual('');
- expect(spec().$container.find('tr:eq(1) td:eq(9)').html()).toEqual('');
- expect(spec().$container.find('tr:eq(1) td:eq(10)').html()).toEqual('');
- });
- it('should insert not more cols than maxCols', () => {
- handsontable({
- startCols: 5,
- maxCols: 7
- });
- alter('insert_col', 1);
- alter('insert_col', 1);
- alter('insert_col', 1);
- expect(countCols()).toEqual(7);
- });
- it('should not insert more columns than allowed by maxCols, when amount parameter is used', () => {
- handsontable({
- data: [
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
- ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
- ],
- maxCols: 10
- });
- alter('insert_col', 1, 10);
- expect(countCols()).toEqual(10);
- expect(spec().$container.find('tr:eq(1) td:eq(1)').html()).toEqual('');
- expect(spec().$container.find('tr:eq(1) td:eq(2)').html()).toEqual('');
- expect(spec().$container.find('tr:eq(1) td:eq(3)').html()).toEqual('b');
- });
- it('should fire callback on create col', () => {
- let outputBefore;
- let outputAfter;
- handsontable({
- minRows: 5,
- data: arrayOfArrays(),
- beforeCreateCol(index, amount, source) {
- outputBefore = [index, amount, source];
- },
- afterCreateCol(index, amount, source) {
- outputAfter = [index, amount, source];
- },
- });
- alter('insert_col', 2, 1, 'customSource');
- expect(outputBefore).toEqual([2, 1, 'customSource']);
- expect(outputAfter).toEqual([2, 1, 'customSource']);
- });
- it('should not create column header together with the column, if headers were NOT specified explicitly', () => {
- handsontable({
- startCols: 3,
- startRows: 2,
- colHeaders: true
- });
- expect(getColHeader()).toEqual(['A', 'B', 'C']);
- expect(countCols()).toEqual(3);
- alter('insert_col', 1);
- expect(countCols()).toEqual(4);
- expect(getColHeader()).toEqual(['A', 'B', 'C', 'D']);
- });
- it('should create column header together with the column, if headers were specified explicitly', () => {
- handsontable({
- startCols: 3,
- startRows: 2,
- colHeaders: ['Header0', 'Header1', 'Header2']
- });
- expect(getColHeader()).toEqual(['Header0', 'Header1', 'Header2']);
- expect(countCols()).toEqual(3);
- alter('insert_col', 1);
- expect(countCols()).toEqual(4);
- expect(getColHeader()).toEqual(['Header0', 'B', 'Header1', 'Header2']);
- });
- it('should stretch the table after adding another column (if stretching is set to \'all\')', () => {
- spec().$container.css({
- width: 500,
- });
- const hot = handsontable({
- startCols: 5,
- startRows: 10,
- stretchH: 'all'
- });
- expect(Handsontable.dom.outerWidth(hot.view.TBODY)).toEqual(500);
- alter('insert_col', null, 1);
- expect(Handsontable.dom.outerWidth(hot.view.TBODY)).toEqual(500);
- alter('insert_col', null, 1);
- expect(Handsontable.dom.outerWidth(hot.view.TBODY)).toEqual(500);
- });
- it('should shift the cell meta according to the new column layout', () => {
- handsontable({
- startCols: 4,
- startRows: 3
- });
- setCellMeta(1, 2, 'className', 'test');
- alter('insert_col', 1, 1);
- expect(getCellMeta(1, 3).className).toEqual('test');
- });
- it('should shift the cell meta according to the new columns (>1) layout', () => {
- handsontable({
- startCols: 4,
- startRows: 3
- });
- setCellMeta(1, 2, 'className', 'test');
- alter('insert_col', 0, 3);
- expect(getCellMeta(1, 5).className).toEqual('test');
- });
- });
- });
|