Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度单元测试素数ng日历_Angular_Unit Testing_Primeng_Primeng Calendar - Fatal编程技术网

Angular 角度单元测试素数ng日历

Angular 角度单元测试素数ng日历,angular,unit-testing,primeng,primeng-calendar,Angular,Unit Testing,Primeng,Primeng Calendar,在单元测试中,当我有一个常规元素时,我可以设置它的值,触发一个更改事件,并验证表单值是否匹配,如下所示: const hostElement = fixture.nativeElement; const userIdSelect: HTMLInputElement = hostElement.querySelector('select[formcontrolname="userId"]'); userIdSelect.value = 'myUser'; userIdSelect.dispatc

在单元测试中,当我有一个常规元素时,我可以设置它的值,触发一个更改事件,并验证表单值是否匹配,如下所示:

const hostElement = fixture.nativeElement;
const userIdSelect: HTMLInputElement = 
hostElement.querySelector('select[formcontrolname="userId"]');
userIdSelect.value = 'myUser';
userIdSelect.dispatchEvent(new Event('change'));  
fixture.detectChanges();
expect(component.form.controls.userId.value).toBe('myUser');
我似乎无法让它与主ng日历控件一起工作

const myDate= new Date(2019,11,31);
const myDateInput: HTMLInputElement = hostElement.querySelector('p-calendar[formcontrolname="myDate"] input');
myDateInput.value = myDate.toLocaleDateString();
myDateInput.dispatchEvent(new Event('input'));
expect(component.form.controls.myDate.value).toBe(myDate.toLocaleDateString());
我觉得这是因为表单绑定到p-calendar元素,而不是它的子输入:

<p-calendar dateFormat="dd/mm/yy" formControlName="myDate"
showIcon="true"
[readonlyInput]="false"></p-calendar>


我尝试过在输入端发送各种事件(模糊、输入等),但不管怎样,我似乎都无法更新表单。有什么想法吗?我应该设置p-calendar元素的值而不是它的输入吗?

在浏览了p-calendar源代码之后,需要两件事才能使其工作。 首先,输入字符串的日期格式必须符合calendar控件的预期,在我的例子中:

myDateInput.value = moment(myDate).format('DD/MM/YYYY');
第二,正因为如此:

必须调度keydown事件

myDateInput.dispatchEvent(new Event('keydown'));