| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620 |
- describe('Core_updateSettings', () => {
- var id = 'testContainer';
- beforeEach(function() {
- this.$container = $(`<div id="${id}"></div>`).appendTo('body');
- });
- afterEach(function() {
- if (this.$container) {
- destroy();
- this.$container.remove();
- }
- });
- it('should inherit cell type', () => {
- handsontable({
- data: [[1, 2]],
- columns: [
- {},
- {type: 'checkbox'},
- ],
- cells(row, col, prop) {
- if (row === 0 && col === 0) {
- return {
- type: 'numeric'
- };
- }
- }
- });
- expect(getCellMeta(0, 0).type).toEqual('numeric');
- expect(getCellMeta(0, 1).type).toEqual('checkbox');
- });
- it('should inherit cell type when columns is a function', () => {
- handsontable({
- data: [[1, 2]],
- columns(column) {
- var colMeta = null;
- if (column === 0) {
- colMeta = {};
- } else if (column === 1) {
- colMeta = {type: 'checkbox'};
- }
- return colMeta;
- },
- cells(row, col, prop) {
- if (row === 0 && col === 0) {
- return {
- type: 'numeric'
- };
- }
- }
- });
- expect(getCellMeta(0, 0).type).toEqual('numeric');
- expect(getCellMeta(0, 1).type).toEqual('checkbox');
- });
- it('should ignore mixed in properties to the cell array option', () => {
- /* eslint-disable no-array-constructor */
- /* eslint-disable no-extend-native */
- Array.prototype.willFail = 'BOOM';
- handsontable({
- data: [[1, true]],
- columns: [
- {type: 'numeric'},
- {type: 'checkbox'}
- ]
- });
- expect(() => {
- updateSettings({cell: new Array()});
- }).not.toThrow();
- });
- it('should ignore mixed in properties to the cell array option when columns is a function', () => {
- /* eslint-disable no-array-constructor */
- /* eslint-disable no-extend-native */
- Array.prototype.willFail = 'BOOM';
- handsontable({
- data: [[1, true]],
- columns: function(column) {
- var colMeta = null;
- if (column === 0) {
- colMeta = {type: 'numeric'};
- } else if (column === 1) {
- colMeta = {type: 'checkbox'};
- }
- return colMeta;
- },
- });
- expect(() => {
- updateSettings({cell: new Array()});
- }).not.toThrow();
- });
- it('should not reset columns types to text', function() {
- handsontable({
- data: [[1, true]],
- columns: [
- {type: 'numeric'},
- {type: 'checkbox'}
- ]
- });
- var td = this.$container.find('td');
- expect(td.eq(0).text()).toEqual('1');
- expect(td.eq(1).text()).toEqual('');
- updateSettings({});
- expect(td.eq(0).text()).toEqual('1');
- expect(td.eq(1).text()).toEqual('');
- });
- it('should not reset columns types to text when columns is a function', function() {
- handsontable({
- data: [[1, true]],
- columns: function(column) {
- var colMeta = null;
- if (column === 0) {
- colMeta = {type: 'numeric'};
- } else if (column === 1) {
- colMeta = {type: 'checkbox'};
- }
- return colMeta;
- }
- });
- var td = this.$container.find('td');
- expect(td.eq(0).text()).toEqual('1');
- expect(td.eq(1).text()).toEqual('');
- updateSettings({});
- expect(td.eq(0).text()).toEqual('1');
- expect(td.eq(1).text()).toEqual('');
- });
- it('should update readOnly global setting', () => {
- handsontable({
- readOnly: true,
- data: [['foo', 'bar']],
- columns: [
- {},
- {},
- ]
- });
- expect(getCellMeta(0, 0).readOnly).toBe(true);
- expect($(getCell(0, 0)).hasClass('htDimmed')).toBe(true);
- expect(getCellMeta(0, 1).readOnly).toBe(true);
- expect($(getCell(0, 1)).hasClass('htDimmed')).toBe(true);
- updateSettings({
- readOnly: false
- });
- expect(getCellMeta(0, 0).readOnly).toBe(false);
- expect($(getCell(0, 0)).hasClass('htDimmed')).toBe(false);
- expect(getCellMeta(0, 1).readOnly).toBe(false);
- expect($(getCell(0, 1)).hasClass('htDimmed')).toBe(false);
- });
- it('should update readOnly global setting when columns is a function', () => {
- handsontable({
- readOnly: true,
- data: [['foo', 'bar']],
- columns(column) {
- var colMeta = {};
- if ([0, 1].indexOf(column) < 0) {
- colMeta = null;
- }
- return colMeta;
- }
- });
- expect(getCellMeta(0, 0).readOnly).toBe(true);
- expect($(getCell(0, 0)).hasClass('htDimmed')).toBe(true);
- expect(getCellMeta(0, 1).readOnly).toBe(true);
- expect($(getCell(0, 1)).hasClass('htDimmed')).toBe(true);
- updateSettings({
- readOnly: false
- });
- expect(getCellMeta(0, 0).readOnly).toBe(false);
- expect($(getCell(0, 0)).hasClass('htDimmed')).toBe(false);
- expect(getCellMeta(0, 1).readOnly).toBe(false);
- expect($(getCell(0, 1)).hasClass('htDimmed')).toBe(false);
- });
- it('should update readOnly columns setting', () => {
- handsontable({
- data: [['foo', true]],
- columns: [
- {type: 'text', readOnly: true},
- {type: 'checkbox'}
- ]
- });
- expect(getCellMeta(0, 0).readOnly).toBe(true);
- expect($(getCell(0, 0)).hasClass('htDimmed')).toBe(true);
- expect(getCellMeta(0, 1).readOnly).toBe(false);
- expect($(getCell(0, 1)).hasClass('htDimmed')).toBe(false);
- updateSettings({
- columns: [
- {type: 'text', readOnly: false},
- {type: 'checkbox'}
- ]
- });
- expect(getCellMeta(0, 0).readOnly).toBe(false);
- expect($(getCell(0, 0)).hasClass('htDimmed')).toBe(false);
- expect(getCellMeta(0, 1).readOnly).toBe(false);
- expect($(getCell(0, 1)).hasClass('htDimmed')).toBe(false);
- });
- it('should update readOnly columns setting when columns is a function', () => {
- handsontable({
- data: [['foo', true]],
- columns(column) {
- var colMeta = null;
- if (column === 0) {
- colMeta = {type: 'text', readOnly: true};
- } else if (column === 1) {
- colMeta = {type: 'checkbox'};
- }
- return colMeta;
- }
- });
- expect(getCellMeta(0, 0).readOnly).toBe(true);
- expect($(getCell(0, 0)).hasClass('htDimmed')).toBe(true);
- expect(getCellMeta(0, 1).readOnly).toBe(false);
- expect($(getCell(0, 1)).hasClass('htDimmed')).toBe(false);
- updateSettings({
- columns(column) {
- var colMeta = null;
- if (column === 0) {
- colMeta = {type: 'text', readOnly: false};
- } else if (column === 1) {
- colMeta = {type: 'checkbox'};
- }
- return colMeta;
- }
- });
- expect(getCellMeta(0, 0).readOnly).toBe(false);
- expect($(getCell(0, 0)).hasClass('htDimmed')).toBe(false);
- expect(getCellMeta(0, 1).readOnly).toBe(false);
- expect($(getCell(0, 1)).hasClass('htDimmed')).toBe(false);
- });
- it('should update readOnly columns setting and override global setting', () => {
- handsontable({
- readOnly: true,
- data: [['foo', true]],
- columns: [
- {type: 'text'},
- {type: 'checkbox'}
- ]
- });
- expect(getCellMeta(0, 0).readOnly).toBe(true);
- expect($(getCell(0, 0)).hasClass('htDimmed')).toBe(true);
- expect(getCellMeta(0, 1).readOnly).toBe(true);
- expect($(getCell(0, 1)).hasClass('htDimmed')).toBe(true);
- updateSettings({
- columns: [
- {type: 'text', readOnly: false},
- {type: 'checkbox'}
- ]
- });
- expect(getCellMeta(0, 0).readOnly).toBe(false);
- expect($(getCell(0, 0)).hasClass('htDimmed')).toBe(false);
- expect(getCellMeta(0, 1).readOnly).toBe(true);
- expect($(getCell(0, 1)).hasClass('htDimmed')).toBe(true);
- });
- it('should update readOnly columns setting and override global setting when columns is a function', () => {
- handsontable({
- readOnly: true,
- data: [['foo', true]],
- columns(column) {
- var colMeta = null;
- if (column === 0) {
- colMeta = {type: 'text'};
- } else if (column === 1) {
- colMeta = {type: 'checkbox'};
- }
- return colMeta;
- }
- });
- expect(getCellMeta(0, 0).readOnly).toBe(true);
- expect($(getCell(0, 0)).hasClass('htDimmed')).toBe(true);
- expect(getCellMeta(0, 1).readOnly).toBe(true);
- expect($(getCell(0, 1)).hasClass('htDimmed')).toBe(true);
- updateSettings({
- columns(column) {
- var colMeta = null;
- if (column === 0) {
- colMeta = {type: 'text', readOnly: false};
- } else if (column === 1) {
- colMeta = {type: 'checkbox'};
- }
- return colMeta;
- }
- });
- expect(getCellMeta(0, 0).readOnly).toBe(false);
- expect($(getCell(0, 0)).hasClass('htDimmed')).toBe(false);
- expect(getCellMeta(0, 1).readOnly).toBe(true);
- expect($(getCell(0, 1)).hasClass('htDimmed')).toBe(true);
- });
- it('should not alter the columns object during init', () => {
- var columns = [
- {
- type: 'text'
- }
- ];
- var columnsCopy = JSON.parse(JSON.stringify(columns));
- handsontable({
- columns
- });
- expect(columns).toEqual(columnsCopy);
- });
- it('should update column type', () => {
- var columns = [
- {
- type: 'text'
- }
- ];
- handsontable({
- columns
- });
- expect(getCellMeta(0, 0).type).toEqual('text');
- expect(getCellRenderer(0, 0)).toBe(Handsontable.renderers.TextRenderer);
- expect(getCellEditor(0, 0)).toBe(Handsontable.editors.TextEditor);
- columns[0].type = 'date';
- updateSettings({
- columns
- });
- expect(getCellMeta(0, 0).type).toEqual('date');
- expect(getCellRenderer(0, 0)).toBe(Handsontable.renderers.AutocompleteRenderer);
- expect(getCellEditor(0, 0)).toEqual(Handsontable.editors.DateEditor);
- });
- it('should update cell type functions, even if new type does not implement all of those functions', () => {
- var columns = [
- {
- type: 'numeric'
- }
- ];
- handsontable({
- columns
- });
- expect(getCellMeta(0, 0).type).toEqual('numeric');
- expect(getCellRenderer(0, 0)).toBe(Handsontable.renderers.NumericRenderer);
- expect(getCellEditor(0, 0)).toBe(Handsontable.editors.NumericEditor);
- expect(getCellValidator(0, 0)).toBe(Handsontable.cellTypes.numeric.validator);
- columns[0].type = 'text';
- updateSettings({
- columns
- });
- expect(getCellMeta(0, 0).type).toEqual('text');
- expect(getCellRenderer(0, 0)).toBe(Handsontable.renderers.TextRenderer);
- expect(getCellEditor(0, 0)).toEqual(Handsontable.editors.TextEditor);
- expect(Handsontable.cellTypes.text.validator).toBeUndefined();
- expect(getCellValidator(0, 0)).toBeUndefined();
- });
- it('should allow updating the table height', function() {
- var hot = handsontable({
- startRows: 22,
- startCols: 5
- });
- var initialHeight = parseInt(this.$container[0].style.height, 10);
- updateSettings({
- height: 300
- });
- expect(parseInt(this.$container[0].style.height, 10)).toEqual(300);
- expect(parseInt(this.$container[0].style.height, 10)).not.toEqual(initialHeight);
- });
- it('should not reset the table height, when the updateSettings config object doesn\'t have any height specified', function() {
- var hot = handsontable({
- startRows: 22,
- startCols: 5,
- height: 300
- });
- var initialHeight = this.$container[0].style.height;
- updateSettings({
- rowHeaders: true
- });
- expect(parseInt(this.$container[0].style.height, 10)).toEqual(parseInt(initialHeight, 10));
- });
- it('should allow resetting the table height', function() {
- var hot = handsontable({
- startRows: 22,
- startCols: 5,
- height: 300
- });
- var initialHeight = this.$container[0].style.height;
- updateSettings({
- height: null
- });
- expect(parseInt(this.$container[0].style.height, 10)).not.toEqual(parseInt(initialHeight, 10));
- });
- it('should allow updating the stretching type', () => {
- var hot = handsontable({
- stretchH: 'last'
- });
- expect(hot.view.wt.getSetting('stretchH')).toEqual('last');
- updateSettings({
- stretchH: 'all'
- });
- expect(hot.view.wt.getSetting('stretchH')).toEqual('all');
- updateSettings({
- stretchH: 'none'
- });
- expect(hot.view.wt.getSetting('stretchH')).toEqual('none');
- updateSettings({
- stretchH: 'last'
- });
- expect(hot.view.wt.getSetting('stretchH')).toEqual('last');
- });
- it('should change colHeader\'s row height if is needed', function() {
- var hot = handsontable({
- colHeaders: true,
- rowHeaders: true
- });
- var rowHeights = [];
- rowHeights.push(this.$container.find('.ht_clone_top_left_corner thead th')[0].clientHeight);
- updateSettings({
- colHeaders: ['A<br/>A']
- });
- rowHeights.push(this.$container.find('.ht_clone_top_left_corner thead th')[0].clientHeight);
- expect(rowHeights[0]).toBeLessThan(rowHeights[1]);
- });
- it('should not overwrite properties (created by columns defined as function) of cells below the viewport by updateSettings #4029', function() {
- var rows = 50;
- var columns = 2;
- handsontable({
- data: Handsontable.helper.createSpreadsheetObjectData(columns, rows),
- columns: function (col) {
- var colProp = {
- data: 'prop' + col,
- readOnly: true
- };
- if (col === 1) {
- colProp.type = 'checkbox';
- }
- return colProp;
- }
- });
- updateSettings({});
- expect(getCellMeta(rows, 0).readOnly).toEqual(true);
- expect(getCellMeta(rows, 1).type).toEqual('checkbox');
- rows = 100;
- updateSettings({data: Handsontable.helper.createSpreadsheetObjectData(columns, rows)});
- expect(getCellMeta(rows, 0).readOnly).toEqual(true);
- expect(getCellMeta(rows, 1).type).toEqual('checkbox');
- updateSettings({
- columns: function (col) {
- var colProp = {
- data: 'prop' + col,
- type: 'numeric'
- };
- return colProp;
- }
- });
- expect(getCellMeta(0, 1).type).toEqual('numeric');
- expect(getCellMeta(0, 1).readOnly).toEqual(false);
- expect(getCellMeta(rows, 1).type).toEqual('numeric');
- expect(getCellMeta(rows, 1).readOnly).toEqual(false);
- });
- it('should not overwrite properties (created by columns defined as array) of cells below the viewport by updateSettings #4029', function() {
- var rows = 50;
- var columns = 2;
- handsontable({
- data: Handsontable.helper.createSpreadsheetObjectData(columns, rows),
- columns: [
- {
- type: 'numeric',
- format: '0,0.00 $'
- },
- {
- type: 'text',
- readOnly: true
- }
- ]
- });
- updateSettings({});
- expect(getCellMeta(rows, 0).type).toEqual('numeric');
- expect(getCellMeta(rows, 1).readOnly).toEqual(true);
- rows = 100;
- updateSettings({data: Handsontable.helper.createSpreadsheetObjectData(columns, rows)});
- expect(getCellMeta(rows, 0).type).toEqual('numeric');
- expect(getCellMeta(rows, 1).readOnly).toEqual(true);
- updateSettings({
- columns: [
- {
- type: 'text',
- readOnly: true
- },
- {
- type: 'numeric',
- format: '0,0.00 $'
- }
- ]
- });
- expect(getCellMeta(0, 0).type).toEqual('text');
- expect(getCellMeta(0, 0).readOnly).toEqual(true);
- expect(getCellMeta(0, 1).type).toEqual('numeric');
- expect(getCellMeta(0, 1).readOnly).toEqual(false);
- expect(getCellMeta(rows, 0).type).toEqual('text');
- expect(getCellMeta(rows, 1).type).toEqual('numeric');
- });
- });
|