简体   繁体   中英

How to pass mock date event value on unit testing in angular

I have a function which needs date from datepicker event. I used matdatepicker to select date from UI.

i want to pass date event value on onDateSelected() function.

can anybody help me to pass date event value to pass my unit test case.

i tried below code but it wont works

my function

 onDateSelected(event: MatDatepickerInputEvent<Date>) {
    const dts = event.value;
    const startDate = moment(dts).format('MM/DD/YYYY');
    this.details.controls.contractStartDate.setValue(startDate);
    this.selectedDate = this.details.get('contractStartDate').value;
    this.selectedDate = event.value;
  }

Unit testing code

  it('should check ondate selected event', () => {
    const date = new Date(10/03/2019);
    component.onDateSelected(date);
    fixture.detectChanges();
    expect(component.details.get('contractStartDate').value.toString()).toEqual(date);
  });
const date: any = { value : new Date(10/03/2019) };

Just look at how you write your function, and follow the object properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM