import { DataUnitInMemoryLoader } from '../../../src/dataunit/loader/dataUnitInMemoryLoader.js'; import { DataUnitInMemoryLoaderConfig, RECORD_DATE_FORMAT, } from '../../../src/dataunit/loader/DataUnitInMemoryLoaderConfig.js'; import { FieldDescriptor, UnitMetadata } from '../../../src/dataunit/metadata/UnitMetadata.js'; import { DataType } from '../../../src/dataunit/metadata/DataType.js'; import DateUtils from '../../../src/utils/DateUtils.js'; import DataUnit from '../../../src/dataunit/DataUnit.js'; import { Record } from '../../../src/dataunit/interfaces/dataUnit.js'; describe('DataUnitInMemoryLoader - loadData', () => { const dataUnitMetetadata: UnitMetadata = { name: 'test', label: 'test', fields: [], }; it('should be called load data when auto load is true', async () => { const config: DataUnitInMemoryLoaderConfig = { autoLoad: true, }; const inMemoryLoader = new DataUnitInMemoryLoader(dataUnitMetetadata, [], config); const loadData = jest.spyOn(inMemoryLoader.dataUnit, 'loadData'); await inMemoryLoader.dataUnit.loadMetadata(); expect(loadData).toHaveBeenCalled(); }); it('should be called load data when auto load is undefined', async () => { const config: DataUnitInMemoryLoaderConfig = { autoLoad: undefined, }; const inMemoryLoader = new DataUnitInMemoryLoader(dataUnitMetetadata, [], config); const loadData = jest.spyOn(inMemoryLoader.dataUnit, 'loadData'); await inMemoryLoader.dataUnit.loadMetadata(); expect(loadData).toHaveBeenCalled(); }); it('should not be called load data when auto load is false', async () => { const config: DataUnitInMemoryLoaderConfig = { autoLoad: false, }; const inMemoryLoader = new DataUnitInMemoryLoader(dataUnitMetetadata, [], config); const loadData = jest.spyOn(inMemoryLoader.dataUnit, 'loadData'); await inMemoryLoader.dataUnit.loadMetadata(); expect(loadData).not.toHaveBeenCalled(); }); it('should be called load data when config is undefined', async () => { const inMemoryLoader = new DataUnitInMemoryLoader(dataUnitMetetadata, []); const loadData = jest.spyOn(inMemoryLoader.dataUnit, 'loadData'); await inMemoryLoader.dataUnit.loadMetadata(); expect(loadData).toHaveBeenCalled(); }); }); describe('DataUnitInMemoryLoader - getConvertedValue', () => { let descriptor: FieldDescriptor; beforeEach(() => { descriptor = { name: 'testField', dataType: DataType.TEXT, label: 'mock' }; // Valor padrão para inicialização }); it('should return the original string if descriptor is undefined', () => { const result = DataUnitInMemoryLoader.getConvertedValue(undefined as any, 'testValue'); expect(result).toBe('testValue'); }); it('should return the original string if dataType is TEXT', () => { descriptor.dataType = DataType.TEXT; const result = DataUnitInMemoryLoader.getConvertedValue(descriptor, 'testValue'); expect(result).toBe('testValue'); }); it('should convert to boolean when dataType is BOOLEAN', () => { descriptor.dataType = DataType.BOOLEAN; expect(DataUnitInMemoryLoader.getConvertedValue(descriptor, 'S')).toBe(true); expect(DataUnitInMemoryLoader.getConvertedValue(descriptor, 'N')).toBe(false); }); it('should convert to number when dataType is NUMBER', () => { descriptor.dataType = DataType.NUMBER; expect(DataUnitInMemoryLoader.getConvertedValue(descriptor, '123')).toBe(123); expect(DataUnitInMemoryLoader.getConvertedValue(descriptor, '45.67')).toBe(45.67); }); it('should parse JSON when dataType is OBJECT', () => { descriptor.dataType = DataType.OBJECT; const jsonString = '{"key": "value"}'; const expectedObject = { key: 'value' }; expect(DataUnitInMemoryLoader.getConvertedValue(descriptor, jsonString)).toEqual(expectedObject); }); it('should convert to date when dataType is DATE and format is ISO', () => { descriptor.dataType = DataType.DATE; const isoDate = '2023-01-01T00:00:00Z'; jest.spyOn(DateUtils, 'validateDate').mockImplementation((date) => date); const result = DataUnitInMemoryLoader.getConvertedValue(descriptor, isoDate, RECORD_DATE_FORMAT.ISO); expect(DateUtils.validateDate).toHaveBeenCalledWith(new Date(isoDate), true); expect(result).toEqual(new Date(isoDate)); }); it('should convert to date when dataType is DATE and format is not ISO', () => { descriptor.dataType = DataType.DATE; const dateString = '01/01/2023'; jest.spyOn(DateUtils, 'strToDate').mockImplementation(() => new Date(2023, 0, 1)); const result = DataUnitInMemoryLoader.getConvertedValue(descriptor, dateString, RECORD_DATE_FORMAT.DD_MM_YYYY); expect(DateUtils.strToDate).toHaveBeenCalledWith(dateString, true); expect(result).toEqual(new Date(2023, 0, 1)); }); it('should return the original string if no dataType matches', () => { descriptor.dataType = undefined as any; // Simula um caso inválido const result = DataUnitInMemoryLoader.getConvertedValue(descriptor, 'testValue'); expect(result).toBe('testValue'); }); }); describe('DataUnitInMemoryLoader - removeLoader', () => { let loader: DataUnitInMemoryLoader; const mockedDataUnit = {} as DataUnit; beforeEach(() => { loader = new DataUnitInMemoryLoader(); }); it('should resolve with the provided record IDs', async () => { const mockRecordIds = ['record1', 'record2', 'record3']; const result = await loader.removeLoader(mockedDataUnit, mockRecordIds); expect(result).toEqual(mockRecordIds); }); it('should handle empty record ID array', async () => { const mockRecordIds: string[] = []; const result = await loader.removeLoader(mockedDataUnit, mockRecordIds); expect(result).toEqual([]); }); it('should resolve the promise', async () => { const mockRecordIds = ['record1']; await expect(loader.removeLoader(mockedDataUnit, mockRecordIds)).resolves.toEqual(mockRecordIds); }); }); describe('DataUnitInMemoryLoader - buildInitialRecords (indiretamente via set records)', () => { let loader: DataUnitInMemoryLoader; let mockRecords: Array = [ { id: '1', amount: '100', isActive: 'S', createdAt: '01/01/2023' }, { id: '2', amount: '200.50', isActive: 'N', createdAt: '02/01/2023' }, ] as unknown as Record[]; let mockMetadata = { fields: [ { name: 'id', label: 'id', dataType: DataType.TEXT }, { name: 'amount', label: 'amount', dataType: DataType.NUMBER }, { name: 'isActive', label: 'isActive', dataType: DataType.BOOLEAN }, { name: 'createdAt', label: 'createdAt', dataType: DataType.DATE }, ], }; beforeEach(() => { loader = new DataUnitInMemoryLoader(mockMetadata as any, [], { recordDateFormat: RECORD_DATE_FORMAT.DD_MM_YYYY }); }); it('should build initial records and convert field values based on metadata', () => { loader.records = mockRecords; const initialRecords = loader['_initialRecords']; expect(initialRecords).toHaveLength(2); expect(initialRecords[0]).toEqual({ id: '1', amount: 100, isActive: true, createdAt: expect.any(Date), __record__id__: expect.any(String), }); expect(initialRecords[1]).toEqual({ id: '2', amount: 200.5, isActive: false, createdAt: expect.any(Date), __record__id__: expect.any(String), }); }); it('should generate unique IDs for records without "__record__id__"', () => { loader.records = [{ id: '3', amount: '300' }] as unknown as Record[]; const builtRecords = loader['_initialRecords']; expect(builtRecords).toHaveLength(1); expect(builtRecords[0]).toHaveProperty('__record__id__'); expect(typeof builtRecords[0]['__record__id__']).toBe('string'); }); it('should leave fields untouched if no matching metadata exists', () => { mockRecords[0]['extraField'] = 'unchanged'; loader.records = mockRecords; const builtRecords = loader['_initialRecords']; expect(builtRecords[0]).toHaveProperty('extraField', 'unchanged'); }); it('should skip conversion for non-string field values', () => { mockRecords[0].amount = 500; loader.records = mockRecords; const builtRecords = loader['_initialRecords']; expect(builtRecords[0].amount).toBe(500); }); });