import { getFormattedValue } from '../../../src/dataunit/formatting/PrettyFormatter'; import { DataType, FieldDescriptor, UserInterface } from '../../../src'; /** * É preciso importart o utilitário DateUtils para que seus métodos * estáticos sejam executados no ambiente de testes */ import { DateUtils } from '../../../src'; describe('getFormattedValue', () => { it('should return empty string when value is null', () => { const value = null; const descriptor = getFileFieldDescriptor(); expect(getFormattedValue(value, descriptor)).toBe(''); }); it('should return empty string when value is not an array', () => { const value = 'not an array'; const descriptor = getFileFieldDescriptor(); expect(getFormattedValue(value, descriptor)).toBe(value); }); it('should return empty string when value is an empty array', () => { const value: any[] = []; const descriptor = getFileFieldDescriptor(); expect(getFormattedValue(value, descriptor)).toBe('0 arquivos'); }); it('should return file name when value is an array with one file object', () => { const value = [{ name: 'file1.txt' }]; const descriptor = getFileFieldDescriptor(); expect(getFormattedValue(value, descriptor)).toBe('file1.txt'); }); it('should return file count when value is an array with multiple file objects', () => { const value = [{ name: 'file1.txt' }, { name: 'file2.txt' }]; const descriptor = getFileFieldDescriptor(); expect(getFormattedValue(value, descriptor)).toBe('2 arquivos'); }); it('should return empty string for undefined file', () => { const value = undefined; const descriptor = { userInterface: UserInterface.FILE } as FieldDescriptor; expect(getFormattedValue(value, descriptor)).toBe(''); }); it('should handle OBJECT type with value field', () => { const value = { value: 'code', label: 'label' }; const descriptor = { dataType: DataType.OBJECT, userInterface: UserInterface.SEARCH } as FieldDescriptor; expect(getFormattedValue(value, descriptor)).toBe('code - label'); }); it('should return empty string when value is undefined', () => { const value = undefined; const descriptor = getObjectFieldDescriptor(); expect(getFormattedValue(value, descriptor)).toBe(''); }); it('should return value as string when value is not an object', () => { const value = 12345; const descriptor = getObjectFieldDescriptor(); expect(getFormattedValue(value, descriptor)).toBe('12345'); }); it('should return value.toString() when value is an object without value property', () => { const value = { someProperty: 'someValue' }; const descriptor = getObjectFieldDescriptor(); expect(getFormattedValue(value, descriptor)).toBe(value.toString()); }); it('should handle BOOLEAN type', () => { expect(getFormattedValue(true, { dataType: DataType.BOOLEAN } as FieldDescriptor)).toBe('Sim'); expect(getFormattedValue(false, { dataType: DataType.BOOLEAN } as FieldDescriptor)).toBe('Não'); }); it('should format numbers correctly', () => { const value = 1234.567; const descriptor = { dataType: DataType.NUMBER, properties: { precision: 2, prettyPrecision: 2 }, } as FieldDescriptor; expect(getFormattedValue(value, descriptor)).toBe('1.234,57'); }); it('should format dates correctly', () => { const value = new Date('2023-07-01T14:30:00'); const descriptor = { userInterface: UserInterface.DATE } as FieldDescriptor; expect(getFormattedValue(value, descriptor)).toBe('01/07/2023'); }); it('should format times correctly', () => { const value = new Date('2023-07-01T14:30:00'); const descriptor = { userInterface: UserInterface.TIME } as FieldDescriptor; expect(getFormattedValue(value, descriptor)).toBe('14:30'); }); it('should format datetime correctly', () => { const value = new Date('2023-07-01T14:30:00'); const descriptor = { userInterface: UserInterface.DATETIME } as FieldDescriptor; expect(getFormattedValue(value, descriptor)).toBe('01/07/2023 14:30'); }); it('should call toString with correct parameters', () => { const value = 123; const descriptor = { dataType: DataType.NUMBER } as FieldDescriptor; expect(getFormattedValue(value, descriptor)).toBe('123,00'); }); it('should handle undefined descriptor gracefully', () => { const value = 123; expect(getFormattedValue(value)).toBe('123'); }); it('should format value correctly with OPTIONSELECTOR and matching option', () => { const value = 'option1'; const descriptor = buildOptionSelectorFieldDescriptor(); expect(getFormattedValue(value, descriptor)).toBe('option1'); }); it('should return value with OPTIONSELECTOR and no matching option', () => { const value = 'option2'; const descriptor = buildOptionSelectorFieldDescriptor(); expect(getFormattedValue(value, descriptor)).toBe('option2'); }); it('should format value correctly with OPTIONSELECTOR and value as object', () => { const value = { value: 'option1' }; const descriptor = buildOptionSelectorFieldDescriptor(); expect(getFormattedValue(value, descriptor)).toBe('option1'); }); it('should return empty string with OPTIONSELECTOR and value as null', () => { const value = null; const descriptor = buildOptionSelectorFieldDescriptor(); expect(getFormattedValue(value, descriptor)).toBe(''); }); it('should format value correctly with OPTIONSELECTOR and options as JSON string', () => { const value = 'option1'; const descriptor: FieldDescriptor = { userInterface: UserInterface.OPTIONSELECTOR, properties: { options: JSON.stringify([{ label: 'Option 1', value: 'option1' }]) }, } as FieldDescriptor; expect(getFormattedValue(value, descriptor)).toBe('option1'); }); it('should return value with OPTIONSELECTOR and options as empty array', () => { const value = 'option1'; const descriptor: FieldDescriptor = { userInterface: UserInterface.OPTIONSELECTOR, properties: { options: [] }, } as FieldDescriptor; expect(getFormattedValue(value, descriptor)).toBe('option1'); }); it('should return label when OPTIONSELECTOR has switchLabelsAndValues and value matches option value', () => { const value = 'option1'; const descriptor: FieldDescriptor = { userInterface: UserInterface.OPTIONSELECTOR, properties: { options: [{ label: 'Option 1', value: 'option1' }], switchLabelsAndValues: true, }, } as FieldDescriptor; expect(getFormattedValue(value, descriptor)).toBe('Option 1'); }); it('should format masked values correctly', () => { const value = '12345678901'; const descriptor = { properties: { mask: 'cpf' } } as FieldDescriptor; expect(getFormattedValue(value, descriptor)).toBe('123.456.789-01'); }); }); function buildOptionSelectorFieldDescriptor(): FieldDescriptor { return { userInterface: UserInterface.OPTIONSELECTOR, properties: { options: [{ label: 'Option 1', value: 'option1' }] }, } as FieldDescriptor; } function getFileFieldDescriptor(): FieldDescriptor{ return { userInterface: UserInterface.FILE } as FieldDescriptor; } function getObjectFieldDescriptor(): FieldDescriptor{ return { dataType: DataType.OBJECT } as FieldDescriptor; }