ControlValueAccessor Fix UI Datepicker + Unit Tests Added

This commit is contained in:
Nino
2025-04-10 14:20:29 +02:00
parent c027791e27
commit 4ac84df25c
2 changed files with 15 additions and 1 deletions

View File

@@ -28,6 +28,8 @@ describe('DatepickerBase', () => {
spectator = createService();
// Override the model's 'set' method to allow spying.
spectator.service.value = { set: jest.fn() } as any;
spectator.service.onChange = jest.fn();
spectator.service.onTouched = jest.fn();
});
describe('setValue', () => {
@@ -36,6 +38,16 @@ describe('DatepickerBase', () => {
expect(spectator.service.value.set).toHaveBeenCalledWith('test string');
});
it('should call onChange with parsed value', () => {
spectator.service.setValue('test string');
expect(spectator.service.onChange).toHaveBeenCalledWith('test string');
});
it('should call onTouched', () => {
spectator.service.setValue('test string');
expect(spectator.service.onTouched).toHaveBeenCalled();
});
it('should log an error when parseValue throws', () => {
jest.spyOn(spectator.service, 'parseValue').mockImplementation(() => {
throw new Error('parse error');

View File

@@ -15,7 +15,7 @@ export abstract class DatepickerBase<TValue> implements ControlValueAccessor {
/**
* Callback function invoked when the control's value changes.
*/
onChange?: (value: DateValue | undefined) => void;
onChange?: (value: TValue | undefined) => void;
/**
* Callback function invoked when the control is touched.
@@ -58,6 +58,8 @@ export abstract class DatepickerBase<TValue> implements ControlValueAccessor {
try {
const parsedValue = this.parseValue(value);
this.value.set(parsedValue);
this.onChange?.(parsedValue);
this.onTouched?.();
} catch (error) {
console.error('Error parsing value:', error);
}