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 |