PluginProbe ʕ •ᴥ•ʔ
Presto Player / 4.3.3
Presto Player v4.3.3
4.3.3 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 / usePerformanceSettings.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 2 months 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
usePerformanceSettings.spec.js
143 lines
1 import { renderHook, act } from "@testing-library/react-hooks";
2 import apiFetch from "@wordpress/api-fetch";
3 import usePerformanceSettings from "../usePerformanceSettings";
4
5 jest.mock("@wordpress/api-fetch");
6
7 const settle = () => act(async () => {});
8
9 beforeEach(() => {
10 apiFetch.mockReset();
11 });
12
13 describe("usePerformanceSettings", () => {
14 it("hydrates `performance` from /wp/v2/settings on mount", async () => {
15 apiFetch.mockResolvedValueOnce({
16 presto_player_performance: {
17 module_enabled: true,
18 automations: false,
19 },
20 });
21
22 const { result, waitForNextUpdate } = renderHook(() =>
23 usePerformanceSettings()
24 );
25 await waitForNextUpdate();
26 await settle();
27
28 expect(apiFetch).toHaveBeenCalledWith({ path: "/wp/v2/settings" });
29 expect(result.current.isLoading).toBe(false);
30 expect(result.current.performance).toEqual({
31 module_enabled: true,
32 automations: false,
33 });
34 expect(result.current.isDirty).toBe(false);
35 });
36
37 it("keeps the defaults when the response has no performance key", async () => {
38 apiFetch.mockResolvedValueOnce({});
39
40 const { result, waitForNextUpdate } = renderHook(() =>
41 usePerformanceSettings()
42 );
43 await waitForNextUpdate();
44 await settle();
45
46 expect(result.current.performance).toEqual({
47 module_enabled: false,
48 automations: true,
49 });
50 });
51
52 it("logs and clears loading when the fetch fails", async () => {
53 const errorSpy = jest
54 .spyOn(console, "error")
55 .mockImplementation(() => {});
56 apiFetch.mockRejectedValueOnce(new Error("network"));
57
58 const { result, waitForNextUpdate } = renderHook(() =>
59 usePerformanceSettings()
60 );
61 await waitForNextUpdate();
62 await settle();
63
64 expect(result.current.isLoading).toBe(false);
65 expect(errorSpy).toHaveBeenCalled();
66 errorSpy.mockRestore();
67 });
68
69 it("setPerformance flips isDirty true and updates state", async () => {
70 apiFetch.mockResolvedValueOnce({});
71 const { result, waitForNextUpdate } = renderHook(() =>
72 usePerformanceSettings()
73 );
74 await waitForNextUpdate();
75 await settle();
76
77 act(() => {
78 result.current.setPerformance({ module_enabled: true, automations: false });
79 });
80
81 expect(result.current.isDirty).toBe(true);
82 expect(result.current.performance).toEqual({
83 module_enabled: true,
84 automations: false,
85 });
86 });
87
88 it("save POSTs the current performance state and clears isDirty", async () => {
89 apiFetch.mockResolvedValueOnce({});
90 const { result, waitForNextUpdate } = renderHook(() =>
91 usePerformanceSettings()
92 );
93 await waitForNextUpdate();
94 await settle();
95
96 act(() => {
97 result.current.setPerformance({ module_enabled: true, automations: false });
98 });
99
100 apiFetch.mockResolvedValueOnce({ ok: true });
101
102 await act(async () => {
103 await result.current.save();
104 });
105
106 expect(apiFetch).toHaveBeenLastCalledWith({
107 path: "/wp/v2/settings",
108 method: "POST",
109 data: {
110 presto_player_performance: {
111 module_enabled: true,
112 automations: false,
113 },
114 },
115 });
116 expect(result.current.isDirty).toBe(false);
117 expect(result.current.isSaving).toBe(false);
118 });
119
120 it("save clears isSaving and rethrows on failure", async () => {
121 apiFetch.mockResolvedValueOnce({});
122 const { result, waitForNextUpdate } = renderHook(() =>
123 usePerformanceSettings()
124 );
125 await waitForNextUpdate();
126 await settle();
127
128 act(() => {
129 result.current.setPerformance({ module_enabled: true, automations: true });
130 });
131
132 apiFetch.mockRejectedValueOnce(new Error("server"));
133
134 await act(async () => {
135 await expect(result.current.save()).rejects.toThrow("server");
136 });
137
138 expect(result.current.isSaving).toBe(false);
139 // Failure leaves isDirty true so the user can retry.
140 expect(result.current.isDirty).toBe(true);
141 });
142 });
143