我写了一个 svelte 组件App,你可以在其中写一个句子,input然后该句子将在h1.
App.svelte
<script>
let sentence = "Hello world";
</script>
<main>
<h1>{sentence}</h1>
<input
value={sentence}
type="text"
on:input={(value) => {
sentence = value.target.value
}}
/>
</main>
但是当我尝试使用@testing-library/svelte测试此行为时,输入不是反应性的,输入的文本h1仍然是"Hello world"(但输入中的值已根据第一个改变expect)。
应用程序测试.js
import { render, fireEvent } from "@testing-library/svelte";
import App from "./App.svelte";
it("should write in input", async () => {
const { container } = render(App);
const input = container.querySelector("input[type=text]");
await fireEvent.change(input, { target: { value: "test" } });
expect(input.value).toBe("test"); // ✅
expect(container.querySelector("h1").textContent).toBe("test"); // ❌
});
有一条错误信息:
Expected: "test"
Received: "Hello world"
8 | await fireEvent.change(input, { target: { value: "test" } });
10 | expect(input.value).toBe("test");
> 11 | expect(container.querySelector("h1").textContent).toBe("test");
12 | });
您可以使用codesandbox检查此行为。
有人知道为什么这个测试失败了吗?
慕娘9325324
胡子哥哥
相关分类