| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987 |
- describe('Core_alter', () => {
- var id = 'testContainer';
- beforeEach(function() {
- this.$container = $(`<div id="${id}"></div>`).appendTo('body');
- });
- afterEach(function() {
- if (this.$container) {
- destroy();
- this.$container.remove();
- }
- });
- var 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'}
- ];
- };
- var 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', () => {
- 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 fire beforeRemoveRow event before removing row', () => {
- var onBeforeRemoveRow = jasmine.createSpy('onBeforeRemoveRow');
- var hot = 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', () => {
- var onBeforeRemoveRow = jasmine.createSpy('onBeforeRemoveRow');
- onBeforeRemoveRow.and.callFake(() => false);
- var hot = 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', function() {
- 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(this.$container.find('tr:eq(0) td:eq(0)').html()).toEqual('a1');
- expect(this.$container.find('tr:eq(1) td:eq(1)').html()).toEqual('c2');
- });
- it('should remove as many rows as given in the amount parameter', function() {
- 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(this.$container.find('tr:eq(0) td:eq(0)').html()).toEqual('a1');
- expect(this.$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', () => {
- var outputBefore;
- var 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', () => {
- var 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', () => {
- var hot = 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', () => {
- var hot = 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', () => {
- it('should remove one column if amount parameter is empty', function() {
- 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(this.$container.find('tr:eq(0) td:eq(0)').html()).toEqual('a');
- expect(this.$container.find('tr:eq(1) td:eq(1)').html()).toEqual('c');
- });
- it('should remove as many columns as given in the amount parameter', function() {
- 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(this.$container.find('tr:eq(0) td:eq(0)').html()).toEqual('a');
- expect(this.$container.find('tr:eq(1) td:eq(1)').html()).toEqual('e');
- });
- it('should not remove more columns that exist', function() {
- 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(this.$container.find('tr:eq(1) td:last').html()).toEqual('f');
- });
- it('should remove one column from end if no parameters are given', function() {
- 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(this.$container.find('tr:eq(1) td:last').html()).toEqual('g');
- });
- it('should remove amount of columns from end if index parameter is not given', function() {
- 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(this.$container.find('tr:eq(1) td:last').html()).toEqual('e');
- });
- it('should fire beforeRemoveCol event before removing col', () => {
- var onBeforeRemoveCol = jasmine.createSpy('onBeforeRemoveCol');
- var hot = 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', () => {
- var onBeforeRemoveCol = jasmine.createSpy('onBeforeRemoveCol');
- onBeforeRemoveCol.and.callFake(() => false);
- var hot = handsontable({
- beforeRemoveCol: onBeforeRemoveCol
- });
- expect(countCols()).toEqual(5);
- alter('remove_col');
- expect(countCols()).toEqual(5);
- });
- it('should fire callback on remove col', () => {
- var outputBefore;
- var 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', function() {
- this.$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', () => {
- var 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', () => {
- var hot = 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', () => {
- var hot = 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', function() {
- 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(this.$container.find('tr:eq(2) td:eq(0)').html()).toEqual('b1');
- });
- 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)', () => {
- var greenRenderer = function(instance, td, row, col, prop, value, cellProperties) {
- Handsontable.renderers.TextRenderer.apply(this, arguments);
- 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', function() {
- 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(this.$container.find('tr:eq(1) td:eq(0)').html()).toEqual('');
- expect(this.$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', function() {
- 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(this.$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', () => {
- var 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', () => {
- var outputBefore;
- var 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);
- var selected = getSelected();
- expect(selected[0]).toEqual(3);
- expect(selected[2]).toEqual(3);
- });
- it('should shift the cell meta according to the new row layout', () => {
- var hot = 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', () => {
- var hot = 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', function() {
- 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(this.$container.find('tr:eq(1) td:eq(2)').html()).toEqual('b');
- });
- it('should insert column at the end if index is not given', function() {
- 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(this.$container.find('tr:eq(1) td:eq(7)').html()).toEqual('h');
- });
- it('should insert the amount of columns at given index', function() {
- 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(this.$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', function() {
- 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(this.$container.find('tr:eq(1) td:eq(7)').html()).toEqual('h');
- expect(this.$container.find('tr:eq(1) td:eq(8)').html()).toEqual('');
- expect(this.$container.find('tr:eq(1) td:eq(9)').html()).toEqual('');
- expect(this.$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', function() {
- 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(this.$container.find('tr:eq(1) td:eq(1)').html()).toEqual('');
- expect(this.$container.find('tr:eq(1) td:eq(2)').html()).toEqual('');
- expect(this.$container.find('tr:eq(1) td:eq(3)').html()).toEqual('b');
- });
- it('should fire callback on create col', () => {
- var outputBefore;
- var 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\')', function() {
- this.$container.css({
- width: 500,
- });
- var 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', () => {
- var hot = 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', () => {
- var hot = handsontable({
- startCols: 4,
- startRows: 3
- });
- setCellMeta(1, 2, 'className', 'test');
- alter('insert_col', 0, 3);
- expect(getCellMeta(1, 5).className).toEqual('test');
- });
- });
- });
|