import { SelectionInfo } from "../../src/dataunit/SelectionInfo"; import { Record, SelectionMode } from "../../src/dataunit/interfaces/dataUnit"; import { DataType } from "../../src/dataunit/metadata/DataType"; import { Filter, Sort, SortMode } from "../../src/dataunit/metadata/UnitMetadata"; import { recordsTest } from "./resources/records"; describe('SelectionInfo', () => { let selectionInfo: SelectionInfo; const filter: Filter = { name: 'test_filter', expression: 'test', }; const sort: Sort = { field: 'column_name_1', dataType: DataType.TEXT, mode: SortMode.DESC, }; beforeEach(() => { selectionInfo = new SelectionInfo( recordsTest, SelectionMode.SOME_RECORDS, 5, [filter], [sort] ); }); it('should return false when mode is some records', () => { const response = selectionInfo.isAllRecords(); expect(response).toBeFalsy(); }); it('should return true when mode is all records', () => { selectionInfo = new SelectionInfo( recordsTest, SelectionMode.ALL_RECORDS, 5, [filter], [sort] ); const response = selectionInfo.isAllRecords(); expect(response).toBeTruthy(); }); it('should return false when records is not empty', () => { const response = selectionInfo.isEmpty(); expect(response).toBeFalsy(); }); it('should return true when records is empty', () => { selectionInfo = new SelectionInfo( [], SelectionMode.ALL_RECORDS, 5, [filter], [sort] ); const response = selectionInfo.isAllRecords(); expect(response).toBeTruthy(); }); it('should return number of total when records is empty', () => { selectionInfo = new SelectionInfo( [], SelectionMode.ALL_RECORDS, 10, [filter], [sort] ); const response = selectionInfo.length; expect(response).toBe(10); }); it('should return 0 when records is empty and total is undefined ', () => { selectionInfo = new SelectionInfo( [], SelectionMode.ALL_RECORDS, undefined, [filter], [sort] ); const response = selectionInfo.length; expect(response).toBe(0); }); it('should return number of length of records when is not empty', () => { const response = selectionInfo.length; expect(response).toBe(5); }); it('should return 0 when records is undefined and total is undefined', () => { selectionInfo = new SelectionInfo( undefined as unknown as Array, SelectionMode.SOME_RECORDS, undefined, [filter], [sort] ); const response = selectionInfo.length; expect(response).toBe(0); }); it('should return undefined when records is undefined', () => { selectionInfo = new SelectionInfo( undefined as unknown as Array, SelectionMode.SOME_RECORDS, undefined, [filter], [sort] ); const response = selectionInfo.recordIds; expect(response).toBeUndefined(); }); it('should call getAllRecords', () => { selectionInfo = new SelectionInfo( [], SelectionMode.ALL_RECORDS, undefined, [filter], [sort] ); selectionInfo.getAllRecords = jest.fn(); const response = selectionInfo.records; expect(response).toBeUndefined(); expect(selectionInfo.getAllRecords).toHaveBeenCalled(); }); it('should handle when getAllRecords is undefined', () => { selectionInfo = new SelectionInfo( [], SelectionMode.ALL_RECORDS, undefined, [filter], [sort] ); try { selectionInfo.records; } catch (error) { expect(error).toBeInstanceOf(Error); } }); });