import { TimeFormatter } from '../../src/utils/TimeFormatter'; import { validateTimeTestCases } from '../testCases/TimeFormatterTestUtils'; describe('TimeFormatter', () => { describe('prepareValue', () => { test('Case: input string unformated with showSeconds', () => { let value = TimeFormatter.prepareValue('1110', false); expect(value).toEqual('11:10'); }); test('Case: input string unformated without showSeconds', () => { let value = TimeFormatter.prepareValue('1110', true); expect(value).toEqual('00:11:10'); }); test('should format valid time without seconds', () => { expect(TimeFormatter.prepareValue('1012', false)).toBe('10:12'); expect(TimeFormatter.prepareValue('12', false)).toBe('00:12'); expect(TimeFormatter.prepareValue('59', false)).toBe('00:59'); }); test('should format valid time with seconds', () => { expect(TimeFormatter.prepareValue('101200', true)).toBe('10:12:00'); expect(TimeFormatter.prepareValue('12', true)).toBe('00:00:12'); expect(TimeFormatter.prepareValue('1', true)).toBe('00:00:01'); }); test('should pad input with leading zeros for proper format', () => { expect(TimeFormatter.prepareValue('1', false)).toBe('00:01'); expect(TimeFormatter.prepareValue('123', false)).toBe('01:23'); expect(TimeFormatter.prepareValue('12345', true)).toBe('01:23:45'); }); test('should return an empty string for invalid input', () => { expect(TimeFormatter.prepareValue('', false)).toBe(''); expect(TimeFormatter.prepareValue(null as any, false)).toBe(''); expect(TimeFormatter.prepareValue(undefined as any, false)).toBe(''); }); test('should throw error when recieveng string', () => { expect(() => { TimeFormatter.prepareValue("abcd", false) }).toThrow(Error); }) test('should handle edge cases correctly', () => { expect(TimeFormatter.prepareValue('0000', false)).toBe('00:00'); expect(TimeFormatter.prepareValue('2359', false)).toBe('23:59'); expect(TimeFormatter.prepareValue('000000', true)).toBe('00:00:00'); expect(TimeFormatter.prepareValue('235959', true)).toBe('23:59:59'); }); test('should format partially filled times correctly', () => { expect(TimeFormatter.prepareValue('1', true)).toBe('00:00:01'); expect(TimeFormatter.prepareValue('123', true)).toBe('00:01:23'); expect(TimeFormatter.prepareValue('12', true)).toBe('00:00:12'); }); }); describe('validateTime', () => { validateTimeTestCases.forEach(e => { it(`should be ${e.expected} when [ value: ${e.value}, showSeconds: ${e.showSeconds}`, () => { expect(TimeFormatter.validateTime(e.value, e.showSeconds)).toBe(e.expected); }); }); }); });