mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-28 22:42:11 +01:00
ControlValueAccessor Fix UI Datepicker + Unit Tests Added
This commit is contained in:
@@ -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');
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user