gpt4 book ai didi

Angular 如何测试@HostListener

转载 作者:太空狗 更新时间:2023-10-29 18:15:18 26 4
gpt4 key购买 nike

我有以下指令。当应用于输入元素时,它会检查字符并在禁止字符时调用 preventDefault:

@Directive({
selector: '[cdtPreventInput]'
})
export class PreventInputDirective implements OnInit {

// the list of characters that are to be prevented
@Input() cdtPreventInput: String;

constructor() { }

ngOnInit() {
if (!this.cdtPreventInput) {
throw new Error('cdtPreventInput cannot be used without providing a
list of characters.');
}
}

@HostListener('keypress', ['$event']) onKeyPress(event) {
if (_.includes(this.cdtPreventInput.split(','), event.key)) {
event.preventDefault();
}
}

工作正常,但我不知道如何测试它。到目前为止,我有以下内容:

describe('PreventInputDirective', () => {
let fixture;
let input: DebugElement;

beforeEach(() => {
fixture = TestBed.configureTestingModule({
declarations: [PreventInputDirective, TestComponent]
}).createComponent(TestComponent);

input = fixture.debugElement.query(By.directive(PreventInputDirective));
});

it('should create an instance', () => {
const directive = new PreventInputDirective();
expect(directive).toBeTruthy();
});

it('should prevent default keypress event', () => {
const event = new KeyboardEvent('keypress', {
'key': '.'
});
input.nativeElement.dispatchEvent(event);

expect(input.nativeElement.value).toEqual('');
});

@Component({
template: `<input cdtPreventInput="." />`
})
class TestComponent { }
});

虽然它不起作用。不触发按键事件。知道如何测试这个指令吗?

最佳答案

我找到了一个解决方案。我没有检查值(永远不会改变),而是检查事件的 defaultPrevented 属性。

我还遗漏了两件事:

  • fixture.detectChanges();在每个

  • 事件应该是可以取消的

这是完整的测试:

     describe('PreventInputDirective', () => {
let fixture;
let input: DebugElement;

beforeEach(() => {
fixture = TestBed.configureTestingModule({
declarations: [PreventInputDirective, TestComponent]
}).createComponent(TestComponent);

input = fixture.debugElement.query(By.directive(PreventInputDirective));
fixture.detectChanges();
});

it('should create an instance', () => {
const directive = new PreventInputDirective();
expect(directive).toBeTruthy();
});

it('should prevent keypress event', () => {
const event = new KeyboardEvent('keypress', {
'key': '.',
cancelable: true
});

input.nativeElement.dispatchEvent(event);
expect(event.defaultPrevented).toBeTruthy();
});

it('should not prevent keypress event', () => {
const event = new KeyboardEvent('keypress', {
'key': '5',
cancelable: true
});

input.nativeElement.dispatchEvent(event);
expect(event.defaultPrevented).toBeFalsy();
});

@Component({
template: `<input cdtPreventInput="." />`
})
class TestComponent { }
});

关于Angular 如何测试@HostListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48931164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com