我正在编写一个input
组件,并且在测试单击或按键stencil
时触发的发出事件时遇到了麻烦。组件:enter
export class Input {
.
.
private inputRef?: HTMLInputElement;
@Event() inputEvent: EventEmitter<{}>;
.
.
render() {
return (
<div class="status">
<div class="input-wrapper">
<input onKeyUp = {event => this.handleKeyDown(event)} ref={el => this.inputRef = el as HTMLInputElement}/>
</div>
</div>);
}
private handleKeyDown(e: KeyboardEvent): void {
this.dispatchSearchEvent(e);
}
private dispatchSearchEvent(e){
this.inputEvent.emit({type: "event-name", event: e, data: (this.inputRef as HTMLInputElement).value});
}
}
conponent.spec.jsx:
it('fires an event upon enter key press', async () => {
const component = new InputFields();
jest.spyOn(component, 'event-name');
const keyUpEvent = new Event("keyup", {
key: "enter",
keyCode: 13,
which: 13,
shiftKey: false,
ctrlKey: false,
metaKey: false
});
keyUpEvent.target = {value: "some input value"}
component.handleKeyDown(keyUpEvent);
component.inputEvent = { emit: jest.fn().mockReturnValue(Promise.resolve()), };
expect(component.inputEvent.emit).toHaveBeenCalled();
});
有一个错误:
Cannot spy the event-name property because it is not a function; undefined given instead
356 | it('fires an event upon enter key press', async () => {
357 | const component = new Input();
> 358 | jest.spyOn(component, 'event-name');
| ^
359 |
360 | const keyUpEvent = new Event("keyup", {
361 | key: "enter",