mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2026-04-16 22:24:49 +00:00
AAE-33828 Fix for the runtime errors on data-table should be handled (#11639)
* AAE-33828 fix for the runtime errors on datatable should be handled in runtime * AAE-33828 removing the unwanted try catches * AAE-33828 adding units to test the number value is a valid value
This commit is contained in:
committed by
GitHub
parent
93fc7c166c
commit
f8fa996b04
@@ -15,7 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ChangeDetectionStrategy, Component, ViewEncapsulation, Input, DEFAULT_CURRENCY_CODE, inject } from '@angular/core';
|
||||
import { ChangeDetectionStrategy, Component, ViewEncapsulation, Input, DEFAULT_CURRENCY_CODE, inject, computed } from '@angular/core';
|
||||
import { DataTableCellComponent } from '../datatable-cell/datatable-cell.component';
|
||||
import { CurrencyConfig } from '../../data/data-column.model';
|
||||
import { CurrencyPipe } from '@angular/common';
|
||||
@@ -41,5 +41,17 @@ export class AmountCellComponent extends DataTableCellComponent {
|
||||
locale: undefined
|
||||
};
|
||||
|
||||
readonly amountValue = toSignal(this.value$);
|
||||
private readonly rawAmountValue = toSignal(this.value$);
|
||||
|
||||
readonly amountValue = computed<number | null>(() => {
|
||||
const value = this.rawAmountValue();
|
||||
|
||||
if (value == null || value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
const numericValue = Number(value);
|
||||
|
||||
return Number.isFinite(numericValue) ? numericValue : null;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -63,7 +63,6 @@ export class DateCellComponent extends DataTableCellComponent implements OnInit
|
||||
if (!date) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (currentConfig.format === 'timeAgo') {
|
||||
return this.timeAgoPipe.transform(date, currentConfig.locale) || '';
|
||||
}
|
||||
|
||||
@@ -60,6 +60,63 @@ describe('NumberCellComponent', () => {
|
||||
it('should render decimal value with custom digitsInfo', () => {
|
||||
renderAndCheckNumberValue({ digitsInfo: '1.2-2' }, 123.456789, '123.46');
|
||||
});
|
||||
|
||||
describe('numberValue validation', () => {
|
||||
it('should return valid number for integer input', () => {
|
||||
component.value$.next(42);
|
||||
fixture.detectChanges();
|
||||
expect(component.numberValue()).toBe(42);
|
||||
});
|
||||
|
||||
it('should return valid number for float input', () => {
|
||||
component.value$.next(3.14159);
|
||||
fixture.detectChanges();
|
||||
expect(component.numberValue()).toBe(3.14159);
|
||||
});
|
||||
|
||||
it('should return valid number for negative number', () => {
|
||||
component.value$.next(-123.45);
|
||||
fixture.detectChanges();
|
||||
expect(component.numberValue()).toBe(-123.45);
|
||||
});
|
||||
|
||||
it('should return valid number for zero', () => {
|
||||
component.value$.next(0);
|
||||
fixture.detectChanges();
|
||||
expect(component.numberValue()).toBe(0);
|
||||
});
|
||||
|
||||
it('should return valid number for numeric string', () => {
|
||||
component.value$.next('456.78');
|
||||
fixture.detectChanges();
|
||||
expect(component.numberValue()).toBe(456.78);
|
||||
});
|
||||
|
||||
it('should return null for boolean true', () => {
|
||||
(component.value$ as { next: (v: unknown) => void }).next(true);
|
||||
fixture.detectChanges();
|
||||
expect(component.numberValue()).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null for NaN', () => {
|
||||
component.value$.next(NaN);
|
||||
fixture.detectChanges();
|
||||
expect(component.numberValue()).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null for non-numeric string', () => {
|
||||
component.value$.next('not a number');
|
||||
fixture.detectChanges();
|
||||
expect(component.numberValue()).toBeNull();
|
||||
});
|
||||
|
||||
it('should not render span when value is invalid', () => {
|
||||
component.value$.next(null);
|
||||
fixture.detectChanges();
|
||||
const displayedNumber = testingUtils.getByCSS('span');
|
||||
expect(displayedNumber).toBeFalsy();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('NumberCellComponent locale', () => {
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ChangeDetectionStrategy, Component, ViewEncapsulation, Input } from '@angular/core';
|
||||
import { ChangeDetectionStrategy, Component, ViewEncapsulation, Input, computed } from '@angular/core';
|
||||
import { DataTableCellComponent } from '../datatable-cell/datatable-cell.component';
|
||||
import { DecimalConfig } from '../../data/data-column.model';
|
||||
import { CommonModule } from '@angular/common';
|
||||
@@ -38,5 +38,17 @@ export class NumberCellComponent extends DataTableCellComponent {
|
||||
locale: undefined
|
||||
};
|
||||
|
||||
readonly numberValue = toSignal(this.value$);
|
||||
private readonly rawNumberValue = toSignal(this.value$);
|
||||
|
||||
readonly numberValue = computed<number | null>(() => {
|
||||
const value = this.rawNumberValue();
|
||||
|
||||
if (value == null || value === '' || typeof value === 'boolean') {
|
||||
return null;
|
||||
}
|
||||
|
||||
const numericValue = Number(value);
|
||||
|
||||
return Number.isFinite(numericValue) ? numericValue : null;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user