Spaces:
Running

File size: 2,638 Bytes
2cd0dd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
import { act, renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { settingsPubSub } from "@/modules/pubSub";
import { defaultSettings } from "@/modules/settings";
import { useSettingsForm } from "./useSettingsForm";

const [updateSettings, onSettingsChange, getSettings] = settingsPubSub;

describe("useSettingsForm", () => {
  beforeEach(() => {
    updateSettings(defaultSettings);
  });

  it("holds only the keys it was given", () => {
    const { result } = renderHook(() =>
      useSettingsForm(["enterToSubmit", "enableTextSearch"]),
    );

    expect(result.current.values).toEqual({
      enterToSubmit: true,
      enableTextSearch: true,
    });
  });

  it("writes a changed key into the settings", () => {
    const { result } = renderHook(() => useSettingsForm(["enterToSubmit"]));

    act(() => result.current.setFieldValue("enterToSubmit", false));

    expect(getSettings()).toEqual({ ...defaultSettings, enterToSubmit: false });
  });

  it("keeps a value another writer changed after the form was opened", () => {
    const { result } = renderHook(() => useSettingsForm(["enterToSubmit"]));

    act(() =>
      updateSettings({
        ...getSettings(),
        enableTextSearch: false,
        wllamaModelId: "another-model",
      }),
    );
    act(() => result.current.setFieldValue("enterToSubmit", false));

    expect(getSettings()).toEqual({
      ...defaultSettings,
      enableTextSearch: false,
      wllamaModelId: "another-model",
      enterToSubmit: false,
    });
  });

  it("writes every key of one change in a single update", () => {
    updateSettings({
      ...defaultSettings,
      textToSpeechEngine: "local",
      selectedVoiceId: "piper:voice",
    });
    const { result } = renderHook(() =>
      useSettingsForm(["textToSpeechEngine", "selectedVoiceId"]),
    );
    const onChange = vi.fn();
    const unsubscribe = onSettingsChange(onChange);

    act(() =>
      result.current.setValues({
        textToSpeechEngine: "system",
        selectedVoiceId: "",
      }),
    );
    unsubscribe();

    expect(onChange).toHaveBeenCalledTimes(1);
    expect(getSettings()).toMatchObject({
      textToSpeechEngine: "system",
      selectedVoiceId: "",
    });
  });

  it("does not write when no value changed", () => {
    const { result } = renderHook(() => useSettingsForm(["enterToSubmit"]));
    const onChange = vi.fn();
    const unsubscribe = onSettingsChange(onChange);

    act(() => result.current.setFieldValue("enterToSubmit", true));
    unsubscribe();

    expect(onChange).not.toHaveBeenCalled();
  });
});