| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714 |
- describe('NumericEditor', () => {
- const id = 'testContainer';
- beforeEach(function() {
- this.$container = $(`<div id="${id}"></div>`).appendTo('body');
- });
- afterEach(function() {
- if (this.$container) {
- destroy();
- this.$container.remove();
- }
- });
- const arrayOfObjects = function() {
- return [
- { id: 1, name: 'Ted', lastName: 'Right' },
- { id: 2, name: 'Frank', lastName: 'Honest' },
- { id: 3, name: 'Joan', lastName: 'Well' },
- { id: 4, name: 'Sid', lastName: 'Strong' },
- { id: 5, name: 'Jane', lastName: 'Neat' },
- { id: 6, name: 'Chuck', lastName: 'Jackson' },
- { id: 7, name: 'Meg', lastName: 'Jansen' },
- { id: 8, name: 'Rob', lastName: 'Norris' },
- { id: 9, name: 'Sean', lastName: 'O\'Hara' },
- { id: 10, name: 'Eve', lastName: 'Branson' }
- ];
- };
- it('should convert "integer like" input value to number (object data source)', async() => {
- handsontable({
- data: arrayOfObjects(),
- columns: [
- { data: 'id', type: 'numeric' },
- { data: 'name' },
- { data: 'lastName' }
- ]
- });
- selectCell(2, 0);
- keyDown('enter');
- document.activeElement.value = '999';
- destroyEditor();
- await sleep(100);
- expect(typeof getDataAtCell(2, 0)).toEqual('number');
- expect(getDataAtCell(2, 0)).toEqual(999);
- });
- it('should not convert formatted "float like" input value to number (object data source) #4706', async() => {
- handsontable({
- data: arrayOfObjects(),
- columns: [
- { data: 'id' },
- { data: 'price_eur', type: 'numeric' },
- { data: 'price_pln', type: 'numeric', numericFormat: { pattern: '$0,0.00', culture: 'en-US' } },
- { data: 'price_aud', type: 'numeric', numericFormat: { pattern: '$0,0.00', culture: 'de-DE' } }
- ]
- });
- selectCell(0, 1);
- keyDown('enter');
- document.activeElement.value = '100.000,0';
- destroyEditor();
- await sleep(100);
- selectCell(1, 1);
- keyDown('enter');
- document.activeElement.value = '200,000.5';
- destroyEditor();
- await sleep(100);
- selectCell(0, 2);
- keyDown('enter');
- document.activeElement.value = '300,000.5';
- destroyEditor();
- await sleep(100);
- selectCell(1, 2);
- keyDown('enter');
- document.activeElement.value = '300.000,5';
- destroyEditor();
- await sleep(100);
- selectCell(0, 3);
- keyDown('enter');
- document.activeElement.value = '400.000,5';
- destroyEditor();
- await sleep(100);
- selectCell(1, 3);
- keyDown('enter');
- document.activeElement.value = '400,000.5';
- destroyEditor();
- await sleep(100);
- expect(getDataAtCell(0, 1)).toEqual('100.000,0');
- expect(getDataAtCell(1, 1)).toEqual('200,000.5');
- expect(getDataAtCell(0, 2)).toEqual('300,000.5');
- expect(getDataAtCell(1, 2)).toEqual('300.000,5');
- expect(getDataAtCell(0, 3)).toEqual('400.000,5');
- expect(getDataAtCell(1, 3)).toEqual('400,000.5');
- });
- it('should convert "float like" input value with dot as determiner to number (object data source)', async() => {
- handsontable({
- data: arrayOfObjects(),
- columns: [
- { data: 'id', type: 'numeric' },
- { data: 'price' },
- { data: 'lastName' }
- ]
- });
- selectCell(2, 0);
- keyDown('enter');
- document.activeElement.value = '77.70';
- destroyEditor();
- await sleep(100);
- expect(typeof getDataAtCell(2, 0)).toEqual('number');
- expect(getDataAtCell(2, 0)).toEqual(77.7);
- });
- it('should convert "float like" input value with comma as determiner to number (object data source)', async() => {
- handsontable({
- data: arrayOfObjects(),
- columns: [
- { data: 'id', type: 'numeric' },
- { data: 'name' },
- { data: 'lastName' }
- ]
- });
- selectCell(2, 0);
- keyDown('enter');
- document.activeElement.value = '77,70';
- destroyEditor();
- await sleep(100);
- expect(typeof getDataAtCell(2, 0)).toEqual('number');
- expect(getDataAtCell(2, 0)).toEqual(77.7);
- });
- it('should convert "float like" input without leading zero to a float', async() => {
- handsontable({
- data: arrayOfObjects(),
- columns: [
- { data: 'id', type: 'numeric' },
- { data: 'name' },
- { data: 'lastName' }
- ]
- });
- selectCell(2, 0);
- keyDown('enter');
- document.activeElement.value = '.74';
- destroyEditor();
- await sleep(100);
- expect(getDataAtCell(2, 0)).toEqual(0.74);
- });
- it('should apply changes to editor after validation', async() => {
- handsontable({
- data: arrayOfObjects(),
- columns: [
- { data: 'id', type: 'numeric' },
- ]
- });
- selectCell(0, 0);
- keyDown('delete');
- await sleep(100);
- expect(getActiveEditor().originalValue).toEqual('');
- });
- it('should not validate string input data containing numbers ', async() => {
- handsontable({
- data: arrayOfObjects(),
- columns: [
- { data: 'id', type: 'numeric' },
- { data: 'price', type: 'numeric', numericFormat: { pattern: '$0,0.00', culture: 'de-DE' } },
- { data: 'lastName' }
- ]
- });
- // Column with default formatting
- selectCell(0, 0);
- keyDown('enter');
- document.activeElement.value = '12aaa34';
- destroyEditor();
- await sleep(100);
- selectCell(1, 0);
- keyDown('enter');
- document.activeElement.value = 'aaa34';
- destroyEditor();
- await sleep(100);
- selectCell(2, 0);
- keyDown('enter');
- document.activeElement.value = '12aaa';
- destroyEditor();
- // Column with specified formatting
- await sleep(100);
- selectCell(0, 1);
- keyDown('enter');
- document.activeElement.value = '12aaa34';
- destroyEditor();
- await sleep(100);
- selectCell(1, 1);
- keyDown('enter');
- document.activeElement.value = 'aaa34';
- destroyEditor();
- await sleep(100);
- selectCell(2, 1);
- keyDown('enter');
- document.activeElement.value = '12aaa';
- destroyEditor();
- await sleep(100);
- expect($(getCell(0, 0)).hasClass('htInvalid')).toBe(true);
- expect(getDataAtCell(0, 0)).toEqual('12aaa34');
- expect($(getCell(1, 0)).hasClass('htInvalid')).toBe(true);
- expect(getDataAtCell(1, 0)).toEqual('aaa34');
- expect($(getCell(2, 0)).hasClass('htInvalid')).toBe(true);
- expect(getDataAtCell(2, 0)).toEqual('12aaa');
- expect($(getCell(0, 1)).hasClass('htInvalid')).toBe(true);
- expect(getDataAtCell(0, 1)).toEqual('12aaa34');
- expect($(getCell(1, 1)).hasClass('htInvalid')).toBe(true);
- expect(getDataAtCell(1, 1)).toEqual('aaa34');
- expect($(getCell(2, 1)).hasClass('htInvalid')).toBe(true);
- expect(getDataAtCell(2, 1)).toEqual('12aaa');
- });
- it('should display a string in a format \'$X,XXX.XX\' when using language=en, appropriate format in column settings and \'XXXX.XX\' as ' +
- 'an input string', async() => {
- handsontable({
- data: arrayOfObjects(),
- columns: [
- { data: 'id', type: 'numeric', numericFormat: { pattern: '$0,0.00', culture: 'en-US' } },
- { data: 'name' },
- { data: 'lastName' }
- ]
- });
- selectCell(2, 0);
- keyDown('enter');
- document.activeElement.value = '2456.22';
- destroyEditor();
- await sleep(100);
- expect(getCell(2, 0).innerHTML).toEqual('$2,456.22');
- });
- it('should display a string in a format \'X.XXX,XX €\' when using language=de, appropriate format in column settings and \'XXXX,XX\' as an ' +
- 'input string (that comes from manual input)', async() => {
- handsontable({
- data: arrayOfObjects(),
- columns: [
- { data: 'id', type: 'numeric', numericFormat: { pattern: '0,0.00 $', culture: 'de-DE' } },
- { data: 'name' },
- { data: 'lastName' }
- ]
- });
- selectCell(2, 0);
- keyDown('enter');
- document.activeElement.value = '2456,22';
- destroyEditor();
- await sleep(100);
- expect(getCell(2, 0).innerHTML).toEqual('2.456,22 €');
- });
- it('should display a string in a format \'X.XXX,XX €\' when using language=de, appropriate format in column settings and \'XXXX.XX\' as an ' +
- 'input string (that comes from paste)', async() => {
- const onAfterValidate = jasmine.createSpy('onAfterValidate');
- handsontable({
- data: arrayOfObjects(),
- columns: [
- { data: 'id', type: 'numeric', numericFormat: { pattern: '0,0.00 $', culture: 'de-DE' } },
- { data: 'name' },
- { data: 'lastName' }
- ],
- afterValidate: onAfterValidate
- });
- selectCell(2, 0);
- keyDown('enter');
- document.activeElement.value = '2456.22';
- destroyEditor();
- await sleep(100);
- expect(getCell(2, 0).innerHTML).toEqual('2.456,22 €');
- });
- it('should display a string in a format \'X XXX,XX €\' when using language=de, appropriate format in column settings and \'XXXX,XX\' as an ' +
- 'input string and ignore not needed zeros at the end', async() => {
- handsontable({
- data: [
- { id: 1, name: 'Ted', lastName: 'Right', money: 0 },
- { id: 2, name: 'Frank', lastName: 'Honest', money: 0 },
- { id: 3, name: 'Joan', lastName: 'Well', money: 0 },
- { id: 4, name: 'Sid', lastName: 'Strong', money: 0 },
- { id: 5, name: 'Jane', lastName: 'Neat', money: 0 },
- { id: 6, name: 'Chuck', lastName: 'Jackson', money: 0 },
- { id: 7, name: 'Meg', lastName: 'Jansen', money: 0 },
- { id: 8, name: 'Rob', lastName: 'Norris', money: 0 },
- { id: 9, name: 'Sean', lastName: 'O\'Hara', money: 0 },
- { id: 10, name: 'Eve', lastName: 'Branson', money: 0 }
- ],
- columns: [
- { data: 'id', type: 'numeric', numericFormat: { pattern: '0,0.00 $', culture: 'de-DE' } },
- { data: 'name' },
- { data: 'lastName' },
- { data: 'money', type: 'numeric', numericFormat: { pattern: '$0,0.00', culture: 'en-US' } }
- ]
- });
- selectCell(2, 0);
- keyDown('enter');
- document.activeElement.value = '2456,220';
- destroyEditor();
- await sleep(100);
- expect(getCell(2, 0).innerHTML).toEqual('2.456,22 €');
- selectCell(2, 3);
- keyDown('enter');
- document.activeElement.value = '2456.220';
- destroyEditor();
- await sleep(100);
- expect(getCell(2, 3).innerHTML).toEqual('$2,456.22');
- });
- it('should display values as "float like" string with dot as determiner after pressing enter ' +
- 'and not change value after closing editor', async() => {
- handsontable({
- data: [
- { id: 1, price_eur: 222.5, price_pln: 1222.6, price_aud: 1333.5 }
- ],
- columns: [
- { data: 'id', type: 'numeric' },
- { data: 'price_eur', type: 'numeric' },
- { data: 'price_pln', type: 'numeric', numericFormat: { pattern: '$0,0.00', culture: 'en-US' } },
- { data: 'price_aud', type: 'numeric', numericFormat: { pattern: '$0,0.00', culture: 'de-DE' } }
- ]
- });
- selectCell(0, 1);
- keyDown('enter');
- await sleep(100);
- expect(document.activeElement.value).toEqual('222.5');
- // closing editor
- keyDown('enter');
- await sleep(100);
- expect(getDataAtCell(0, 1)).toEqual(222.5);
- selectCell(0, 2);
- keyDown('enter');
- await sleep(100);
- expect(document.activeElement.value).toEqual('1222.6');
- // closing editor
- keyDown('enter');
- await sleep(100);
- expect(getDataAtCell(0, 2)).toEqual(1222.6);
- selectCell(0, 3);
- keyDown('enter');
- await sleep(100);
- expect(document.activeElement.value).toEqual('1333.5');
- // closing editor
- keyDown('enter');
- await sleep(100);
- expect(getDataAtCell(0, 3)).toEqual(1333.5);
- });
- it('should display values as "float like" string with dot as determiner after double click ' +
- 'and not change value after closing editor', async() => {
- handsontable({
- data: [
- { id: 1, price_eur: 222.5, price_pln: 1222.6, price_aud: 1333.5 }
- ],
- columns: [
- { data: 'id', type: 'numeric' },
- { data: 'price_eur', type: 'numeric' },
- { data: 'price_pln', type: 'numeric', numericFormat: { pattern: '$0,0.00', culture: 'en-US' } },
- { data: 'price_aud', type: 'numeric', numericFormat: { pattern: '$0,0.00', culture: 'de-DE' } }
- ]
- });
- mouseDoubleClick(getCell(0, 1));
- await sleep(100);
- expect(document.activeElement.value).toEqual('222.5');
- // closing editor
- keyDown('enter');
- await sleep(100);
- expect(getDataAtCell(0, 1)).toEqual(222.5);
- mouseDoubleClick(getCell(0, 2));
- await sleep(100);
- expect(document.activeElement.value).toEqual('1222.6');
- // closing editor
- keyDown('enter');
- await sleep(100);
- expect(getDataAtCell(0, 2)).toEqual(1222.6);
- mouseDoubleClick(getCell(0, 3));
- await sleep(100);
- expect(document.activeElement.value).toEqual('1333.5');
- // closing editor
- keyDown('enter');
- await sleep(100);
- expect(getDataAtCell(0, 3)).toEqual(1333.5);
- });
- it('should mark text as invalid without removing when using `setDataAtCell`', async() => {
- const hot = handsontable({
- data: arrayOfObjects(),
- columns: [
- { data: 'id', type: 'numeric' },
- { data: 'name' },
- { data: 'lastName' }
- ],
- });
- hot.setDataAtCell(0, 0, 'abc');
- await sleep(200);
- expect(hot.getDataAtCell(0, 0)).toEqual('abc');
- expect($(getCell(0, 0)).hasClass('htInvalid')).toBe(true);
- });
- it('should allow custom validator', async() => {
- handsontable({
- data: arrayOfObjects(),
- allowInvalid: false,
- columns: [
- {
- data: 'id',
- type: 'numeric',
- validator(val, cb) {
- cb(parseInt(val, 10) > 100);
- }
- },
- { data: 'name' },
- { data: 'lastName' }
- ]
- });
- selectCell(2, 0);
- keyDown('enter');
- document.activeElement.value = '99';
- destroyEditor();
- await sleep(100);
- expect(getDataAtCell(2, 0)).not.toEqual(99); // should be ignored
- document.activeElement.value = '999';
- destroyEditor();
- await sleep(100);
- expect(getDataAtCell(2, 0)).toEqual(999);
- });
- // Input element can not lose the focus while entering new characters. It breaks IME editor functionality for Asian users.
- it('should not lose the focus on input element while inserting new characters (#839)', async() => {
- let blured = false;
- const listener = () => {
- blured = true;
- };
- const hot = handsontable({
- data: arrayOfObjects(),
- columns: [
- { data: 'id', type: 'numeric', numericFormat: { pattern: '0,0.00', culture: 'en-US' } },
- { data: 'name' },
- { data: 'lastName' }
- ],
- });
- selectCell(0, 0);
- keyDownUp('enter');
- hot.getActiveEditor().TEXTAREA.addEventListener('blur', listener);
- await sleep(200);
- hot.getActiveEditor().TEXTAREA.value = '1';
- keyDownUp('1'.charCodeAt(0));
- hot.getActiveEditor().TEXTAREA.value = '12';
- keyDownUp('2'.charCodeAt(0));
- hot.getActiveEditor().TEXTAREA.value = '123';
- keyDownUp('3'.charCodeAt(0));
- expect(blured).toBeFalsy();
- hot.getActiveEditor().TEXTAREA.removeEventListener('blur', listener);
- });
- it('should not throw error on closing editor when column data is defined as \'length\'', () => {
- hot = handsontable({
- data: [
- { length: 4 },
- { length: 5 },
- ],
- columns: [
- {
- data: 'length', type: 'numeric'
- },
- {},
- {}
- ]
- });
- selectCell(1, 0);
- keyDown('enter');
- document.activeElement.value = '999';
- expect(() => {
- destroyEditor();
- }).not.toThrow();
- });
- describe('Cell corner is showed properly when changing focused cells #3877', () => {
- const isFocusedCellDisplayingCornerTest = function(settings) {
- const moveFromRow = settings.moveFromRow;
- const moveFromCol = settings.moveFromCol;
- const moveToRow = settings.moveToRow;
- const moveToCol = settings.moveToCol;
- const doneFunc = settings.doneFunc;
- const $corner = settings.$container.find('.wtBorder.current.corner');
- selectCell(moveFromRow, moveFromCol);
- keyDown('enter');
- selectCell(moveToRow, moveToCol);
- setTimeout(() => {
- expect($corner.css('display')).toEqual('block');
- doneFunc();
- }, 100);
- };
- it('Moving from numeric editor to text editor', (done) => {
- handsontable({
- data: [
- { id: 1, name: 'Ted', lastName: 'Right', money: 0 }
- ],
- columns: [
- { data: 'id' },
- { data: 'name' },
- { data: 'lastName' },
- { data: 'money', type: 'numeric', numericFormat: { pattern: '$0,0.00', culture: 'en-US' } }
- ]
- });
- isFocusedCellDisplayingCornerTest({
- moveFromRow: 0,
- moveFromCol: 3,
- moveToRow: 0,
- moveToCol: 0,
- $container: spec().$container,
- doneFunc: done
- });
- });
- it('Moving from text editor to numeric editor', (done) => {
- handsontable({
- data: [
- { id: 1, name: 'Ted', lastName: 'Right', money: 0 }
- ],
- columns: [
- { data: 'id' },
- { data: 'name' },
- { data: 'lastName' },
- { data: 'money', type: 'numeric', numericFormat: { pattern: '$0,0.00', culture: 'en-US' } }
- ]
- });
- isFocusedCellDisplayingCornerTest({
- moveFromRow: 0,
- moveFromCol: 1,
- moveToRow: 0,
- moveToCol: 3,
- $container: spec().$container,
- doneFunc: done
- });
- });
- });
- describe('IME support', () => {
- it('should focus editable element after selecting the cell', async() => {
- handsontable({
- type: 'numeric', numericFormat: { pattern: '$0,0.00', culture: 'en-US' }
- });
- selectCell(0, 0, 0, 0, true, false);
- await sleep(10);
- expect(document.activeElement).toBe(getActiveEditor().TEXTAREA);
- });
- });
- });
|