import { AbstractControl } from '@angular/forms';
import { dateBoundsValidator } from './date-bounds.validator';

describe('dateBoundsValidator', () => {
  const createControl = (value: any): AbstractControl =>
    ({
      value,
    }) as AbstractControl;

  it('should return null if control value is empty', () => {
    const control = createControl(null); // statt ''
    const validator = dateBoundsValidator(
      new Date(2023, 0, 1),
      new Date(2023, 11, 31),
    );
    expect(validator(control)).toBeNull();
  });

  it('should return { invalidDate: true } if the control value is not a valid date', () => {
    const control = createControl(new Date('invalid'));
    const validator = dateBoundsValidator(
      new Date(2023, 0, 1),
      new Date(2023, 11, 31),
    );
    expect(validator(control)).toEqual({ invalidDate: true });
  });

  it('should return { minDate: true } if the date is before the minimum bound', () => {
    const minDate = new Date(2023, 0, 1); // Jan 1, 2023
    const control = createControl(new Date(2022, 11, 31)); // Dec 31, 2022
    const validator = dateBoundsValidator(minDate, undefined);
    expect(validator(control)).toEqual({ minDate: true });
  });

  it('should return { maxDate: true } if the date is after the maximum bound', () => {
    const maxDate = new Date(2023, 11, 31); // Dec 31, 2023
    const control = createControl(new Date(2024, 0, 1)); // Jan 1, 2024
    const validator = dateBoundsValidator(undefined, maxDate);
    expect(validator(control)).toEqual({ maxDate: true });
  });

  it('should return null if the date equals the minimum bound', () => {
    const minDate = new Date(2023, 0, 1);
    const control = createControl(new Date(2023, 0, 1));
    const validator = dateBoundsValidator(minDate, undefined);
    expect(validator(control)).toBeNull();
  });

  it('should return null if the date equals the maximum bound', () => {
    const maxDate = new Date(2023, 11, 31);
    const control = createControl(new Date(2023, 11, 31));
    const validator = dateBoundsValidator(undefined, maxDate);
    expect(validator(control)).toBeNull();
  });

  it('should return null if the date is within the provided bounds', () => {
    const minDate = new Date(2023, 0, 1);
    const maxDate = new Date(2023, 11, 31);
    const control = createControl(new Date(2023, 5, 15)); // June 15, 2023
    const validator = dateBoundsValidator(minDate, maxDate);
    expect(validator(control)).toBeNull();
  });

  it('should return null if no bounds are provided', () => {
    const control = createControl(new Date(2023, 5, 15)); // June 15, 2023
    const validator = dateBoundsValidator();
    expect(validator(control)).toBeNull();
  });
});
