【发布时间】:2021-11-17 15:35:18
【问题描述】:
我想在 stencilJs 中为我的自定义 Web 组件编写单元测试,但不知道如何以正确的方式进行。这是我到目前为止所做的!
.tsx
...
valueFormat(event: Event): void {
const val = (event.target as HTMLInputElement).value;
const format = Number.parseInt(val, 10);
const newVal = format.toLocaleString(undefined, {
minimumFractionDigits: 2,
});
this.value = newVal;
}
.spec.tsx
it('should format value', async () => {
const comp = new MyComponent();
const spy = jest.spyOn(comp, 'valueFormat');
comp.myInputEvent.emit();
expect(spy).toHaveBeenCalled();
});
我想测试这个案例,当我在输入字段中输入一个数字时,它会格式化它。所以我的 valueFormat() 方法应该在键盘事件触发时调用。希望你能帮帮我!
【问题讨论】:
标签: reactjs jestjs event-handling stenciljs