Skip to content
Source
Sign inSign up
Open History to see the last commit

SecretInput.browser.test.tsx

42 lines1.5 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { userEvent } from "vite-plus/test/browser";
3​
4import { render, type Screen } from "../../../test/render";
5import { SecretInput } from "./SecretInput";
6​
7const input = (screen: Screen) => screen.container.querySelector("input")!;
8​
9async function edit(screen: Screen, value: string) {
10 await userEvent.clear(input(screen));
11 if (value) await userEvent.type(input(screen), value);
12 await userEvent.click(document.body);
14​
15describe("SecretInput", () => {
16 it("preserves a configured secret on empty edits and commits a replacement", async () => {
17 const onCommit = vi.fn();
18 await using screen = await render(<SecretInput configured value="" onCommit={onCommit} />);
19 expect(input(screen).type).toBe("password");
20​
21 await userEvent.click(input(screen));
22 await userEvent.click(document.body);
23 expect(onCommit).not.toHaveBeenCalled();
24​
25 await edit(screen, "replacement");
26 expect(onCommit).toHaveBeenCalledExactlyOnceWith("replacement");
27 expect(input(screen).value).toBe("");
28 });
29​
30 it("keeps an in-progress replacement when settings refresh", async () => {
31 const onCommit = vi.fn();
32 const ui = () => <SecretInput configured value="" onCommit={onCommit} />;
33 await using screen = await render(ui());
34​
35 await userEvent.type(input(screen), "replacement");
36 await screen.rerender(ui());
37 expect(input(screen).value).toBe("replacement");
38​
39 await userEvent.keyboard("{Enter}");
40 expect(onCommit).toHaveBeenCalledExactlyOnceWith("replacement");
41 });
42});