Jest throwing TypeError : this.inputEl.focus is not a function #1964
Jest throwing TypeError : this.inputEl.focus is not a function #1964
模板版本:
@stencil/core@1.7.0
开玩笑版本:
"jest": "24.8.0"
当前行为:
我正在尝试将输入元素集中在按钮单击上。
哪个工作正常,但是在尝试使用 npm test
测试功能时,jest
抛出一个 TypeError
说 focus 不是一个函数。
对于所有手动事件调用,如 click
、blur
、focus
,此错误正在重复出现。
因此测试用例没有通过。
预期行为:
它不应该抛出错误。
重现步骤:
我正在提供相关的演示代码以供检查。
相关代码:
demo-btn.tsx
import { Component, h, Element } from '@stencil/core';
@Component({
tag: 'demo-btn',
styleUrl: 'demo-btn.css',
shadow: true
})
export class DemoBtnComponent {
@Element() el!: HTMLElement;
private inputEl?: HTMLElement;
onClick = () => {
if (this.inputEl) {
this.inputEl.focus();
}
}
render() {
return (
<div class="input-container">
<input ref={el => this.inputEl = el} type="text" />
<button onClick={this.onClick}>
Click Me
</button>
</div>
);
}
}
demo-btn.spec.tsx
import { newSpecPage } from '@stencil/core/testing';
import { DemoBtnComponent } from './demo-btn';
describe('my-component', () => {
it('should focus input el on btn click', async ()=> {
const page = await newSpecPage({
components: [DemoBtnComponent],
html: '<demo-btn></demo-btn>',
});
const btn = page.root.shadowRoot.querySelector('button')
btn.click(); // Throws error after this line
await page.waitForChanges();
expect(true).toBeTruthy(); // For sake of completion
});
});
任何帮助将不胜感激。
我通过模拟输入元素的 focus
解决了这个问题。
下面是我试过的代码:
import { newSpecPage } from '@stencil/core/testing';
import { DemoBtnComponent } from './demo-btn';
describe('my-component', () => {
it('should focus input el on btn click', async ()=> {
const page = await newSpecPage({
components: [DemoBtnComponent],
html: '<demo-btn></demo-btn>',
});
/** Mock Input Elements focus function */
const inputEl = page.root.querySelector('input');
inputEl.focus = jest.fn();
const btn = page.root.querySelector('button')
btn.click();
await page.waitForChanges();
expect(true).toBeTruthy();
});
});
关闭此问题。
我遇到过类似的问题,但是如果 focus() 函数在生命周期事件中,则上述解决方案不起作用。因此,为了解决这个问题,我在代码本身中使用了可选链接:
this.inputEl?.focus?.();
本文中的更多详细信息:https://github.com/ionic-team/stencil/issues/1964
模板版本:
@stencil/core@1.7.0
开玩笑版本:
"jest": "24.8.0"
当前行为:
我正在尝试将输入元素集中在按钮单击上。
哪个工作正常,但是在尝试使用 npm test
测试功能时,jest
抛出一个 TypeError
说 focus 不是一个函数。
对于所有手动事件调用,如 click
、blur
、focus
,此错误正在重复出现。
因此测试用例没有通过。
预期行为:
它不应该抛出错误。
重现步骤: 我正在提供相关的演示代码以供检查。 相关代码:
demo-btn.tsx
import { Component, h, Element } from '@stencil/core';
@Component({
tag: 'demo-btn',
styleUrl: 'demo-btn.css',
shadow: true
})
export class DemoBtnComponent {
@Element() el!: HTMLElement;
private inputEl?: HTMLElement;
onClick = () => {
if (this.inputEl) {
this.inputEl.focus();
}
}
render() {
return (
<div class="input-container">
<input ref={el => this.inputEl = el} type="text" />
<button onClick={this.onClick}>
Click Me
</button>
</div>
);
}
}
demo-btn.spec.tsx
import { newSpecPage } from '@stencil/core/testing';
import { DemoBtnComponent } from './demo-btn';
describe('my-component', () => {
it('should focus input el on btn click', async ()=> {
const page = await newSpecPage({
components: [DemoBtnComponent],
html: '<demo-btn></demo-btn>',
});
const btn = page.root.shadowRoot.querySelector('button')
btn.click(); // Throws error after this line
await page.waitForChanges();
expect(true).toBeTruthy(); // For sake of completion
});
});
任何帮助将不胜感激。
我通过模拟输入元素的 focus
解决了这个问题。
下面是我试过的代码:
import { newSpecPage } from '@stencil/core/testing';
import { DemoBtnComponent } from './demo-btn';
describe('my-component', () => {
it('should focus input el on btn click', async ()=> {
const page = await newSpecPage({
components: [DemoBtnComponent],
html: '<demo-btn></demo-btn>',
});
/** Mock Input Elements focus function */
const inputEl = page.root.querySelector('input');
inputEl.focus = jest.fn();
const btn = page.root.querySelector('button')
btn.click();
await page.waitForChanges();
expect(true).toBeTruthy();
});
});
关闭此问题。
我遇到过类似的问题,但是如果 focus() 函数在生命周期事件中,则上述解决方案不起作用。因此,为了解决这个问题,我在代码本身中使用了可选链接:
this.inputEl?.focus?.();
本文中的更多详细信息:https://github.com/ionic-team/stencil/issues/1964