我正在为指令(在输入事件上调用)编写单元测试,该指令正在修改 formControl 上的输入值。我在我的规范文件中为此创建了一个测试组件。我注意到 triggerEventHandler() 和 dispatchEvent() 之间的区别,当 dispatchEvent() 正确触发事件并触发指令时,在 triggerEventHandler() 事件未触发的情况下。谁能告诉我,除了在 nativeElement 上调用 dispatchEvent() 之外,它们之间有什么区别。
// directive
export class AlphaNumericCheckDirective {
constructor(private ctrl: NgControl) {
}
@HostListener('input')
onInputChange() {
const pattern = /[^0-9]/g;
const elVal = (this.ctrl.control as AbstractControl).value;
if (pattern.test(elVal)) {
const newVal = elVal.replace(pattern, '');
(this.ctrl.control as AbstractControl).setValue(newVal);
}
}
}
// relevant code of test
it('should allow only numerics', () => {
const fixture = TestBed.createComponent(TestComponent);
const inputEl = fixture.debugElement.query(By.css('input'));
(fixture.componentInstance.testGroup.get('testControl') as
AbstractControl).patchValue('12a');
inputEl.triggerEventHandler('input', null); // not …Run Code Online (Sandbox Code Playgroud) 我正在努力提高网站的性能分数。Reduce JavaScript execution time在under中运行 lighthouse 时diagnostics,我看到Script Evaluation一些 js 脚本的时间很长。
如果我在不同的环境中运行 lighthouse different evaluation time for the same scripts,这种差异非常大,例如对于一个脚本,它从 1700 毫秒变为 100 毫秒,
我想了解how lighthouse calculates the evaluation time,这样我就可以努力减少它。
这些 js 文件比其他评估时间较短的文件相对较小,这是我无法理解的。如果重要的话,在分析中我可以看到同一脚本的执行时间是 45 毫秒。
PS-我知道脚本执行和评估是不同的
谢谢任何帮助,将不胜感激。