【发布时间】:2020-05-26 06:55:19
【问题描述】:
这里是我比较日期的 SelectEndDate 函数。
selectEndDate = () => {
const currentDate = this.createDate();
const endDate = this.addSurveyForm.value.endDate;
const startDate = this.addSurveyForm.value.startDate;
Eif (startDate === undefined || startDate === null || startDate === '') {
this.addSurveyForm.patchValue({
endDate: new FormControl({ value: ' ', disabled: true }, Validators.required)
});
} else {
if (new Date(endDate) < new Date(currentDate)) {
this.addSurveyForm.patchValue({
endDate: ''
});
}
if (new Date(endDate) < new Date(startDate)) {
this.addSurveyForm.patchValue({
endDate: ''
});
}
}
}
我想为此功能编写测试用例。要检查开始日期是否为空,如果是,那么我写了这个案例
it('should call selectEndDate if startDate is null ', () => {
const startDate = '';
component.selectEndDate();
fixture.detectChanges();
expect(startDate).toBe('');
});
现在我不知道如何涵盖其他部分,我无法使用小于运算符来表示日期。 所以请帮帮我。
【问题讨论】:
-
您可以在 javascript 的日期对象上使用 getTime 方法进行比较。喜欢
new Date().getTime() < startDate.getTime()。 FYR - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
标签: angular karma-jasmine date-comparison