summaryrefslogtreecommitdiffhomepage
path: root/src/features/settings/ui/ChatLimitField.test.ts
blob: 8f8da2506767d7d97fac1d22d574bb2df67dfb70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { render, screen } from "@testing-library/svelte";
import userEvent from "@testing-library/user-event";
import { describe, expect, it } from "vitest";
import type { ChatLimitSaveResult } from "../logic/view-model";
import ChatLimitField from "./ChatLimitField.svelte";

// A fake save that resolves ok with the value it was given.
function fakeSave(): {
  saves: number[];
  last: number | null;
  impl: (value: number) => Promise<ChatLimitSaveResult>;
} {
  const saves: number[] = [];
  return {
    saves,
    last: null,
    impl: async (value: number) => {
      saves.push(value);
      return { ok: true, chatLimit: value };
    },
  };
}

describe("ChatLimitField", () => {
  it("seeds the input from the persisted limit and disables Set while unchanged", () => {
    render(ChatLimitField, { props: { chatLimit: 256, save: fakeSave().impl } });
    expect(screen.getByLabelText("Chat limit")).toHaveValue("256");
    expect(screen.getByRole("button", { name: "Set" })).toBeDisabled();
  });

  it("enables Set when the typed value differs (regression: number-binding coercion)", async () => {
    const user = userEvent.setup();
    const save = fakeSave();
    render(ChatLimitField, { props: { chatLimit: 256, save: save.impl } });

    const input = screen.getByLabelText("Chat limit");
    await user.clear(input);
    await user.type(input, "10");

    expect(screen.getByRole("button", { name: "Set" })).toBeEnabled();
  });

  it("keeps Set disabled for non-numeric input", async () => {
    const user = userEvent.setup();
    const save = fakeSave();
    render(ChatLimitField, { props: { chatLimit: 256, save: save.impl } });

    const input = screen.getByLabelText("Chat limit");
    await user.clear(input);
    await user.type(input, "abc");

    expect(screen.getByRole("button", { name: "Set" })).toBeDisabled();
  });

  it("clicking Set calls save with the parsed value and shows the confirmation", async () => {
    const user = userEvent.setup();
    const save = fakeSave();
    const { rerender } = render(ChatLimitField, {
      props: { chatLimit: 256, save: save.impl },
    });

    const input = screen.getByLabelText("Chat limit");
    await user.clear(input);
    await user.type(input, "50");
    await user.click(screen.getByRole("button", { name: "Set" }));

    expect(save.saves).toEqual([50]);
    // In the real app the reactive `chatLimit` prop updates to the saved value
    // (the store sets it synchronously), which clears `dirty` and reveals the
    // "Saved" badge. Rerender simulates that propagation.
    rerender({ chatLimit: 50, save: save.impl });
    expect(screen.getByText(/Saved/i)).toBeInTheDocument();
  });

  it("clamps a below-floor value when saving", async () => {
    const user = userEvent.setup();
    const save = fakeSave();
    render(ChatLimitField, { props: { chatLimit: 256, save: save.impl } });

    const input = screen.getByLabelText("Chat limit");
    await user.clear(input);
    await user.type(input, "5"); // clamps to MIN (10)
    await user.click(screen.getByRole("button", { name: "Set" }));

    // The save port receives the clamped value (the view-model clamps before save).
    expect(save.saves).toEqual([10]);
    expect(input).toHaveValue("10");
  });
});