问题是,在用户从字段中移出之前(例如,单击其他位置或按TAB键),错误消息实际上不会显示。
inputElement
派遣
模糊
活动:
inputElement.dispatchEvent(new Event('blur'));
it('should display error when the inserted description text is too long', () => {
const inputElement: HTMLInputElement = hostElement.querySelector('.input-element');
inputElement.value = getRandomString(256);
inputElement.dispatchEvent(new Event('input'));
inputElement.dispatchEvent(new Event('blur')); // this line has been added
fixture.detectChanges();
const errorElement: HTMLElement = hostElement.querySelector('.error-element');
expect(errorElement).toBeTruthy();
expect(errorElement.innerText).toContain('Please enter no more than 255 characters.');
});