1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Angular unit test for Angular Material DatePicker failing, can't understand why

Discussão em 'Angular' iniciado por MB34, Junho 6, 2025.

  1. MB34

    MB34 Guest

    I've got these 2 Unit tests for the Angular Material DatePicker in Angular. I can't understand why the result I get is 1 vs. expected 31...

    describe('startTimeChange', () => {
    test('should emit updated input start date range with time', () => {
    component.ngOnInit();
    jest.spyOn(component, 'dateRangeChanged');

    // any = MatDatepickerInputEvent<Date>
    const updatedDateTime: any = {
    value: new DateRange<moment.Moment>(moment(), component.dateRange.end)
    }

    component.startTimeChange(updatedDateTime);

    expect(component.selectedDateRangeOption).toBe(DateRangeOption.Custom);
    expect(component.dateRange.start?.date()).toEqual(moment(updatedDateTime.value).date()); // Here's where it fails
    expect(component.dateRange.end).toEqual(component.dateRange.end);
    expect(component.dateRangeChange.emit).toHaveBeenCalledWith(component.dateRange);
    expect(component.dateRangeChanged).toHaveBeenCalledTimes(1);
    });
    });

    describe('endTimeChange', () => {
    test('should emit updated input end date range with time', () => {
    component.ngOnInit();
    jest.spyOn(component, 'dateRangeChanged');

    // any = MatDatepickerInputEvent<Date>
    const updatedDateTime: any = {
    value: new DateRange<moment.Moment>(component.dateRange.end, moment())
    }

    component.endTimeChange(updatedDateTime);

    expect(component.selectedDateRangeOption).toBe(DateRangeOption.Custom);
    expect(component.dateRange.start).toEqual(component.dateRange.start);
    expect(component.dateRange.end?.date()).toEqual(moment(updatedDateTime.value).date()); // Here's where it fails
    expect(component.dateRangeChange.emit).toHaveBeenCalledWith(component.dateRange);
    expect(component.dateRangeChanged).toHaveBeenCalledTimes(1);
    });
    });

    Continue reading...

Compartilhe esta Página