PluginProbe ʕ •ᴥ•ʔ
Presto Player / 4.3.2
Presto Player v4.3.2
4.3.2 4.3.1 4.3.0 4.2.4 4.2.3 4.2.2 4.2.0 4.2.1 trunk 1.10.0 1.10.1 1.10.2 1.11.0 1.12.0 1.13.0 1.14.0 1.14.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.13 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3-beta1 2.3.0 2.3.1 2.3.2 2.3.3 3.0.0 3.0.0-beta1 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1.0 3.1.1 3.1.2 3.1.3 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4
presto-player / src / admin / dashboard / hooks / test / useSettingOption.spec.js
presto-player / src / admin / dashboard / hooks / test Last commit date
useCompleteOnboarding.spec.js 1 month ago useDateRangePicker.spec.js 2 months ago useEmail.spec.js 2 months ago useEngagementChartData.spec.js 2 months ago useLicenseSettings.spec.js 2 months ago useLink.spec.js 2 months ago useMediaDetail.spec.js 2 months ago useMediaLibrary.spec.js 2 months ago useMediaList.spec.js 1 month ago usePerformanceSettings.spec.js 2 months ago useRegisterActivePage.spec.js 2 months ago useSettingOption.spec.js 2 months ago useSimpleSettingsPage.spec.js 2 months ago useTopPerforming.spec.js 2 months ago useTopVideosPaginated.spec.js 2 months ago useUpgradeCTA.spec.js 2 months ago useUserDetail.spec.js 2 months ago
useSettingOption.spec.js
128 lines
1 import { renderHook, act } from "@testing-library/react-hooks";
2
3 const mockUseSettingsData = jest.fn();
4
5 jest.mock("../../pages/settings/shared/SettingsDataProvider", () => ({
6 useSettingsData: () => mockUseSettingsData(),
7 }));
8
9 import useSettingOption from "../useSettingOption";
10
11 const stubData = (overrides = {}) => ({
12 settings: {},
13 lastSaved: {},
14 isLoading: false,
15 savingKeys: new Set(),
16 updateLocal: jest.fn(),
17 resetKey: jest.fn(),
18 saveSlice: jest.fn(),
19 ...overrides,
20 });
21
22 beforeEach(() => {
23 mockUseSettingsData.mockReset();
24 });
25
26 describe("useSettingOption", () => {
27 it("returns the stored value when present", () => {
28 mockUseSettingsData.mockReturnValue(
29 stubData({
30 settings: { presto_branding: { logo: "x.png" } },
31 lastSaved: { presto_branding: { logo: "x.png" } },
32 })
33 );
34
35 const { result } = renderHook(() =>
36 useSettingOption("presto_branding", { logo: "" })
37 );
38 expect(result.current.data).toEqual({ logo: "x.png" });
39 expect(result.current.isDirty).toBe(false);
40 });
41
42 it("falls back to defaultValue when the stored option is undefined", () => {
43 mockUseSettingsData.mockReturnValue(stubData());
44 const { result } = renderHook(() =>
45 useSettingOption("missing_key", { fallback: true })
46 );
47 expect(result.current.data).toEqual({ fallback: true });
48 });
49
50 it("falls back to defaultValue when the stored option is null (WP schema-rejected)", () => {
51 mockUseSettingsData.mockReturnValue(
52 stubData({ settings: { foo: null } })
53 );
54 const { result } = renderHook(() => useSettingOption("foo", "default"));
55 expect(result.current.data).toBe("default");
56 });
57
58 it("treats null in lastSaved as defaultValue when computing isDirty", () => {
59 mockUseSettingsData.mockReturnValue(
60 stubData({
61 settings: { foo: "default" },
62 lastSaved: { foo: null },
63 })
64 );
65 const { result } = renderHook(() => useSettingOption("foo", "default"));
66 // data === defaultValue ("default") and effectiveSaved === defaultValue
67 // → not dirty.
68 expect(result.current.isDirty).toBe(false);
69 });
70
71 it("flags isDirty=true when local data differs from lastSaved", () => {
72 mockUseSettingsData.mockReturnValue(
73 stubData({
74 settings: { foo: { a: 1 } },
75 lastSaved: { foo: { a: 2 } },
76 })
77 );
78 const { result } = renderHook(() => useSettingOption("foo", {}));
79 expect(result.current.isDirty).toBe(true);
80 });
81
82 it("uses deep equality so identical-shape objects are not dirty", () => {
83 mockUseSettingsData.mockReturnValue(
84 stubData({
85 settings: { foo: { a: 1, b: { c: 2 } } },
86 lastSaved: { foo: { a: 1, b: { c: 2 } } },
87 })
88 );
89 const { result } = renderHook(() => useSettingOption("foo", {}));
90 expect(result.current.isDirty).toBe(false);
91 });
92
93 it("flags isSaving=true when the option key is in savingKeys", () => {
94 mockUseSettingsData.mockReturnValue(
95 stubData({
96 settings: { foo: "x" },
97 lastSaved: { foo: "x" },
98 savingKeys: new Set(["foo"]),
99 })
100 );
101 const { result } = renderHook(() => useSettingOption("foo", ""));
102 expect(result.current.isSaving).toBe(true);
103 });
104
105 it("setData delegates to updateLocal(optionKey, next)", () => {
106 const updateLocal = jest.fn();
107 mockUseSettingsData.mockReturnValue(stubData({ updateLocal }));
108
109 const { result } = renderHook(() => useSettingOption("foo", ""));
110 act(() => {
111 result.current.setData("new-value");
112 });
113 expect(updateLocal).toHaveBeenCalledWith("foo", "new-value");
114 });
115
116 it("save delegates to saveSlice([optionKey])", () => {
117 const saveSlice = jest.fn();
118 mockUseSettingsData.mockReturnValue(stubData({ saveSlice }));
119
120 const { result } = renderHook(() => useSettingOption("foo", ""));
121 act(() => {
122 result.current.save();
123 });
124 expect(saveSlice).toHaveBeenCalledWith(["foo"]);
125 });
126
127 });
128