我寫了一個 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)。應用程序測試.jsimport { 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檢查此行為。有人知道為什么這個測試失敗了嗎?
如何測試苗條的輸入反應性?
ITMISS
2022-12-29 15:19:58